Error code 424

grapevine

Board Regular
Joined
May 23, 2007
Messages
208
In the code below, I need the macro to look through sheets in the workbook for sheet names that contain certain text. In this example main and ST (the full workbook name is much longer) when a sheet is found I need to copy a sheet from another workbook to insert next to it. I have managed to get excel to count the sheets and I believe I have got excel to work through the sheets, but I have come across the problem of now getting the macro to identify the sheet name against the variable that I used to work through the sheets (x). I am sure I am doing something very stupid but cannot work how to make it then look at the name of the sheet. My code is as follows.

Any guidance on allowing me to move forward would be very welcome.
Kind regards

Sub JNinternalOrder()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'This coding ensures that the file has been inserted and positioned in the correct place
'The relevant Jane Stilton file needs to be run and then copied and pasted into the Gabor file
'in order for this macro to run.
Dim response As String
response = MsgBox("Have you copied the address list to page 1 of the JN split macro and named the sheet with the file name eg: JN00012?", vbQuestion + vbYesNo)
If response = vbNo Then
MsgBox ("Please copy the file over and then restart the macro")
Exit Sub
End If

Dim x, totalsheets
x = 1
totalsheets = ActiveWorkbook.Sheets.Count

Do Until x = totalsheets

If x.worksheetName Like "*Main*ST*" Then (runtime error424 occuring here so my x does not mean anything to the macro)
Call copysheet
End If
Loop
End Sub

Sub copysheet() (not yet tested as I could not get the top bit working)
'Dim oBook As Workbook

Dim wb As Workbook, totalsheets, sh1 As Workbook

totalsheets = ActiveWorkbook.Sheets.Count
Set sh1 = ThisWorkbook
Set wb = Workbooks("lookupfiles.xls").Worksheets("jewio").Activate
Worksheets("jewio").Copy Before:=sh1(x)

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try

Code:
If Worksheets(x).Name Like "*Main*ST*" Then
    Call copysheet
    x = x + 1
    End If
Loop
 
Upvote 0
Thank you so much, I knew I had to be doing something very silly and yes I was. Should have spotted this, but had gone round and round different options and was going nowhere fast!
Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

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