This question already has answers here:
Better way to find last used row
(9 answers)
Closed 10 months ago.
I've created a program that is supposed to select an entire column and autofill it to the end starting with a formula that will ALWAYS be in a fixed spot on the column. However, the issue is that in terms of row numbers, the number changes daily, so I can't hardcode an ending range.
I tried to do this:
Range("W11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R01""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[R01-PO QTY]]="""",0,[#[R01-PO QTY]])" & Chr(10) & " +IF([#[R01-ALC QTY]]="""",0,[#[R01-ALC QTY]])" & Chr(10) & " +IF([#[R01-JOB QTY]]="""",0,[#[R01-JOB QTY]])" & Chr(10) & " +IF([#[R01-GIT QTY]]="""",0,[#[R01-GIT QTY]])," & Chr(10) & "R[-1]C)"
Range("W11").Select
Selection.AutoFill Destination:=Range("W11:W")
But it returns a "Method Range of Object _Global failed.
What do I do instead? How do I select the entire column AFTER W11 in this case?
I got this working. If anyone is wondering what I did, I did it in a sort of roundabout way. I first created a function called GetLastRow
Function GetLastRow(Strt As String, Cur As String)
'''''
'Get the row of the last line of data
'''''
GetLastRow = Range(Strt, Range(Cur).End(xlDown)).Rows.Count
If GetLastRow > 1000000 Then
GetLastRow = ElimAlpha(Cur)
End If
End Function
I then created some Dims to get the last row for EACH of my columns. Worked beautifully.
Sub RunningBalances()
Dim NumRows01 As Integer
Dim NumRows02 As Integer
Dim NumRowsRDS As Integer
Dim NumRows1 As Integer
Dim NumRowsPDS As Integer
'
' RunningBalances Macro
'
'
'CompanyR W/H 01
NumRows01 = GetLastRow("W11", "W11" & VBA.CStr(StrtPOs))
Range("W11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R01""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[R01-PO QTY]]="""",0,[#[R01-PO QTY]])" & Chr(10) & " +IF([#[R01-ALC QTY]]="""",0,[#[R01-ALC QTY]])" & Chr(10) & " +IF([#[R01-JOB QTY]]="""",0,[#[R01-JOB QTY]])" & Chr(10) & " +IF([#[R01-GIT QTY]]="""",0,[#[R01-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("W11" & ":W" & NumRows01)
'CompanyR W/H 02
NumRows02 = GetLastRow("AF11", "AF11" & VBA.CStr(StrtPOs))
Range("AF11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R02""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[R02-PO QTY]]="""",0,[#[R02-PO QTY]])" & Chr(10) & " +IF([#[R02-ALC QTY]]="""",0,[#[R02-ALC QTY]])" & Chr(10) & " +IF([#[R02-JOB QTY]]="""",0,[#[R02-JOB QTY]])" & Chr(10) & " +IF([#[R02-GIT QTY]]="""",0,[#[R02-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("AF11" & ":AF" & NumRows02)
'CompanyR W/H DS
NumRowsRDS = GetLastRow("AO11", "AO11" & VBA.CStr(StrtPOs))
Range("AO11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""RDS""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[RDS-PO QTY]]="""",0,[#[RDS-PO QTY]])" & Chr(10) & " +IF([#[RDS-ALC QTY]]="""",0,[#[RDS-ALC QTY]])" & Chr(10) & " +IF([#[RDS-JOB QTY]]="""",0,[#[RDS-JOB QTY]])" & Chr(10) & " +IF([#[RDS-GIT QTY]]="""",0,[#[RDS-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("AO11" & ":AO" & NumRowsRDS)
'CompanyP W/H 1
NumRows1 = GetLastRow("AX11", "AX11" & VBA.CStr(StrtPOs))
Range("AX11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""P1""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[P1-PO QTY]]="""",0,[#[P1-PO QTY]])" & Chr(10) & " +IF([#[P1-ALC QTY]]="""",0,[#[P1-ALC QTY]])" & Chr(10) & " +IF([#[P1-JOB QTY]]="""",0,[#[P1-JOB QTY]])" & Chr(10) & " +IF([#[P1-GIT QTY]]="""",0,[#[P1-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("AX11" & ":AX" & NumRows1)
'CompanyP W/H DS
NumRowsPDS = GetLastRow("BG11", "BG11" & VBA.CStr(StrtPOs))
Range("BG11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R02""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[PDS-PO QTY]]="""",0,[#[PDS-PO QTY]])" & Chr(10) & " +IF([#[PDS-ALC QTY]]="""",0,[#[PDS-ALC QTY]])" & Chr(10) & " +IF([#[PDS-JOB QTY]]="""",0,[#[PDS-JOB QTY]])" & Chr(10) & " +IF([#[PDS-GIT QTY]]="""",0,[#[PDS-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("BG11" & ":BG" & NumRowsPDS)
I have a long MySQL query in VBA.
I tried to put a space and '_'. What am I missing?
Sql = "insert IGNORE into nhc (date,dealer_code,name,area_executive,address1,address2,address3,area_territory_id,area_territory_name,micro_market_id,micro_market_name,town,postcode,state,area_name,distributor,remark,may_2020_ga,may_2020_awtu10,may_2020_sellin,may_2020_awmi10,jun_2020_ga,jun_2020_awtu10, _
jun_2020_sellin,jun_2020_awmi10,jul_2020_ga,jul_2020_awtu10,jul_2020_sellin,jul_2020_awmi10,aug_2020_ga,aug_2020_awtu10,aug_2020_sellin,aug_2020_awmi10,dealer_class,may_2020_projected_dealer_class,jun_2020_projected_dealer_class,jul_2020_projected_dealer_class,aug_2020_projected_dealer_class, _
current_clas_awtu_target,current_class_sellin_target,current_class_awmi_target,dealer_status,disc_date,ambitious_dealer?_y/n,shopfront_signage,may_2020_mnp_awtu10,jun_2020_mnp_awtu10,jul_2020_mnp_awtu10,aug_2020_mnp_awtu10,may_2020_ereload_sellin,jun_2020_ereload_sellin,jul_2020_ereload_sellin,aug_2020_ereload_sellin,may_2020_hero_sell_through,jun_2020_hero_sell_through,jul_2020_hero_sell_through,aug_2020_hero_sell_through _
,may_2020_ga_with_ocr,jun_2020_ga_with_ocr,jul_2020_ga_with_ocr,aug_2020_ga_with_ocr)values _
('" & row.Cells(1).Value & "', '" & row.Cells(2).Value & "','" & row.Cells(3).Value & "', '" & row.Cells(4).Value & "', '" & row.Cells(5).Value & "','" & row.Cells(6).Value & "', '" & row.Cells(7).Value & "', '" & row.Cells(8).Value & "','" & row.Cells(9).Value & "', '" & row.Cells(10).Value & "', '" & row.Cells(11).Value & "','" & _
row.Cells(12).Value & "', '" & row.Cells(13).Value & "', '" & row.Cells(14).Value & "','" & row.Cells(15).Value & "', '" & row.Cells(16).Value & "', '" & row.Cells(17).Value & "','" & row.Cells(18).Value & "', '" & row.Cells(19).Value & "', '" & row.Cells(20).Value & "','" & row.Cells(21).Value & "', '" & row.Cells(22).Value & "', '" & row.Cells(23).Value & "','" & row.Cells(24).Value & "', '" & row.Cells(25).Value & "', '" & _
row.Cells(26).Value & "','" & row.Cells(27).Value & "', '" & row.Cells(28).Value & "', '" & row.Cells(29).Value & "','" & row.Cells(30.Value & "', '" & _
row.Cells(31).Value & "', '" & row.Cells(32).Value & "','" & row.Cells(33).Value & "', '" & row.Cells(34).Value & "', '" & row.Cells(35).Value & "','" & row.Cells(36).Value & "', '" & row.Cells(37).Value & "', '" & row.Cells(38).Value & "','" & _
row.Cells(39).Value & "', '" & row.Cells(40).Value & "', '" & row.Cells(41).Value & "','" & row.Cells(42).Value & "', '" & row.Cells(43).Value & "', '" & row.Cells(44).Value & "','" & row.Cells(45).Value & "', '" & row.Cells(46).Value & "', '" & row.Cells(47).Value & "','" & row.Cells(48).Value & "', '" & row.Cells(49).Value & "', '" & row.Cells(50).Value & "','" & row.Cells(51).Value & "', '" & row.Cells(52).Value & "', '" & row.Cells(53).Value & "','" & row.Cells(54).Value & "', '" & row.Cells(55).Value & "', '" & _
row.Cells(56).Value & "','" & row.Cells(57).Value & "', '" & row.Cells(58).Value & "', '" & row.Cells(59).Value & "','" & row.Cells(60).Value & "', '" & row.Cells(61).Value & "')"
Break it into pieces:
SQL = "insert IGNORE into nhc"
SQL = SQL & " (date,dealer_code,name,area_executive,"
etc...
and pay attention to spaces in the right place. I like to make sure each line starts with a space (as shown above) so it's easy to tell if you are missing one.
Max String Size in VBA
Also consider using parameters instead for something like this so you dont need a string that is a million miles long.
There are no string literals in the VBA. You need to end each line by closing off the quotes, an ampersand to concatenate the next line and an underscore as a line continuation " & _.
Using WorksheetFunction.TextJoin() will make your life a lot easier.
Sql = "insert IGNORE into nhc (date" & _
"dealer_code, name, area_executive, address1, address2, address3, " & _
"area_territory_id, area_territory_name, micro_market_id, micro_market_name, town, postcode, " & _
"state, area_name, distributor, remark, may_2020_ga, may_2020_awtu10, " & _
"may_2020_sellin, may_2020_awmi10, jun_2020_ga, jun_2020_awtu10, jun_2020_sellin, jun_2020_awmi10, " & _
"jul_2020_ga, jul_2020_awtu10, jul_2020_sellin, jul_2020_awmi10, aug_2020_ga, aug_2020_awtu10, " & _
"aug_2020_sellin, aug_2020_awmi10, dealer_class , may_2020_projected_dealer_class, jun_2020_projected_dealer_class, jul_2020_projected_dealer_class, " & _
"aug_2020_projected_dealer_class, current_clas_awtu_target, current_class_sellin_target, current_class_awmi_target, dealer_status, disc_date, " & _
"ambitious_dealer?_y/n, shopfront_signage, may_2020_mnp_awtu10, jun_2020_mnp_awtu10, jul_2020_mnp_awtu10, aug_2020_mnp_awtu10, " & _
"may_2020_ereload_sellin, jun_2020_ereload_sellin, jul_2020_ereload_sellin, aug_2020_ereload_sellin, may_2020_hero_sell_through, jun_2020_hero_sell_through, " & _
"jul_2020_hero_sell_through, aug_2020_hero_sell_through, may_2020_ga_with_ocr, jun_2020_ga_with_ocr, jul_2020_ga_with_ocr, aug_2020_ga_with_ocr, " & _
"Error 2042) VALUES" & _
"('" & WorksheetFunction.TextJoin("', '", False, Row.Resize(1, 60)) & ")"
I'm not sure if there is any documentation to support my answer specifically but per the Microsoft Documentation for the String Data Type:
You must enclose a String literal within quotation marks (" ").
Your continuations weren't working because they were within your string until you start adding the row.Cells(x).Value bits.
For the continuaton to be correct, per MS documentation for Visual Basic How To: Break and Combine Statements:
Use the line-continuation character, which is an underscore (_), at the point at which you want the line to break. The underscore must be immediately preceded by a space and immediately followed by a line terminator (carriage return) or (starting with version 16.0) a comment followed by a carriage return.
Note: This documentation is for Visual Basic. To make it relevant to Visual Basic for Applications you must concatenate (&) the string with the line-continuation character if you are continuing the string literal onto a new line; e.g:
"This string will be continued" & _
" on this line."
To use the line continuation correctly with your provided string, it should look like this:
Sql = "insert IGNORE into nhc (date,dealer_code,name,area_executive,address1,address2,address3,area_territory_id,area_territory_name,micro_market_id,micro_market_name,town,postcode,state,area_name,distributor,remark,may_2020_ga,may_2020_awtu10,may_2020_sellin,may_2020_awmi10,jun_2020_ga,jun_2020_awtu10," & _
" jun_2020_sellin,jun_2020_awmi10,jul_2020_ga,jul_2020_awtu10,jul_2020_sellin,jul_2020_awmi10,aug_2020_ga,aug_2020_awtu10,aug_2020_sellin,aug_2020_awmi10,dealer_class,may_2020_projected_dealer_class,jun_2020_projected_dealer_class,jul_2020_projected_dealer_class,aug_2020_projected_dealer_class," & _
" current_clas_awtu_target,current_class_sellin_target,current_class_awmi_target,dealer_status,disc_date,ambitious_dealer?_y/n,shopfront_signage,may_2020_mnp_awtu10,jun_2020_mnp_awtu10,jul_2020_mnp_awtu10,aug_2020_mnp_awtu10,may_2020_ereload_sellin,jun_2020_ereload_sellin,jul_2020_ereload_sellin,aug_2020_ereload_sellin,may_2020_hero_sell_through,jun_2020_hero_sell_through,jul_2020_hero_sell_through,aug_2020_hero_sell_through" & _
" ,may_2020_ga_with_ocr,jun_2020_ga_with_ocr,jul_2020_ga_with_ocr,aug_2020_ga_with_ocr)values" & _
" ('" & row.Cells(1).Value & "', '" & row.Cells(2).Value & "','" & row.Cells(3).Value & "', '" & row.Cells(4).Value & "', '" & row.Cells(5).Value & "','" & row.Cells(6).Value & "', '" & row.Cells(7).Value & "', '" & row.Cells(8).Value & "','" & row.Cells(9).Value & "', '" & row.Cells(10).Value & "', '" & row.Cells(11).Value & "','" & _
row.Cells(12).Value & "', '" & row.Cells(13).Value & "', '" & row.Cells(14).Value & "','" & row.Cells(15).Value & "', '" & row.Cells(16).Value & "', '" & row.Cells(17).Value & "','" & row.Cells(18).Value & "', '" & row.Cells(19).Value & "', '" & row.Cells(20).Value & "','" & row.Cells(21).Value & "', '" & row.Cells(22).Value & "', '" & row.Cells(23).Value & "','" & row.Cells(24).Value & "', '" & row.Cells(25).Value & "', '" & _
row.Cells(26).Value & "','" & row.Cells(27).Value & "', '" & row.Cells(28).Value & "', '" & row.Cells(29).Value & "','" & row.Cells(30.Value & "', '" & _
row.Cells(31).Value & "', '" & row.Cells(32).Value & "','" & row.Cells(33).Value & "', '" & row.Cells(34).Value & "', '" & row.Cells(35).Value & "','" & row.Cells(36).Value & "', '" & row.Cells(37).Value & "', '" & row.Cells(38).Value & "','" & _
row.Cells(39).Value & "', '" & row.Cells(40).Value & "', '" & row.Cells(41).Value & "','" & row.Cells(42).Value & "', '" & row.Cells(43).Value & "', '" & row.Cells(44).Value & "','" & row.Cells(45).Value & "', '" & row.Cells(46).Value & "', '" & row.Cells(47).Value & "','" & row.Cells(48).Value & "', '" & row.Cells(49).Value & "', '" & row.Cells(50).Value & "','" & row.Cells(51).Value & "', '" & row.Cells(52).Value & "', '" & row.Cells(53).Value & "','" & row.Cells(54).Value & "', '" & row.Cells(55).Value & "', '" & _
row.Cells(56).Value & "','" & row.Cells(57).Value & "', '" & row.Cells(58).Value & "', '" & row.Cells(59).Value & "','" & row.Cells(60).Value & "', '" & row.Cells(61).Value & "')"
You'll notice that now each line is enclosed in quotation marks, followed by a space and underscore. With the formatting on SO, the underscore is now white in colour showing it's not part of the string literal anymore (which is red) as it's not enclosed in the quotation marks any longer.
With saying all that, I'd agree with the answer from Brax that it'd be 'better' to break your string down into smaller pieces and concatenate the smaller chunks together and the use of parameters to help reduce string size.
I'm trying to create a vba code to write a formula in determined cell on my sheet, but it doesn't work. What's wrong ? 'Cause, i really don't see what am i doing wrong in this code.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your computer's use of a ; as the regional list separator is fouling you up. VBA is very EN-US-centric so the Range.Formula and Range.FormulaR1C1 expect a comma (,) as the function's argument list separator.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, " & """" & "DROP" & """" & ", " & """" & "REVIEW" & """" & "), " & """" & "REVIEW" & """" & "), " & """" & """" & ")"
Alternately, Range.FormulaLocal property or Range.FormulaR1C1Local property can be used with your own semi-colon as the list separator.
Range("BC" & ActiveCell.Row).FormulaLocal = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your doubling up double quotes within a quoted string is a bit verbose and xlR1C1 notation would save some steps. TEXT(,) is the same as "" in a formula; each produces the same zero-length string.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=""REVIEW"", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=""OK"", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
'.FormulaR1C1
Range("BC" & ActiveCell.Row).FormulaR1C1 = "=IF(RC54=""REVIEW"", IF(ROW(R:R)<MAX(IF(C54:C54=""OK"", C1:C1)), IF(TODAY()-RC51>=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
The xlA1 and xlR1C1 formula rewrites above each produce the following when ActiveCell is on the second row.
=IF($BB2="REVIEW", IF(ROW($BB2)<MAX(IF($BB:$BB="OK", $A:$A)), IF(TODAY()-$AY2>=3, "DROP", "REVIEW"), "REVIEW"), TEXT(,))
As I mentioned in comments above, that formula would seem to be an array formula. In that case, use the .Formula comma based rewrite but change the Range.Formula property to Range.FormulaArray.