Method range of object global failed

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have the following code which I use with sheet T4 copied in from different monthly files. The code works fine except when I use T4 sheet for the June data it doesn't. The code simply tries to find which row the country codes begin. The country codes are

GBR
DEU

etc.

I get the error 1004 (Method range of object global failed if I comment out line On Error Resume Next.

Code:
    Sheets("T4").Select
    
    For iCol = 1 To ActiveSheet.UsedRange.Columns.Count
        If intCountryLine > 0 Then
            Exit For
        End If
        For x = 1 To ActiveSheet.UsedRange.Rows.Count
            strCountryCode = Cells(x, iCol)
            On Error Resume Next
            varResult = Application.WorksheetFunction.Match(strCountryCode, Range("CountryCodes"), 0)
            If Err = 0 Then
                intCountryLine = x
                y = iCol
                Exit For
            End If
            On Error GoTo 0
        Next x
    Next iCol
 
If you mean the country codes on the Main sheet, No. These country codes are fixed.
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Well something is happening to you named ranges and what I'm trying to suggest is that you use code to create them.
Code:
Dim LastRow As Long
 
LastRow = Worksheets("Main").Range("A" & Rows.Count).End(xlUp).Row
 
Worksheets("Main").Range("A2"& LastRow).Name = "CountryCodes"
Change columns/rows as needed.
 
Upvote 0
Norie, is the last line of code right? Should there be a semi-colon?
 
Upvote 0
Why not try adding one?

Perhaps another A as well.

PS Why are you doing this in Excel?

Couldn't you import the data to Access and set up a UNION query to normalize the data?

If you do need the results in Excel export the query.
 
Upvote 0
I'm not using Access as it's not intalled on my machine.
I'll try looking at the code tomorrow. Thank you for your help today.
 
Upvote 0
Changed last line to

Code:
Worksheets("Main").Range("A2:A" & LastRow).Name = "CountryCodes"

and this made my original code work. GREAT.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

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