+ Reply to Thread
Results 1 to 11 of 11

Run-time error when running macro from personal workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    05-20-2008
    Posts
    9

    Run-time error when running macro from personal workbook

    Hi,

    I have a very simple macro that i use to divide a an entire column (besides the header row) by 1000...

    Sub Macro1()
    
    Const x As Long = 1000
    Dim cl As Range
    Dim rng As Range
     
    Set rng = Sheet1.Range("d2", Range("d65536").End(xlUp)).SpecialCells(xlCellTypeConstants)
    For Each cl In rng
        cl.Value = cl.Value / x
    Next cl
    End Sub
    It works fine as long as the macro is attached to the workbook in which I am trying to use it. If the macro is attached to the personal workbook, I get a runtime error when I attempt to run it...

    Run-time error '1004':

    Method 'Range' of object '_Worksheet' failed


    When I hit Debug, this line is highlighted...

    Set rng = Sheet1.Range("d2", Range("d65536").End(xlUp)).SpecialCells(xlCellTypeConstants)
    There appears to be some confusion when the range is defined, but I don't see why which workbook the macro is attached to should make a difference.

    I am VERY new to this, so any help would be appreciated.

    Thanks,
    Corey

  2. #2
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177
    I get the error no matter where the macro is.
    Try this.
    Sub Test1()
    Const x As Long = 1000
    Dim cl As Range
    Dim Sh1RangeD   'Sh1RangeD
    Dim Sh1LastRowD 'Sht1LastRowD
    
    With Sheets("Sheet1") 'Column D
       Sh1LastRowD = .Cells(Rows.Count, "D").End(xlUp).Row
       Set Sh1RangeD = .Range("D2:D" & Sh1LastRowD).SpecialCells(xlCellTypeConstants)
    End With
    
    For Each cl In Sh1RangeD
        cl.Value = cl.Value / x
    Next cl
    End Sub
    Regards

    Rick
    Win10, Office 365

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Here's another way:
    Sub Macro1()
        Dim rBlank  As Range
    
        Set rBlank = ActiveSheet.Cells.SpecialCells(xlCellTypeBlanks).Cells(1, 1)
        rBlank.Value = 1000
        rBlank.Copy
        Range("D2", Cells(Rows.Count, "D").End(xlUp)).SpecialCells(xlCellTypeConstants, xlNumbers).PasteSpecial _
                Paste:=xlPasteValues, operation:=xlPasteSpecialOperationDivide
        rBlank.ClearContents
    End Sub

  4. #4
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177
    Damn you and your clean compact code!


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

  6. #6
    Registered User
    Join Date
    05-20-2008
    Posts
    9
    Thanks very much for the replies guys. Rick's code worked like a charm. When I tried shg's code, I got the following error:

    Run-time error '1004':

    No cells were found


    Upon debugging, the following line of code was highlighted...

    Set rBlank = ActiveSheet.Cells.SpecialCells(xlCellTypeBlanks).Cells(1, 1)

    Like I said, I'm a complete noob, so it's entirely possible that I deployed it wrong. I've just been cutting and pasting these into Visual Basic. Any thoughts?

    Thanks again for your replies and patience.

    -Corey

+ 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