+ Reply to Thread
Results 1 to 6 of 6

VBA Method 'Range' of object '_Global' failed error

Hybrid View

JKCincy VBA Method 'Range' of object... 12-21-2009, 04:26 PM
rwgrietveld Re: VBA Method 'Range' of... 12-21-2009, 04:40 PM
royUK Re: VBA Method 'Range' of... 12-21-2009, 04:45 PM
shg Re: VBA Method 'Range' of... 12-21-2009, 07:08 PM
Leith Ross Re: VBA Method 'Range' of... 12-21-2009, 07:10 PM
protonLeah Re: VBA Method 'Range' of... 12-21-2009, 07:46 PM
  1. #1
    Registered User
    Join Date
    12-21-2009
    Location
    Cincinnati, OH
    MS-Off Ver
    Excel 2007
    Posts
    4

    VBA Method 'Range' of object '_Global' failed error

    Hello,


    (Edited for Forum Rule #3 compliance -- sorry about that)


    I'm a newbie to VBA programming, but am pretty experienced with Excel formulas. I'm attempting to invoke the VLOOKUP worksheet function in a macro I am writing (see last 7 lines of my code below), but each time I run I get the Method 'Range' of object '_Global' failed error.

    ("lookuptab" is a named range already defined in the workbook.)

    Any help would be appreciated. Again, I'm new to VBA, so I'm sure it's some basic misunderstanding on my part.

    Thanks in advance,
    Jim





    Sub reformat1()
    '
    ' reformat1 Macro
    '
    
    '
        
    Dim NumCols As Integer, NumRows As Integer
    
        Rows("11:11").Select
        Selection.Find(What:="Team Member Mean", After:=ActiveCell, LookIn:=xlFormulas _
            , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        NumCols = ActiveCell.Column
    
        Columns("a:a").Select
        Selection.Find(What:="Grand Total", After:=ActiveCell, LookIn:=xlFormulas _
            , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        NumRows = ActiveCell.Row
    
    
    Range(Cells(NumRows, 1), Cells(NumRows, NumCols)).Select
    
        With Selection.Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.149998474074526
            .PatternTintAndShade = 0
        End With
        With Selection
            .NumberFormat = "0.0"
                .Font.Bold = True
        End With
    
    
    Range(Cells(11, NumCols), Cells(NumRows, NumCols)).Select
    
        With Selection.Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.149998474074526
            .PatternTintAndShade = 0
        End With
        With Selection
            .NumberFormat = "0.0"
                .Font.Bold = True
        End With
    
    Cells(11, NumCols).Select
    ActiveCell.Value = "Team Member Mean"
    Cells(11, NumCols + 1).Select
    ActiveCell.Value = "Overall Satisfaction w/CVS Caremark"
    
    
    
    Range(Cells(11, NumCols + 1), Cells(NumRows, NumCols + 1)).Select
    
        With Selection.Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.149998474074526
            .PatternTintAndShade = 0
        End With
        With Selection
            .NumberFormat = "0.0"
            .Font.Bold = True
        End With
    
    
    
    
      
    Range(Cells(11, 2), Cells(NumRows, NumCols + 1)).Select
    
        Selection.ColumnWidth = 11
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
    
    Dim cell As Range
    Dim ltab As Range
    
    Set ltab = Range("lookuptab")
    
    For Each cell In Range(Cells(12, NumCols + 1), Cells(NumRows - 1, NumCols + 1))
    cell = Worksheet.Function.VLookup((Range("cell").Offset(0, 0 - NumCols)), ltab, 109, False)
    Next cell
    
    
    End Sub
    Last edited by JKCincy; 12-21-2009 at 04:55 PM.

  2. #2
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: VBA Method 'Range' of object '_Global' failed error

    Welcome to this forum. Please read the forum rules.

    It will explain that you should use code Tags for readability.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA Method 'Range' of object '_Global' failed error

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Thanks for pointing this out Ricardo
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA Method 'Range' of object '_Global' failed error

        Dim cell        As Range
        Dim ltab        As Range
    
        Set ltab = Range("lookuptab")
    
        For Each cell In Range(Cells(12, iCol + 1), Cells(iRow - 1, iCol + 1))
            cell = Worksheet.Function.VLookup((Range("cell").Offset(0, 0 - iCol)), ltab, 109, False)
        Next cell
    1. Is "cell" a defined name on the worksheet? That's what the code shows.

    2. Does .Offset(0, 0 - iCol) resolve to to a valid column?

    3. Are there at least 109 columns in lookuptab?

    Step through your code and look at variables as the code executes. Learning to debug is the biggest favor you could do for your VBA skills.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Method 'Range' of object '_Global' failed error

    Hello JKCincy,

    You have dimensioned the variable cell as a Range object. Later on you are trying to assign the VLookup value to it, which won't work. The code below has several problems.
    For Each cell In Range(Cells(12, NumCols + 1), Cells(NumRows - 1, NumCols + 1))
    cell = Worksheet.Function.VLookup((Range("cell").Offset(0, 0 - NumCols)), ltab, 109, False)
    Next cell
    Replace it with this code...
    Dim Result As Variant
    
    For Each cell In Range(Cells(12, NumCols + 1), Cells(NumRows - 1, NumCols + 1))
      Result = Worksheet.Function.VLookup(cell.Offset(0, 0 - NumCols), ltab, 109, False)
    Next cell
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,963

    Re: VBA Method 'Range' of object '_Global' failed error

    Also, Worksheet.Function.VLookup...
    Should be WorksheetFunction.VLookup...; i.e., no period between worksheet and function
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1