BeachSoccerG
New Member
- Joined
- Mar 14, 2025
- Messages
- 4
- Office Version
- 365
- Platform
- MacOS
I have a sheet where Column A can range in size, with some blank cells within the range.
I have a table in the same sheet. The table starts at Column AN and it's range of rows as well as columns also can vary, though for my purposes right now, the number of columns in the table doesn't matter.
I have used the following to successfully find the last row for both column A as well as the table.
column_number = 1
last_row = Cells(Rows.Count, column_number).End(xlUp).Row
column_number2 = 39
last_row2 = Cells(Rows.Count, column_number2).End(xlUp).Row
' Display the value of the last non-blank cell
MsgBox "Last Row of Column A: " & last_row
MsgBox "Last Row of Column AM: " & last_row2
Next, I move on to If + Count_If, the results of which go into Column AK (which will have the same last-row2 as AM):
Range("AK2").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTIF(Table1[@[Division Name]],""""),"""",IF(COUNTIF((Cells(2, 1), Cells(last_row, 1)),Table1[@[Division Name]]),"""",""NO""))"
The non-VBA formula is =IF(COUNTIF(Table1[@[Division Name]],""),"",IF(COUNTIF($A$2:$A$353,Table1[@[Division Name]]),"","NO")) and it works great! I just can't convert it to VBA to accommodate the fact that "353" will not always be the row count.
I can successfully select ranges of variable size, for example
Range(Cells(2, 1), Cells(last_row, 1)).Select
... and even fill down using a similar variable range reference:
Range("AK2").Select
Selection.AutoFill Destination:=Range(Cells(2, 37), Cells(last_row2, 37)), Type:=xlFillDefault
On my sample sheet photos, attached, the green cells use the non-VBA formula that works great other than it being inflexible for a changing range size.
The cells with the red font (in col AK) used the faulty VBA resulting in #NAME? error.
I have a table in the same sheet. The table starts at Column AN and it's range of rows as well as columns also can vary, though for my purposes right now, the number of columns in the table doesn't matter.
I have used the following to successfully find the last row for both column A as well as the table.
column_number = 1
last_row = Cells(Rows.Count, column_number).End(xlUp).Row
column_number2 = 39
last_row2 = Cells(Rows.Count, column_number2).End(xlUp).Row
' Display the value of the last non-blank cell
MsgBox "Last Row of Column A: " & last_row
MsgBox "Last Row of Column AM: " & last_row2
Next, I move on to If + Count_If, the results of which go into Column AK (which will have the same last-row2 as AM):
Range("AK2").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTIF(Table1[@[Division Name]],""""),"""",IF(COUNTIF((Cells(2, 1), Cells(last_row, 1)),Table1[@[Division Name]]),"""",""NO""))"
The non-VBA formula is =IF(COUNTIF(Table1[@[Division Name]],""),"",IF(COUNTIF($A$2:$A$353,Table1[@[Division Name]]),"","NO")) and it works great! I just can't convert it to VBA to accommodate the fact that "353" will not always be the row count.
I can successfully select ranges of variable size, for example
Range(Cells(2, 1), Cells(last_row, 1)).Select
... and even fill down using a similar variable range reference:
Range("AK2").Select
Selection.AutoFill Destination:=Range(Cells(2, 37), Cells(last_row2, 37)), Type:=xlFillDefault
On my sample sheet photos, attached, the green cells use the non-VBA formula that works great other than it being inflexible for a changing range size.
The cells with the red font (in col AK) used the faulty VBA resulting in #NAME? error.