Sort Columns low to high but im told cells are merged

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Hi,

I have two columns A & B of which i need each sorted separatley from low to high numbers.
This worked fine.
Rich (BB code):
Private Sub SortFrameNumbers_Click()
Range("A2:A150").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo
Range("B2:B50").Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlNo
End Sub
It sorted column A from 1-100 perfect then it sorted column B 1-22 perfect.

So i then added it to my code which populates these two columns but i then get a RTE 1004 please see screenshot.

This is the code i added it to.

Rich (BB code):
Private Sub GoldWingCountry_Click()
    Dim Cell As Range
    Dim wsMC As Worksheet
    Dim wsCountry As Worksheet
    
    Set wsMC = Sheets("MCLIST")
    Set wsCountry = Sheets("COUNTRYLIST")
    
    With Sheets("COUNTRYLIST")
        .Range("A2:B1000").ClearContents
    End With
    
    wsCountry.Range("A1") = "GOLD WING UK"
    wsCountry.Range("B1") = "GOLD WING USA"
    
    With wsMC
        For Each Cell In .Range("D8:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
            If Cell.Value = "GOLD WING UK" Then
                wsCountry.Range("A" & Rows.Count).End(xlUp).Offset(1) = Cell.Offset(, -2)
            ElseIf Cell.Value = "GOLD WING USA" Then
               wsCountry.Range("B" & Rows.Count).End(xlUp).Offset(1) = Cell.Offset(, -2)
            End If
        Next Cell
    End With
    Sheets("COUNTRYLIST").Select
    Range("A2:A150").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo
    Range("B2:B50").Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlNo
End Sub

I run the code & columns A & B are populated.

The code sorts column A2 & down the page from 1-100 etc & at this point column B is untouched.
Once done the code sorts column B2 & down the page from 1 -50 etc & at this point column A is untouched.

Please advise as none of the cells are merged
 

Attachments

  • EaseUS_2023_10_10_19_03_17.jpg
    EaseUS_2023_10_10_19_03_17.jpg
    13.5 KB · Views: 7
Can you upload a copy of your workbook to a free file hosting site like www.box.com or www.dropbox.com, mark the file file for sharing and paste the link it provides in the thread.

Please sanitize any sensitive data before uploading (you can delete any of the sheets other than "COUNTRYLIST")
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Afraid that I won't download from that site as it is forcing me to allow some cookies that I don't want to allow.
Hopefully someone else will be willing to look at it for you.
 
Upvote 0
Can you see if you have the same issue if you change the code to
VBA Code:
    With Sheets("COUNTRYLIST")
        .Range("A2:A150").Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlNo
        .Range("B2:B50").Sort Key1:=.Range("B2"), Order1:=xlAscending, Header:=xlNo
    End With
 
Upvote 0
Solution
I think when you used
VBA Code:
Sheets("COUNTRYLIST").Select
It wasn't making "COUNTRYLIST" active and so "MCLIST" was still Active and the code was acting on that which does have merged cells.

Probably changing
VBA Code:
Sheets("COUNTRYLIST").Select
to
VBA Code:
Sheets("COUNTRYLIST").Activate
would also work but I don't like using either Select or Activate unless I have to.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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