JoeBobJenkins
New Member
- Joined
- Feb 15, 2019
- Messages
- 4
I basically copied code from a macro to sort and I get errors with a minor tweak. I'm not sure what's wrong.
Code:
Public Sub PopulateXL(varArray2Write() As Variant, strTitle As String)
Dim xlappApp As Excel.Application
Dim wkbkMain As Workbook
Dim wkshtMain As WorkSheet
Dim lngRowCount As Long, lngColCount As Long
Dim lngCount1 As Integer, lngCount2 As Integer, lngCount3 As Integer
Dim strDesktopPath As String: strDesktopPath = Environ("USERPROFILE") & "\Desktop"
Dim strFilePath As String: strFilePath = strDesktopPath & "" & strTitle & ".xslx"
Set xlappApp = CreateObject("Excel.Application")
xlappApp.Visible = True
Set wkbkMain = xlappApp.Workbooks.Add
wkbkMain.Activate
Set wkshtMain = wkbkMain.Worksheets.Item(1)
wkshtMain.Name = "All Data"
lngRowCount = UBound(varArray2Write, 2)
lngColCount = UBound(varArray2Write)
For lngCount1 = 0 To lngColCount
For lngCount2 = 0 To lngRowCount
wkshtMain.Cells(lngCount2 + 1, lngCount1 + 1).Value = varArray2Write(lngCount1, lngCount2)
Next lngCount2
Next lngCount1
wkshtMain.Columns("A:AZ").EntireColumn.AutoFit
Dim rng2Sort As Range
Set rng2Sort = wkshtMain.Range(wkshtMain.Cells(1, 1), wkshtMain.Cells(lngRowCount + 1, lngColCount + 1))
'Set rng2Sort = wkshtMain.Range("A1:AZ100000").Select
rng2Sort.Select
rng2Sort.Activate
ActiveWorkbook.Worksheets("All Data").Sort.SortFields.Clear
'I get the error at this line
rng2Sort.Sort Key1:=Range("B:B"), _
SortOn:=xlSortOnValues, Order:=xlAscending, dtataoption:=xlSortNormal
With ActiveWorkbook.Worksheets("All Data").Sort
.SetRange rng2Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
wkbkMain.SaveAs (strFilePath)
wkbkMain.Close
End Sub
Last edited by a moderator: