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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,714
Members
448,294
Latest member
jmjmjmjmjmjm

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