how to insert more than 1 vba at one go

Status
Not open for further replies.

Bilaal xaka

New Member
Joined
Mar 7, 2021
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
Hello everyone, I need your help please.
(1) I have 2 VBA which I was applying in all sheets in a workbook, but which now needs to be applied to only 2 sheets named "Sheet1" and "Sheet2" (as attached). I don't know how to edit the VBA from all the sheets to only those two sheets . plz help. Please find below the 2 VBA.
(2) Can I insert the 2 VBA at one go?? ( i mean; not inserting the first VBA, run it, then delete it, then same thing for the second VBA), such that I can run whichever VBA at my desired time in one-two clicks. (i dont know, via macro or something?? ) please help

VBA 1
Sub SortAllSheets()

Dim ws As Worksheet

Application.ScreenUpdating = False
For Each ws In Sheets
ws.Range("AI96") = "Sort"
ws.Range("AI97", "AI120").Formula = "=IF(J97=0,""ZZZ"",J97)"
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range("AI97:AI120"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.Sort
.SetRange Range("I96:AI120")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ws.Range("AI96", "AI120").Clear
Application.Goto ws.Range("A1"), True
Next

End Sub


VBA 2
Sub SortAllSheets()

Dim ws As Worksheet

Application.ScreenUpdating = False
For Each ws In Sheets
ws.Range("AA125") = "Sort"
ws.Range("AA126", "AA149").Formula = "=IF(J126=0,""ZZZ"",J126)"
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range("AA126:AA149"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.Sort
.SetRange Range("I125:AA149")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ws.Range("AA125", "AA149").Clear
Application.Goto ws.Range("A1"), True
Next

End Sub
 

Attachments

  • Capture.PNG
    Capture.PNG
    2.7 KB · Views: 4

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Please do not post the same question more than once. Thread closed.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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