Compile error: Variable not defined

mightymorgs

New Member
Joined
Dec 22, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I'm using a code that was working perfectly. A few weeks went by before I had to use it again and now I'm getting the subject error. The error flags when it gets to line 12 at the "ws.Control.ListObjects..." It seems that the code isn't finding the worksheet named "Control," even though that worksheet is clearly labeled within my workbook. Any suggestions on how to correct it?

VBA Code:
Sub Delete()
Dim ws As Worksheet
Dim lastrow As Long
Dim i As Long
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
        For Each ws In ThisWorkbook.Worksheets
            If TryMatch(Lookup:=ws.Name, _
                Lookin:=wsControl.ListObjects("tblTarget").DataBodyRange) Then
                ws.Range("A:A,B:B,C:C,D:D,E:E,M:M,N:N,Q:Q,R:R").Delete
            End If
            If TryMatch(Lookup:=ws.Name, _
                Lookin:=wsControl.ListObjects("tblTarget").DataBodyRange) Then
                lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
                    For i = lastrow To 1 Step -1
                        If TryMatch(Lookup:=ws.Cells(i, "A").Interior.Color, _
                        Lookin:=wsControl.ListObjects("tblColours").ListColumns(2).DataBodyRange) Then
                        ws.Rows(i).Delete
                        End If
                    Next i
            End If
            If TryMatch(Lookup:=ws.Name, _
                Lookin:=wsControl.ListObjects("tblTarget").DataBodyRange) Then
                ws.Select
                ws.Rows("1:1").Select
                    Application.CutCopyMode = False
                    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            End If
            If TryMatch(Lookup:=ws.Name, _
                Lookin:=wsControl.ListObjects("tblTarget").DataBodyRange) Then
                ws.Range("A1:J1").Value = Array("EE #", "mfg", "Serial #", "Initial Status", "Final Status", "Cost", "Description", "CSN", "CSN Description", "Category")
            End If
        Next ws
    With Application
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
In the code you posted, all the instances seem to show:
"wsControl.ListObjects"
instead of
"ws.Control.ListObjects"
(note the missing period afer "ws")
 
Upvote 0
In the code you posted, all the instances seem to show:
"wsControl.ListObjects"
instead of
"ws.Control.ListObjects"
(note the missing period afer "ws")
I should have mentioned that I tried that. When I add the period in for "ws.Control.ListObjects" I get a Compile error: Method or data member not found and it highlights "Control." But I clearly have a worksheet titled "Control" in my workbook.
 

Attachments

  • Control.PNG
    Control.PNG
    14.3 KB · Views: 8
Upvote 0
You code is looping through the worksheets in it, so you shouldn't have to reference any sheet by name. "ws" is your worksheet reference.
Your even declared it as such:
VBA Code:
Dim ws As Worksheet

I thought your code was trying to loop through the Controls on the worksheet.
 
Upvote 0
But if you're trying to reference your worksheet named Control, try . . .

VBA Code:
Worksheets("Control").ListObjects
 
Upvote 0
Yes, that's exactly what I was trying to do. However, when I change it from wsControl / ws.Control to Worksheets("Control") I get a different compile error.

Compile error: Member already exists in an object module from which this object module derives.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,841
Members
449,051
Latest member
excelquestion515

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