Fault in code

femma

Board Regular
Joined
Jul 13, 2016
Messages
156
hi!

Can't seem to find the fault in my code. Returns an Object required -error.
I think the problem lies where it finds the sheet with the *support*-part and the sets it as the active worksheet...

Code:
Sub help()

Dim wscount As Integer
Dim ws As Worksheet

wscount = ThisWorkbook.Worksheets.Count
For i = 2 To wscount
    If Worksheet.Name Like "*Support*" Then
            Set ws = ActiveWorksheet
            Set ws1 = Sheets("Data")
            ws.[B15].Value = ws1.[E5].Value
            ws.[B21].Value = ws1.[E6].Value
            ws.[B43].Value = ws1.[E10].Value
            ws.[B44].Value = ws1.[E11].Value
            ws.[B45].Value = ws1.[E12].Value
            ws.[B47].Value = ws1.[E13].Value
            ws.[B15].Value = ws1.[E5].Value
        End If
        
Next i
            

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi, try changing that line to:

Code:
If Worksheets(i).Name Like "*Support*" Then
 
Upvote 0
You are never moving off the original sheet.

Try this:
Code:
Sub help()

Dim wscount As Integer
[COLOR=#ff0000]Dim i As Integer[/COLOR]
Dim ws As Worksheet
[COLOR=#ff0000]Dim ws1 As Worksheet[/COLOR]

wscount = ThisWorkbook.Worksheets.Count
For i = 2 To wscount
[COLOR=#ff0000]    If Sheets(i).Name Like "*Support*" Then
            Set ws = Sheets(i)[/COLOR]
            Set ws1 = Sheets("Data")
            ws.[B15].Value = ws1.[E5].Value
            ws.[B21].Value = ws1.[E6].Value
            ws.[B43].Value = ws1.[E10].Value
            ws.[B44].Value = ws1.[E11].Value
            ws.[B45].Value = ws1.[E12].Value
            ws.[B47].Value = ws1.[E13].Value
            ws.[B15].Value = ws1.[E5].Value
        End If
        
Next i
            
End Sub
 
Last edited:
Upvote 0
You need to use

Code:
Sheets(i).Name

not Worksheet.Name. Then use ActiveSheet for your ws variable.
 
Upvote 0
Hi, try changing that line to:

Code:
If Worksheets(i).Name Like "*Support*" Then
You would also need to change the next line setting the ws variable, as looping through the sheet numbers does NOT select each sheet. So the ActiveWorksheet would still be the initial sheet you were on when you started the macro. See how I did it in my code above.
 
Upvote 0
Then use ActiveSheet for your ws variable.
Nope. Won't work for the reasons I stated above.
Looping through the sheet names does NOT actually loop through the sheets!
So the ActiveSheet never changes, unless you explicitly select each sheet as you loop through them.
Just use:
Code:
Set ws = Sheets(i)
 
Upvote 0
Yeah i wasnt reading. I just saw ActiveWorksheet and that doesnt exist. Well it doesnt in version 7 anyway.
 
Upvote 0
You would also need to change ...

Yes, I didn't delve too deeply past the reported problem. I'd still recommend using Worksheets(i) versus Sheets(i) though. There might not be any sheets that aren't worksheets but I don't think that's a given.
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,404
Members
449,156
Latest member
LSchleppi

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