Shorter VBA version to do the same thing?

mrexcelhelp123

New Member
Joined
Mar 10, 2010
Messages
38
HI, I have the code I'm using now which works but was wondering if someone can help me condense it to do the same thing: THANKS!

Code:
Sub Modify_Instr_Tab()
 
Dim ws As Worksheet
 
'Assigns a due date to cell D10
Worksheets("Instructions").Range("D10").Select
ActiveCell.Formula = "5/28/2010"
 
'Assigns another due date to cell E10 if applicable
Worksheets("Instructions").Range("E10").Select
ActiveCell.Value = "NO UPDATE REQUIRED"
 
'Shades range A10:E10
Range("A10:E10").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
.PatternTintAndShade = 0
End With
 
For Each ws In Sheets(Array("Instructions"))
'Unshade filling for range A9: E9
With ws.Range("A9:E9")
.Interior.Pattern = xlNone
.Interior.TintAndShade = 0
.Interior.PatternTintAndShade = 0
 
'Clear contents for range D9:E9
ws.Range("D9:E9").ClearContents 
End With
Next ws
End Sub
 
Last edited by a moderator:
My code should work in all files, as long as that sheet is part of the active workbook.

And solve that by further qualifying the sheet listed in your With statement to include the workbook. I would recommend not keeping that as an assumption. :cool:
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi Zack,

post #9 in this topic justifies it doing this way... ;)
 
Upvote 0
Hi Wigi,

So the only way to update the Instruction tab on all 40 workbooks containing the same tab is to activate a workbook one at a time and run the macro. In otherwords, run the macro 40 times?
 
Upvote 0
Try this.
Code:
Option Explicit
 
Sub Modify_Instr_Tab()
Dim wb As Workbook
Dim wsInstr As Worksheet
 
    For Each wb In Workbooks

        With wb

            Set wsInstr = .Worksheets("Instructions")

            With wsInstr

                'Assigns due dates
                .Range("D10:E10") = Array("5/28/2010", "NO UPDATE REQUIRED")
 
                'Shades range A10:E10
                With .Range("A10:E10").Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .Color = 49407
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
                
                'Unshade filling for range A9:E9
                With .Range("A9:E9").Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
                
                .Range("D9:E9") = ""
            End With

        End With

    Next wb
    
End Sub
 
Upvote 0
Hi Zack,

post #9 in this topic justifies it doing this way... ;)

Uh, actually, no it doesn't. ;) Using the "assumption" method isn't justified just because you want it to work in the active workbook. If you want it to work in the active workbook, I still recommend you use that object "ActiveWorkbook", and not just leaving it off the child object (i.e. worksheet, et al). Explicitly defining the object(s) is always a good idea.

Norie has shown an example of looping through all workbooks.
 
Upvote 0

Forum statistics

Threads
1,214,797
Messages
6,121,629
Members
449,041
Latest member
Postman24

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