Sorting alphabetically in column "n" and keeping the rows intact

skylarjung

New Member
Joined
Sep 3, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I am trying to use VBA to sort Column 1 Alphabetically while also keeping the rows intact in column b (meaning if a row in column 1 moves its corresponding row in column b will move with it).
I am trying to avoid using the sort function in excel because I want this to be integrated later into an already existing code.
Ive tried to look around for already existing sort code so if you have any recommended resources please send them my way.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
How many columns do you have?
Also do you have a header row & if so, what row is it?
 
Upvote 0
How many columns do you have?
Also do you have a header row & if so, what row is it?
The amount of columns will be changing as more information gets added, so if possible the whole column A and the header row it row 1.
 
Upvote 0
Ok, how about
VBA Code:
Sub skylarjung()
   Dim UsdCols As Long
   
   UsdCols = Cells(1, Columns.Count).End(xlToLeft).Column
   With Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(UsdCols)
      .Sort Range("A1"), xlAscending, , , , , , xlYes
   End With
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub skylarjung()
   Dim UsdCols As Long
  
   UsdCols = Cells(1, Columns.Count).End(xlToLeft).Column
   With Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(UsdCols)
      .Sort Range("A1"), xlAscending, , , , , , xlYes
   End With
End Sub
how would I go about specifying the sheet if there were many sheets in the workbook?
 
Upvote 0
That will work on the active sheet. Do you want it to work on one sheet only?
 
Upvote 0
That will work on the active sheet. Do you want it to work on one sheet only?
The code worked well! Thank you! I was just unsure if it needed to have a sheet specified. I dont want it to sort on other sheets. Looking at it it seems like this only occurred on the sheet that was open.
 
Upvote 0
What is the name of the sheet you want to sort?
 
Upvote 0
Ok, how about
VBA Code:
Sub skylarjung()
   Dim UsdCols As Long
   
   With Sheets("Sheet1")
      UsdCols = .Cells(1, Columns.Count).End(xlToLeft).Column
      With .Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(UsdCols)
         .Sort .Range("A1"), xlAscending, , , , , , xlYes
      End With
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,925
Members
449,094
Latest member
teemeren

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