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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Gerald Higgins

Well-known Member
Joined
Mar 26, 2007
Messages
9,194
I'm not a code expert, but could it be something as simple as replacing
"dtataoption"
with
"dataoption"
?
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,016
Messages
5,834,956
Members
430,330
Latest member
drAli77

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
Top