Running VBA Code on two worksheets simultaneously

Wamhoi

New Member
Joined
Mar 4, 2011
Messages
48
Hi All, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
I wanted to know if it is possible to run a VBA simultaneously on two worksheets (in order to increase speed and reduce coding) <o:p></o:p>
<o:p></o:p>
For example, I want the following code to run on worksheet A & B<o:p></o:p>
<o:p></o:p>
Sheets("A").Select<o:p></o:p>
'Weeknum
Range("AE2").Formula = "=Int(Day(AD2) / 7 + 0.99)"
Range("AE2").AutoFill Range("AE2").Resize(Lastline - 1)
Range("AE1") = "WeekNum"
Range("AE:AE").Value = Range("AE:AE").Value<o:p></o:p>

<o:p></o:p>
Sheets("B").Select<o:p></o:p>
'Weeknum
Range("AE2").Formula = "=Int(Day(AD2) / 7 + 0.99)"
Range("AE2").AutoFill Range("AE2").Resize(Lastline - 1)
Range("AE1") = "WeekNum"
Range("AE:AE").Value = Range("AE:AE").Value<o:p></o:p>

<o:p></o:p>
Can I combine these codes so it runs without jumping from one sheet to the next? <o:p></o:p>
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try like this

Code:
Dim ws As Worksheet
For Each ws In Worksheets(Array("A", "B"))
    With ws
        .Range("AE2").Formula = "=Int(Day(AD2) / 7 + 0.99)"
        .Range("AE2").AutoFill .Range("AE2").Resize(Lastline - 1)
        .Range("AE1") = "WeekNum"
        .Range("AE:AE").Value = .Range("AE:AE").Value
    End With
Next ws
 

Wamhoi

New Member
Joined
Mar 4, 2011
Messages
48
Sorry to reopen the chat but I'm having a bit of trouble adding some additional code. I keep getting a runtime error.

Dim ws As Worksheet
For Each ws In Worksheets(Array("A", "B"))
With ws
.Range("AE2").Formula = "=Int(Day(AD2) / 7 + 0.99)"
.Range("AE2").AutoFill .Range("AE2").Resize(Lastline - 1)
.Range("AE1") = "WeekNum"
.Range("AE:AE").Value = .Range("AE:AE").Value

'This part isn't working properly
.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

.Range("A1").Select
If ActiveSheet.AutoFilterMode = True Then Selection.AutoFilter
If ActiveSheet.FilterMode = True Then ActiveSheet.ShowAllData

.Range("$A$1:$AH$18").AutoFilter Field:=33, Criteria1:="No"
.Rows("2:2").Select 'delete from row 2 to lastline
.Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
.Range("A1").AutoFilter

End With
Next ws
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Dim ws As Worksheet, LR As Long
For Each ws In Worksheets(Array("A", "B"))
    With ws
        .Range("AE2").Formula = "=Int(Day(AD2) / 7 + 0.99)"
        .Range("AE2").AutoFill .Range("AE2").Resize(Lastline - 1)
        .Range("AE1") = "WeekNum"
        .Range("AE:AE").Value = .Range("AE:AE").Value
        
        'This part isn't working properly
        .Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        .Range("A1").Select
        If .AutoFilterMode = True Then Range("A1").AutoFilter
        If .FilterMode = True Then .ShowAllData
        
        .Range("$A$1:$AH$18").AutoFilter Field:=33, Criteria1:="No"
        LR = .Range("A2").End(xlDown).Row
        .Range("A2:A" & LR).EntireRow.Delete
        .Range("A1").AutoFilter
    End With
Next ws
 

Forum statistics

Threads
1,176,382
Messages
5,902,772
Members
434,995
Latest member
excelfarmer76

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
Top