Newbie VBA coder struggling with a Run-time error '1004'

lvizcarra

New Member
Joined
Dec 31, 2018
Messages
8
I have a fairly simple macro built that is supposed to delete row 2, bold format font in row 1, auto fit column widths, and sort by column. The number of rows in the worksheet will vary, but the number of columns are the same every time. I've done a lot of searching in forums for the 1004 error, but nothing I try seems to work. Here is my code:

Code:
    Rows("2:2").Delete Shift:=xlUp
    Rows("1:1").Font.Bold = True
    Columns("A:A").EntireColumn.AutoFit
    Columns("B:B").EntireColumn.AutoFit
    Columns("C:C").EntireColumn.AutoFit
    Columns("D:D").EntireColumn.AutoFit
 
    With ActiveSheet.Sort
        .SortFields.Add Key:=ActiveSheet.Range("A1"), Order:=xlAscending
        .SortFields.Add Key:=ActiveSheet.Range("B1"), Order:=xlAscending
        .SortFields.Add Key:=ActiveSheet.Range("C1"), Order:=xlAscending
        .SortFields.Add Key:=ActiveSheet.Range("F1"), Order:=xlAscending
        .SetRange Range("A1").CurrentRegion
        .Header = xlYes
        .Apply
    End With

Thanks in advance!
 
Yes, I stepped through it using F8 and there are no errors until I get to .Apply. That said, I have the worksheet open next to the VBE window and nothing happens to the worksheet at each step (i.e., the code doesn't result in any changes to the worksheet). In regards to special characters, the worksheet does show forward slashes and hyphens (/-). I will upload a copy of the sheet to dropbox and reply with the link.

Sheet not protected is it ?
Have you stepped through the code using F8, to see which line gives the error ?
If there are no special characters in any of the columns, I'd suggest you upload the sheet to dropbox, so we can take a look !
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
No merged cells. The exact error message is:

Run-time error '1004':

The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank.


Do you have any merged cells?
Also what is the exact error message?
 
Upvote 0
Using both routines works, but the worksheet is only sorted by column A. If that's the end result, I can work with that, but was hoping I could sort by columns A, B, C, F.

See if this accomplishes your goal :

Code:
Option Explicit


Sub DynamicRange()


ActiveSheet.UsedRange.Sort Key1:=Columns("A"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
  
End Sub


Sub foo()
Rows("2:2").Delete Shift:=xlUp
    Rows("1:1").Font.Bold = True
    Columns("A:A").EntireColumn.AutoFit
    Columns("B:B").EntireColumn.AutoFit
    Columns("C:C").EntireColumn.AutoFit
    Columns("D:D").EntireColumn.AutoFit
 
    DynamicRange
    
End Sub
 
Last edited:
Upvote 0
.
You can try this adjustment. May not be the prettiest but it does work.

Code:
Sub DynamicRange()


With Sheet1
    Range("A:A").Sort Key1:=Columns("A"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With
      
With Sheet1
    Range("B:B").Sort Key1:=Columns("B"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With
      
With Sheet1
    Range("C:C").Sort Key1:=Columns("C"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With
      
With Sheet1
    Range("F:F").Sort Key1:=Columns("F"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With


End Sub


Sub foo()
Rows("2:2").Delete Shift:=xlUp
    Rows("1:1").Font.Bold = True
    Columns("A:A").EntireColumn.AutoFit
    Columns("B:B").EntireColumn.AutoFit
    Columns("C:C").EntireColumn.AutoFit
    Columns("D:D").EntireColumn.AutoFit
 
    DynamicRange
    
End Sub
 
Upvote 0
Success! It works. Thank you, Logit. And thanks to everyone else for your input. Happy New Year everyone.

.
You can try this adjustment. May not be the prettiest but it does work.

Code:
Sub DynamicRange()


With Sheet1
    Range("A:A").Sort Key1:=Columns("A"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With
      
With Sheet1
    Range("B:B").Sort Key1:=Columns("B"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With
      
With Sheet1
    Range("C:C").Sort Key1:=Columns("C"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With
      
With Sheet1
    Range("F:F").Sort Key1:=Columns("F"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With


End Sub


Sub foo()
Rows("2:2").Delete Shift:=xlUp
    Rows("1:1").Font.Bold = True
    Columns("A:A").EntireColumn.AutoFit
    Columns("B:B").EntireColumn.AutoFit
    Columns("C:C").EntireColumn.AutoFit
    Columns("D:D").EntireColumn.AutoFit
 
    DynamicRange
    
End Sub
 
Upvote 0
Should there not be a lot of periods/full stops to link the ranges/columns to the With statements in the DynamicRange sub :confused:

Code:
Sub DynamicRange()


With Sheet1
    [COLOR="#FF0000"].[/COLOR]Range("A:A").Sort Key1:=[COLOR="#FF0000"].[/COLOR]Columns("A"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With
      
With Sheet1
    [COLOR="#FF0000"].[/COLOR]Range("B:B").Sort Key1:=[COLOR="#FF0000"].[/COLOR]Columns("B"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With
      
With Sheet1
    [COLOR="#FF0000"].[/COLOR]Range("C:C").Sort Key1:=[COLOR="#FF0000"].[/COLOR]Columns("C"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With
      
With Sheet1
    [COLOR="#FF0000"].[/COLOR]Range("F:F").Sort Key1:=[COLOR="#FF0000"].[/COLOR]Columns("F"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With


End Sub
or
Code:
Sub DynamicRange()


With Sheet1
    [COLOR="#FF0000"].[/COLOR]Range("A:A").Sort Key1:=[COLOR="#FF0000"].[/COLOR]Columns("A"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom

    [COLOR="#FF0000"].[/COLOR]Range("B:B").Sort Key1:=[COLOR="#FF0000"].[/COLOR]Columns("B"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom

    [COLOR="#FF0000"].[/COLOR]Range("C:C").Sort Key1:=[COLOR="#FF0000"].[/COLOR]Columns("C"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom

    [COLOR="#FF0000"].[/COLOR]Range("F:F").Sort Key1:=[COLOR="#FF0000"].[/COLOR]Columns("F"), Order1:=xlAscending, _
      Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,576
Members
449,173
Latest member
Kon123

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