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
 
I don't need them at all. The original T1 and T2 sheets come from an external file. As I am copying these sheets into my workbook, I only need access to the raw data. Hence all the ranges of T1 and T2 can be deleted.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Aziz

Well that's the answer - don't use them in your code.

You don't even really need to delete them, but it would probably be a good idea.

As to what to replace them with, we could probably help if you explained what ranges you are trying to reference.
 
Upvote 0
But I need the ranges of the same name on the 'Main' sheet. When I deleted all the ranges in T1 and T4 and then ran the code, it still didn't work.

I'm now getting Type mismatch when I debug

Range("CountryNames").Value
 
Upvote 0
Aziz

Can you please explain why you must have the named ranges that came with this workbook?

They don't seem to be working very well.:)

If you do need named ranges then if you can explain what they are supposed to refer to we can possibly help you create/recreate the ones that aren't working.
 
Upvote 0
On the Main sheet in MY workbook, I have a list of 22 country codes e.g.

GBR
IRL
DNK

in a column. The range is called CountryCodes.

I then obtain data from a spreadsheet (T1 and T4). On the T4 sheet, I have a row with the country codes with data underneath each code. The row upon which the country codes on T4 may change. Hence my original code essentially finds the row that contains the country codes on T4.

Having gone to all the trouble of writing the original code which did work for other data files I have changed the code to

Code:
    For iCol = 1 To Sheets("T4").UsedRange.Columns.Count
        If intCountryLine > 0 Then
            Exit For
        End If
        For x = 1 To Sheets("T4").UsedRange.Rows.Count
            If Cells(x, iCol) = "GBR" Then
                intCountryLine = x
                Exit For
            End If
        Next x
    Next iCol

which does the same job.

My frustration is that I can't see why the original code isn't working since I deleted the ranges from T1 and T4.
 
Upvote 0
Aziz

Did you make a mistake when you created the ranges?
 
Upvote 0
No I don't think so. Before I started this thread, it worked for data for Jan, Feb, Mar, Apr, May, Jul. It was the Jun data that caused the problem. When I then went back to doing the Jul again it stopped working. You can imagine how frustrating it all is. :)

The new code does the job so I guess I have to accept it. I always like to learn why something doesn't work so that I don't repeat the same mistake again.

I can post all the code, but it will be longish and I'm sure not very good code. Haha. I'm a MS Access person not a MS Excel person.
 
Upvote 0
Aziz

So how did you create the named ranges?
 
Upvote 0
Simply entered the codes on the Main sheet and selected them. I then went to the Name box and entered CountryCodes.
 
Upvote 0
Are you importing this data from the external source you mentioned earlier?
 
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