Hi,
I just started VBA this year and am struggling to troubleshoot...hoping you can help.
I have some code I upgraded today which filters a table and copies the first column to another sheet.
It uses a dynamic named range as the header to find the right column to filter (new today).
It uses a dynamic named range to define the table to filer (new today).
Both of these are to allow for insertion/deletion of columns over time.
It also checks the second sheet for the number of entries and adds or deletes rows because there is additional data below the copy/paste dataset. (New today).
For some reason, I am now getting "Compile Error: Variable not defined" and it goes to "x1Values" on the "Set Range2 row". Option explicit is enabled and all variables are defined ahead of time.
Any suggestions on why I'm getting this error, or how to troubleshoot?
Google was hopeless.....searched for an hour.
Thanks in advance!
Oh, there is more code above and below this section...it all works perfectly when I comment out this section....not sure its relevant or not.....
I just started VBA this year and am struggling to troubleshoot...hoping you can help.
I have some code I upgraded today which filters a table and copies the first column to another sheet.
It uses a dynamic named range as the header to find the right column to filter (new today).
It uses a dynamic named range to define the table to filer (new today).
Both of these are to allow for insertion/deletion of columns over time.
It also checks the second sheet for the number of entries and adds or deletes rows because there is additional data below the copy/paste dataset. (New today).
For some reason, I am now getting "Compile Error: Variable not defined" and it goes to "x1Values" on the "Set Range2 row". Option explicit is enabled and all variables are defined ahead of time.
Any suggestions on why I'm getting this error, or how to troubleshoot?
Google was hopeless.....searched for an hour.
Thanks in advance!
Oh, there is more code above and below this section...it all works perfectly when I comment out this section....not sure its relevant or not.....
Code:
Dim Col_num1 As Integer
Dim Col_num2 As Integer
Dim Range1 As Range
Dim Range2 As Range
Dim Weber_SKU_Count As Integer
Dim IE_Data_SKU_Count As Integer
Dim counter As Integer
Sheets("IE_Download").Select
With ActiveSheet 'Checks if sheet filters are all cleared. If yes, then do nothing. Prevents error from popping up.
If .FilterMode Then
.ShowAllData
End If
End With
Set Range1 = ActiveSheet.Range("datamatch").Find("Category", , xlValues, xlWhole)
Set Range2 = ActiveSheet.Range("datamatch").Find("Slicing Hall", , x1Values, x1Whole)
Col_num1 = Range1.Column
Col_num2 = Range2.Column
'datamatch and dataall are dynamic named ranges which are configured in the name manager.
'datamatch = the header in the IE_download.
'dataall = the header and the data, matched to the width of datamatch.
'Filter to correct list
ActiveSheet.Range("dataall").AutoFilter Field:=Col_num2, Criteria1:=Array( _
"10", "11", "12", "13", "4", "5", "6", "7A", "8", "9", "="), Operator:=xlFilterValues
ActiveSheet.Range("dataall").AutoFilter Field:=Col_num1, Criteria1:=Array( _
"BACON", "BOLOGNA", "SLICED MEATS"), Operator:=xlFilterValues
Range("A10").Select
Range(Selection, Selection.End(xlDown)).Select
'Count number of skus filtered
Set IE_Data_SKU_Count = Selection.Count
Sheets("Weber Calculations").Select
With ActiveSheet 'Checks if sheet filters are all cleared. If yes, then do nothing. Prevents error from popping up.
If .FilterMode Then
.ShowAllData
End If
End With
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
'Count number of skus on weber calculator
Set Weber_SKU_Count = Selection.Count
'Insert or delete number of rows needed so overlap does not occur. - Maintains existing blank rows between regular skus and PDP skus.
If IE_Data_SKU_Count > Weber_SKU_Count Then
For counter = 1 To IE_Data_SKU_Count - Weber_SKU_Count
Rows("Weber_SKU_Count+4").Select
Selection.Insert Shift:=xlDown
Next i
If IE_Data_SKU_Count < Weber_SKU_Count Then
For counter = 1 To Weber_SKU_Count - IE_Data_SKU_Count
Rows("Weber_SKU_Count+4").Select
Selection.Delete Shift:=xlUp
Next i
'Clear old sku list on weber tab to ensure eliminating duplicate skus at bottom of list.
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
'Copy and paste from IE_download to Weber
Sheets("IE_Download").Select
Range("A10").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Weber Calculations").Select
Range("A2").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Last edited: