Compile Error Help

Dspace

New Member
Joined
Nov 2, 2020
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Good morning. I'm trying to run some VBA code & I keep getting a compile error that highlights "sheetName" in the 6th line & says "For Each control Variable on arrays must be Variant".
Any help would be appreciated. Thanks!

Dim targetSheets(1 To 2) As String
Dim ws As Worksheet
Dim sheetName As String
targetSheets(1) = "Sales"
targetSheets(2) = "Revenue"
For Each sheetName In targetSheets ERROR IS ON THIS LINE
Set ws = ThisWorkbook.Worksheets(sheetName)

ws.Range("A:AD").Copy
ws.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ws.Range("AG:BB").Delete Shift:=xlToLeft
ws.Rows("1:21").Delete Shift:=xlUp
ws.Range("F:AA").Delete Shift:=xlToLeft
ws.Range("$D$12:$D$5000").AutoFilter Field:=1, Criteria1:="yes"
ws.Range("D:E").EntireColumn.Hidden = True

Next sheetName
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
It means what it says. ;) You declared sheetName as String, but it has to be as Variant if you want to use it as a variable in a For Each statement.
 
Upvote 0
Sometimes you just need a second set of eyes. Thanks RoryA!
Fairly new to VBA & don't use it enough to stay 'fresh' with it.
 
Upvote 0

Forum statistics

Threads
1,215,741
Messages
6,126,608
Members
449,321
Latest member
syzer

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