last day (15 days later) » 

8:52 PM
0
Q: Run vba code on the selection of first listRow in excel

Mahmoud TariefIn a structured table when table is initially created the listRow is 0. Let us say in Sheet1 we have a listobject Table1 I need to run the following code: ThisWorkbook.Worksheets("Sheet1").ListObjects("Table1").ListRows.Add AlwaysInsert:=True. when Table1 has no rows and the first row is clicked ...

 
May I see your workbook?
 
@SiddharthRout: To test....unprotect the sheet and create a 2nd row you will find the formula coming. protect the sheet and try to enter QTY and Price formula will not work
 
I just entered the values and it is working. It is giving me the total in last column
Are you sure your calculations are set to Automatic and not Manual? Check the Formulas Tab|Calculation Options
 
@SiddharthRout: Yes it is automatic..Look I added a GIF image explaining the problem..appreciate your help
Hi
 
Hi :)
 
8:53 PM
Thanks for the support btw :)
 
Ok Do this
1. Unprotect your worksheet
2. Add a second row
3. Check if calculations are working
4. Delete the 1st row and not the 2nd row
Now protect the sheet and try gain
is this what you are trying?
 
It is working. It is only not working when list row is fresh in the table while in the table definition for the 3rd cell there is a formula
I'm not sure if I'm able to explain this correctly
 
hmm.. one moment
 
Ok when the table is fresh, you execute that code and you can see the formula in row 2 and not 1. Is that your question?
 
9:00 PM
Yes
No code it is a standard formula
 
Code = ThisWorkbook.Worksheets("Sheet1").ListObjects("Table1").ListRows.Add AlwaysInsert:=True
 
1- created table
2- added formula
3- deleted all rows
4- protected sheet
5- try to enter data so formula would auto calculate
6- not working
7- unprotect table
8- delete row
9- try the same again without protecting table
10- working fine for row 1
 
Do exactly this... and then tell me what happens... ok?
 
Cell A1: A3 - Type your table headers
Sorry A1:C1
Done?
 
9:05 PM
Yes
 
Select A1:C1 and click on "Insert" Tab and click on "Table"
I hope you are doing this in a new sheet
 
Yes
My table has headers
 
In the dialog box check the box, "My Table have headers
correct
 
Ok continue
 
In C2, type the formula "=[aaa]+[bbb]" these are your headers
 
9:08 PM
done
 
Highlight rows 2 to 5 and delete them
 
I only have 1 row now
 
Correct
and you can't see the formula
 
correct?
 
9:09 PM
it returns zero
 
correct
but if you put any value in col 1 and 2 do you get the sum?
 
awesome. now run the code from the vba immediate window
ThisWorkbook.Worksheets("Sheet1").ListObjects("Table4").ListRows.Add AlwaysInsert:=True
change sheet name and table name
 
sheet> right click> view code?
or somewhere else?
 
yes or alt F11
do you see the Immediate window
if not then press ctrl G
 
9:12 PM
yes got it after ctrl G
done
 
now run the code
paste the code in immediate windod and press enter
*window
 
ok it added a 2nd row
calculated cell as zero
 
does it have the formula?
 
that is fine
 
9:14 PM
has formula
 
now everytime you run the code, a row will be added with 0. That 0 can also be taken care of
i.e show nothing if there is nothing
 
ok, 1 question
 
=IF([aaa]+[sss]=0,"",[aaa]+[sss])
yes?
 
let us delete these 2 rows> enter A2 and B2 any values
formula is restored and working fine
 
9:16 PM
but when cell is protected, same behavior is not happening
 
let me check
Yes that is becuase your sheet is protected... you need to unprotect it in code. Add the rows and then reprotect it...
Sub Sample()
    With ThisWorkbook.Worksheets("Sheet1")
        .Unprotect
        .ListObjects("Table1").ListRows.Add AlwaysInsert:=True
        .Protect
    End With
End Sub
 
great, I want to run this whenever I press on A2:B2 to enter their data
so that code will add line to restore the formula
 
meaning then whenever you enter a value in A2:B2, a new row should be added?
 
not 2nd row. First row should be added
 
a row above thatshould be added
 
9:22 PM
because when cell is protected and I enter in A2, excel doesn't feel that a row is added
That is why formula is not calculating
 
you want a new row to be added (as the 1st row)
 
one moment... adding an answer to your question
 
I'm sorry, do you have AMMYY or team viewer I will give u access to my screen
If not possible then no issues
 
yes i have
but i am already posting an answer...
you dont want me to post an answer?
 
9:24 PM
yea sure, but you addressed the problem, right?
you have the same on your pc?
 
yeah the moment you enter text in A2:B2, a new row should be added on top
with the formula intact
 
no, look just 1 min on team viewer
I will show u
 
590 151 071
s6u2e9
 
9:42 PM
Posted an answer
please delete the teamviewer details from above for your own safety
 

last day (15 days later) »