To help with debugging you can either print key values to the immediate window using Debug.Print
or to a messagebox using MsgBox
. In this case though, I am curious if you get any error messages when you attempt to run the macro. Editing your code and attempting to run it, it runs fine when the result I get from the CountIf
is larger than one, but aborts with an error when it is one or less. If the table you attempt to resize don't have headers, I assume the macro will run fine if CountIf
is greater than zero.
Here is the the code, sample data, and output I got when attempting to debug your code. I ran the code 3 times, and had return values of 8, 1, and 2 from the CountIf
-function. Note how I didn't get the third address for the listobject on the second run-through, this was because the code aborted when it tried to set the ListObject
to only its headers (A2:J2
).
Code
Option Explicit
Sub AdjRow()
Dim r As Range
Dim i As Long
Dim lo As ListObject
Set r = Sheet2.Range("Securities[Strategy]")
i = Application.WorksheetFunction.CountIf(r, "Test1")
i = i + 1
Set lo = Sheet1.ListObjects("Commodity")
Debug.Print i
Debug.Print r.Address
Debug.Print lo.Range.Address
lo.Resize Range("$A$2:$J$" & i)
Debug.Print lo.Range.Address
End Sub
Sheet1

Sheet2

Output to immediate window
9
$A$2:$A$10
$A$2:$J$9
$A$2:$J$9
2
$A$2:$A$10
$A$2:$J$9
3
$A$2:$A$11
$A$2:$J$9
$A$2:$J$3