Refine Macro code and error correction

N Prakash

Active Member
Joined
Nov 7, 2003
Messages
409
Hi,

I am getting Compilation error in the Sub Sort_Combined_Sheets() Macro code in the highlighted line. Will some one help me in correcting / refining the code Sub club Combined() and Sub Sort_Combined_Sheets() codes please. (First it has to combine and the sort left to right.

Sub Combined()

'This macro code will put all individual sheet records in different columns in a new sheet.
Dim Cnt As Long
Dim Sht As Worksheet
Dim Shts As Long

Shts = Sheets.Count
Sheets.Add after:=Sheets(Shts)
Set Sht = Sheets(Shts + 1)
Sht.Name = "Combined"
For Cnt = 1 To Shts
Sheets(Cnt).Columns(1).Copy Sht.Columns(Cnt)
Next Cnt

End Sub
---------------------------------------------------------
Sub Sort_Combined_Sheets()
'
' This Macro will sort all combined sheets in a new Work sheet Combined as required by the user
'
Dim lngLast As Long

Rows("1:1").Select
Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(R[1]C,_SRT,2,FALSE)"
Range("A1").Select
Selection.AutoFill Destination:=Range("A1:AN1"), Type:=xlFillDefault
Range("A1:AN1").Select
lngLast = Range("A" & Rows.Count).End(xlUp).Row
ActiveWorkbook.Worksheets("Combined").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Combined").Sort.SortFields.Add Key:=Range("A1:AN1") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Combined").Sort
.SetRange Range("A1:AN1002")
.Header = xlYes
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
End Sub


Thanks
Prakash
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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