Less is more - consolidate 3 similar macros.

Eviltauro

New Member
Joined
May 24, 2014
Messages
12
Any tips for streamlining the following 3 macros into 1 tidy macro please: -

Sub priorforecastsOTL()
Dim current
current = Sheet5.Range("H3")
Sheet3.Select
Range("J3", current).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Selection.EntireColumn.Hidden = True
Sheet3.Range("B3").Select
End Sub


Sub priorforecastsSUPP()
Dim current
current = Sheet5.Range("I3")
Sheet1.Select
Range("M3", current).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Selection.EntireColumn.Hidden = True
Sheet1.Range("B3").Select
End Sub


Sub priorforecastsCONTJRN()
Dim current
current = Sheet5.Range("J3")
Sheet6.Select
Range("I3", current).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Selection.EntireColumn.Hidden = True
Sheet6.Range("B3").Select
End Sub

Many thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
When/how are these run?
If you can list out what they are supposed to do, and when they are supposed to run, we can probably help you consolidate them.
 
Upvote 0
When/how are these run?
If you can list out what they are supposed to do, and when they are supposed to run, we can probably help you consolidate them.

Hi Joe4

Apologies for my lack of context, end of a long day :-)

Each sub routine is being "called" from another sub.

They basically take a value from the front summary sheet5 (which is adjusting the reporting period) and using that value as the end column in another sheet which defines the range of cells to be cleared.

It then returns the selection to a neutral point in the sheet (my OCD showing).

So each of the 3 subs do the same thing but to different sheets and I am looking to tidy it up.

Regards
Mark
 
Upvote 0
Can you walk me through a simple example?
Like, what do the values in H3:I3 look like?
I am trying to recreate your scenario here, because not only can the codes can be combined, they can also be cleaned up a little (you usually do not need Selects in your code).
 
Upvote 0
See if this does what you want:
Code:
Sub priorforecasts()
    
    Dim current
    Dim rng As Range
    Dim sh() As Variant
    Dim c As Long
    Dim i As Long

    Application.ScreenUpdating = False

'   Set sheet indexes to loop through
    sh = Array(3, 1, 6)

'   Loop through columns H-J
    For c = 8 To 10
        current = Sheet5.Cells(3, c)
        Sheets(sh(8 - c)).Select
        Set rng = Range("I3", current)
        With Range(rng, rng.End(xlDown))
            .ClearContents
            .EntireColumn.Hidden = True
        End With
        Sheets(sh(8 - c)).Range("B3").Select
    Next c
    
    Application.ScreenUpdating = True
    
End Sub
If not, please let me know what values are in H3:J3, and give you an example of how sheets 6,1,3 are structured so I can try to recreate it on my side.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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