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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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