Sorting column in new workbook before saving

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
150
Office Version
  1. 2013
Platform
  1. Windows
Hi All,

My code creates a new workbook which is working fine. I've added a line to sort column C in ascending order but it is not working. I'm thinking it has something to do with LRow being the sheet that has just been created?

Many thanks

VBA Code:
LRow = wbnew.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row

With wbnew
.Sheets(1).Range("A1").Value = ThisWorkbook.Sheets(1).Range("X1").Value
.Sheets(1).Range("B1").Value = ThisWorkbook.Sheets(1).Range("Y1").Value
.Sheets(1).Range("C1").Value = ThisWorkbook.Sheets(1).Range("B1").Value
.Sheets(1).Range("D1").Value = ThisWorkbook.Sheets(1).Range("A1").Value
.Sheets(1).Range("E1").Value = ThisWorkbook.Sheets(1).Range("K1").Value
.Sheets(1).Range("F1").Value = ThisWorkbook.Sheets(1).Range("G1").Value
.Sheets(1).Range("G1").Value = ThisWorkbook.Sheets(1).Range("I1").Value
.Sheets(1).Range("H1").Value = ThisWorkbook.Sheets(1).Range("T1").Value
.Sheets(1).Range("I1").Value = ThisWorkbook.Sheets(1).Range("U1").Value
.Sheets(1).Range("J1").Value = ThisWorkbook.Sheets(1).Range("L1").Value
.Sheets(1).Range("K1").Value = ThisWorkbook.Sheets(1).Range("O1").Value
.Sheets(1).Range("L1").Value = ThisWorkbook.Sheets(1).Range("V1").Value
.Sheets(1).Range("A2:L2").Resize(UBound(ArrOUT)).Value = ArrOUT
.Sheets(1).Range("A1:L1").Font.Bold = True
.Sheets(1).Columns("I:J").NumberFormat = "0"
.Sheets(1).Columns("A:L").EntireColumn.AutoFit
.Sheets(1).Columns("A:L").HorizontalAlignment = xlCenter
.Sheets(1).Name = SheetName
.Sheets(1).Range("A1:N" & LRow).Sort key1:=.Sheets(1).Range("C1:C" & LRow), _
order1:=xlAscending, Header:=xlYes
.SaveAs FileName:=ThisWorkbook.Path & "\" & SheetName & ".xlsx"
End With
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try
VBA Code:
.Sheets(1).Range("A1:N1").Sort key1:=.Sheets(1).Range("C1"), _
order1:=xlAscending, Header:=xlYes
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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