Selecting workbook then worksheet without using sheet name

ajt8888

New Member
Joined
Aug 23, 2010
Messages
35
Office Version
  1. 2019
Platform
  1. Windows
I am trying to use a macro across a number of worksheets in a workbook I am not very good at writing VBA so just record it but from some reason the macro just references the one sheet how do I change it so that it doesnt just look for the same sheet all the time? Also the number of rows varies in each sheet how do I get it to select however many rows there are?

Columns("B:B").Select
ActiveWorkbook.Worksheets("F1711412").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("F1711412").Sort.SortFields.Add2 Key:=Range( _
"B2:B91"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("F1711412").Sort
.SetRange Range("B1:B91")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A50")
Range("A2:A50").Select
Range("B1").Select
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Run the following on the sheet you need.

Code:
Sub test()
  Dim lr As Long
  lr = Range("B" & Rows.Count).End(xlUp).Row
  Range("B1:B" & lr).Sort key1:=Range("B1"), order1:=xlAscending, Header:=xlYes
  Range("A2").AutoFill Destination:=Range("A2:A" & lr)
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,762
Messages
6,126,744
Members
449,335
Latest member
Tanne

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