+ Reply to Thread
Results 1 to 12 of 12

Run time error 1004 - Method 'Range' of 'Object'_Global' failed

Hybrid View

  1. #1
    Registered User
    Join Date
    01-23-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    58

    Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    Hi I am trying to build a macro which will format the columns of a spreadsheet - basically it inserts some columns, writes formulas and highlights them. Here is a code I have got so far...

    When I try to run this I get a run time error 1004 - Method 'Range' of 'Object'_Global' failed. The part of the code
    Range("N2:N").FormulaR1C1 = "=(RC[-7]/RC[-2])"

    is highlighted in the debugger.

    Can anyone tell me why this is happening, also it would be great if you could suggest better ways of writing this code - as I am new to vba programming and most of my macros are built using the recorder and then 'working' on them.

    Thanks.


    Sub formatcolumns()
    
        Columns("G:G").Insert Shift:=xlToRight
        Range("H1").Select
        Selection.Copy
        Range("G1").Select
        ActiveSheet.Paste
        Columns("N:N").Select
        Application.CutCopyMode = False
        Selection.Insert Shift:=xlToRight
        Range("N1").FormulaR1C1 = "=(RC[-1])"
        Range("N2:N").FormulaR1C1 = "=(RC[-7]/RC[-2])"
        Columns("N:N").Interior.ColorIndex = 36
        Columns("R:R").Insert Shift:=xlToRight
        Range("R1").FormulaR1C1 = "=(RC[-1])"
        Range("R2:R").FormulaR1C1 = "=(RC[-4]/RC[-2])"
        Columns("R:R").Interior.ColorIndex = 36
        Columns("U:U").Insert Shift:=xlToRight
        Range("U1").FormulaR1C1 = "=(RC[-1])"
        Range("U2:U").FormulaR1C1 = "=(RC[-14]*RC[-2])"
        Columns("U:U").Interior.ColorIndex = 36
        Columns("AC:AC").Insert Shift:=xlToRight
        Range("AC1").FormulaR1C1 = "=(RC[-1])"
        Range("AC2:AC").FormulaR1C1 = "=(RC[-5]+RC[-4]+RC[5]+RC[-22])/(RC[1]+RC[2])"
        Columns("AC:AC").Interior.ColorIndex = 15
        Columns("AJ:AJ").Interior.ColorIndex = 35
        Columns("AN:AN").Insert Shift:=xlToRight
        Range("AN1").FormulaR1C1 = "=(RC[-1])"
        Range("AN2:AN").FormulaR1C1 = "=(RC[-4]-RC[-33])"
        Columns("AN:AN").Interior.ColorIndex = 35
        Columns("AR:AR").Insert Shift:=xlToRight
        Range("AR1").FormulaR1C1 = "=(RC[-1])"
        Range("AR2:AR").FormulaR1C1 = "=(RC[-4]/RC[-3])"
        Columns("AR:AR").Interior.ColorIndex = 35
        Columns("BB:BD").Select
        Selection.Cut
        Columns("L:L").Insert Shift:=xlToRight
        Columns("M:N").Select
        Selection.Cut
        Columns("R:R").Insert Shift:=xlToRight
        Columns("J:J").Select
        Selection.Cut
        Columns("D:D").Insert Shift:=xlToRight
        Range("N2").Select
        Selection.AutoFilter
        ActiveWindow.FreezePanes = True
        Columns("AG:AG").Select
        Selection.Interior.ColorIndex = 34
        Columns("AH:AH").Select
        Selection.Interior.ColorIndex = 33
    End Sub

  2. #2
    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: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    Range("N2:N") doesn't describe a range; it needs a row number for the second N.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    01-23-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    Ok, I will need it to do it for variable number of rows since the number of rows will difffer with the data - do I need to define a lastrow variable? if so, how can I do that?

  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: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    Perhaps
    Range("N2").Resize(nRows).FormulaR1C1 = ...
    ... however you calculate nRows

  5. #5
    Registered User
    Join Date
    01-23-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    I did this...

    Range("N2").Resize(nRows).FormulaR1C1 = "=(RC[-7]/RC[-2])"
    ....it gives me the same error again...

  6. #6
    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: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    How did you calculate nRows?

  7. #7
    Registered User
    Join Date
    01-23-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    I did not and I do not know how to...

  8. #8
    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: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    Me either -- I don't know what you're doing.

  9. #9
    Registered User
    Join Date
    01-23-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    I am trying to get it to write that formula in all cells from N2 to the last row of the data.

  10. #10
    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: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    Try
    Range("N2", Range("N2").End(xlDown)).FormulaR1C1 = "=(RC[-7]/RC[-2])"

  11. #11
    Registered User
    Join Date
    01-23-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    It worked. Thanks - Is there anything else you would suggest to clean up the code?

  12. #12
    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: Run time error 1004 - Method 'Range' of 'Object'_Global' failed

    It's hard to tell by looking at the code.

+ 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