Error "438" with code essentially copied from a macro

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:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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