Yes, I'm trying to automate Excel from Access VBA. I have all the data in Access and executing the SQL in Access writing data in particular cells and after that I need to sum these cells. And these number of columns need to dynamic e.g. it can be =SUM(B3:B7) or =SUM(B3:B10) depends on number of records fetched by SQL.
Ria, nothing was attached, u can try this code to sum, 1 to 80 is column
Code:
Private Sub CommandButton1_Click()
Dim LastRow As Long
For i = 1 To 80
LastRow = Cells(Rows.Count, i).End(xlUp).Row
Cells(LastRow + 1, i) = WorksheetFunction.Sum(Range(Cells(1, i), Cells(LastRow, i)))
Next
End Sub
1) When working with sheets fully `Qualify` your objects else the code will work with the "Active" sheet
2) The above is also true and crucial when finding last row. See the EXTRA DOT before Rows.Count. This usually causes a problem when you open xls file in compatibility mode and don't use the EXTRA DOT.
Something like this
Code:
With objXLSheet
LastRow = .Cells(.Rows.Count, i).End(xlUp).Row
End With
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Ria, I don't see the whole code but my guess is that you have not specified the sheet.
Code:
With objXLSheet
For i = 1 To 80
RowEnd = .Cells(.Rows.count, i).End(xlUp).Row
.Cells(RowEnd + 1, i) = WorksheetFunction.SUM(.range(Cells(1, i), .Cells(RowEnd, i)))
Next
End With
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
@ sid
wouldn't he want to keep all the totals on the same row?
could always put the formula into the cells, to allow sheet editing later
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
can either put into cells on row above totals as text, or set top border of range of cells containing totals
for her to
missed that part, apology
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
what value has tempstring? should be something like "R" & startrow & "C"
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Can you please help to explain if I need to calculated sum from different range e.g. Row 15 to Row 20, and later using same code Row 23 to 28 and so on.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
One more question, how to get the total for range on columns?
i don't understand, i thought that was what you already did for each section
if you sum the columns now, it will include the sum for each section as well as the sum of all rows
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
use the same for summing rows as you do for columns, just change the RC values to suit, loop rows rather than columns
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Sub calculateRowSubtotal(sRange As String, sumRowStart As Integer)
With objXLSheet.range(sRange)
objXLSheet.range(sRange).FormulaR1C1 = "=SUM(RC[-12]:RC[-1])"
End With
End Sub
Only issue is this shows the formula in the cell. What about if want to show actual number only instead of formula in cells?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete