Sort Variable Number of Rows Alphabetically -Copy Formula to Last Row -Last Row Issues

Rose401k

New Member
Joined
Aug 14, 2018
Messages
8
Hi Everyone,

In a formatting step of a larger macro, I am trying to select a variable number of rows beginning with cell C9:J, and then sort alphabetically. Below is what I have written, but it doesn't seem to be working so far (no errors given though).

Dim LRowC As Long
LRowC = Cells(Rows.Count, "C").End(xlUp).Row

Range("C9:J" & LRowC).Sort Key1:=Range("C9:C" & LRowC), _
Order1:=xlDescending, Header:=xlNo

Additionally I am trying to use a simliar process to copy vlookup formulas down column D next to all non-blank cells in column C. This is only working until about 5 rows down and I'm just stumped. Obviously I have something wrong in my understanding of the copy to last row coding here and I would really appreciate any corrected formulas/advice. Thanks!

Dim LRowD As Long
LRowD = Cells(Rows.Count, 1).End(xlUp).Row

ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-1],'MATC Positions'!R2C3:R4000C12,3,FALSE)"
Range("D9").Select
Selection.AutoFill Destination:=Range("D9:D" & LRowD)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I believe that you only need the first cell in the column you want to sort by, so try this:
Code:
[COLOR=#333333]Range("C9:J" & LRowC).Sort Key1:=Range([/COLOR][COLOR=#ff0000][B]"C9"[/B][/COLOR][COLOR=#333333]), _[/COLOR]
[COLOR=#333333]Order1:=xlDescending, Header:=xlNo[/COLOR]

For your second part:
LRowD = Cells(Rows.Count, 1).End(xlUp).Row
This is actually finding the last row in column A. It is important to understand the format of Cells. It is Cells(row,column).
The column reference can be the letter or the number of the column. Column D would then be "D" or 4, i.e.
Code:
[COLOR=#333333]LRowD = Cells(Rows.Count, 4).End(xlUp).Row[/COLOR]
or
Code:
[COLOR=#333333]LRowD = Cells(Rows.Count, "D").End(xlUp).Row[/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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