Error on using quotation marks

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
Office Version
  1. 2019
Platform
  1. Windows
I am trying to put sum formula for last rows of columns L, M, N however I get error when I try to run the code below and I think it's caused by the placing of quotation marks?


VBA Code:
Sub TotalAmt()

Dim lr as String

lr = Sheets(6).Cells(Rows.Count, "D").End(xlUp).Row
Sheets(6).Range("L" & lr : "N" & lr).Formula = "=SUM(L8:L" & lr -1 & ")"

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA Code:
Sub TotalAmt()

Dim lr as Long
lr = Sheets(6).Cells(Rows.Count, "D").End(xlUp).Row
Sheets(6).Range("L" & lr & ":N" & lr).Formula = "=SUM(L8:L" & lr - 1 & ")"
End Sub
 
Upvote 0
Note that I also amended variable type to LONG
 
Upvote 0
VBA Code:
Sub TotalAmt()

Dim lr as Long
lr = Sheets(6).Cells(Rows.Count, "D").End(xlUp).Row
Sheets(6).Range("L" & lr & ":N" & lr).Formula = "=SUM(L8:L" & lr - 1 & ")"
End Sub

Thanks! Do you have any reference where I could study the proper usage of "" on coding?
Most of the time those "" are the reason for my error when running the code ?
 
Upvote 0
I simply use this method and look to see what is written to the immediate window - which is displayed in VBA editor with {CTRL} g
With complicated strings, I build up the string in stages
I only put the string in the code after I get the syntax correct ;)

Rich (BB code):
Dim lr as Long
lr = Sheets(6).Cells(Rows.Count, "D").End(xlUp).Row
Debug.Print "L" & lr & ":N" & lr
Debug.Print "=SUM(L8:L" & lr - 1 & ")"
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top