For Each ws within ActiveWorkbook not working

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
What's wrong with this code?
This code was working just fine until I tried doing this on each ws.

Is it the .select that's messing things up.

When I run this code it does this at the end of the worksheet:
1636373251541.png


VBA Code:
Sub UBTSignoff()
'
' UBTSignoff Macro
'
' Keyboard Shortcut: Ctrl+f
'
 Dim strFullDate As String
 Dim Ws As Worksheet
 
 strFullDate = Format(Now, "mm/dd/yy")
 

For Each Ws In Worksheets
        Range("B" & Rows.Count).End(xlUp).Offset(3).Select
        ActiveCell.FormulaR1C1 = "Agreed to cancel all."
        Range("B" & Rows.Count).End(xlUp).Offset(1).Select
        ActiveCell.FormulaR1C1 = "Completed by TCH " & strFullDate
Next


End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You're iterating the worksheets collection without activating each worksheet so the .Select method fails. Activating or selecting anything is almost never necessary, so try this ...

VBA Code:
Sub UBTSignoff()
    '
    ' UBTSignoff Macro
    '
    ' Keyboard Shortcut: Ctrl+f
    '
    Dim strFullDate As String
    Dim Ws As Worksheet
 
    strFullDate = Format(Now, "mm/dd/yy")
 

    For Each Ws In Worksheets
        Ws.Range("B" & Rows.Count).End(xlUp).Offset(3).Value = "Agreed to cancel all."
        Ws.Range("B" & Rows.Count).End(xlUp).Offset(1).Value = "Completed by TCH " & strFullDate
    Next

End Sub
 
Upvote 0
Solution
You're iterating the worksheets collection without activating each worksheet so the .Select method fails. Activating or selecting anything is almost never necessary, so try this ...

VBA Code:
Sub UBTSignoff()
    '
    ' UBTSignoff Macro
    '
    ' Keyboard Shortcut: Ctrl+f
    '
    Dim strFullDate As String
    Dim Ws As Worksheet
 
    strFullDate = Format(Now, "mm/dd/yy")
 

    For Each Ws In Worksheets
        Ws.Range("B" & Rows.Count).End(xlUp).Offset(3).Value = "Agreed to cancel all."
        Ws.Range("B" & Rows.Count).End(xlUp).Offset(1).Value = "Completed by TCH " & strFullDate
    Next

End Sub
Worked great! thanks
 
Upvote 0
You are welcome and thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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