+ Reply to Thread
Results 1 to 6 of 6

Loop through sheets error

Hybrid View

  1. #1
    Registered User
    Join Date
    10-29-2013
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    4

    Loop through sheets error

    Hi,

    I'm new to VBA and I have a problem with a macro I created. I am trying to loop through all the worksheets in the workbook paste some formulas, do a calculation, take that calculation and place it in a worksheet at the end of the workbook and place the value into the first empty cell in column "A" of the worksheet. The problem is when I run the code I get an error on the destination line:

    Range("IO5").Copy Destination:=Worksheets("Sheet4").Range("A" & Rows.Count).End(xlDown).Offset(1).Select
    What am I doing wrong?

    Here is the complete code:

    Sub INSERT()
    '
    ' INSERT Macro
    '
    
        Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
      
        Windows("workbook1.xlsm").Activate
        Range("H1:IW1675").Select
        Selection.Copy
        Windows("workbook2.xlsm").Activate
        Range("H1").Select
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
            Range("IO5").Copy Destination:=Worksheets("Sheet4").Range("A" & Rows.Count).End(xlDown).Offset(1).Select
            
            ws.Delete
            Next ws
    End Sub
    Thanks
    Last edited by JM5; 12-28-2013 at 01:01 AM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Loop through sheets error

    Hi, JM5,

    please add code-tags when showing procedures where on Excel Forum.

    You start at the last cell and want to go further down - no chance there:
    Range("IO5").Copy Destination:=Worksheets("Sheet4").Range("A" & Rows.Count).End(xlDown).Offset(1)
    You could check the last cell for any entry:
    Range("IO5").Copy Destination:=Worksheets("Sheet4").Range("A" & Rows.Count).End(xlUp).Offset(1)
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    10-29-2013
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Loop through sheets error

    Hey thanks. That worked great. I have one more problem. When I delete the worksheet at the end of the macro, it removes the the numbers in "sheet4" and gives me zeros. I tried looking at how to special paste values with a destination, but it seems you can't that. How would I paste the values either directly to "sheet4" or paste the way I am doing and then repasting the values in "column B" with special paste? Thanks.

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Loop through sheets error

    You may put the value directly:
    Worksheets("Sheet4").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Range("IO5").Value
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  5. #5
    Registered User
    Join Date
    10-29-2013
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Loop through sheets error

    That works thanks.

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Loop through sheets error

    Hi @JM5,

    Please add code tags to your first post and mark the thread as solved.

    Thanks.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] loop action through all sheets in workbook skipping certain sheets
    By sawa85sa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2013, 06:40 PM
  2. Error handling--resume next loop, but for a "DO WHILE" loop
    By submariner18 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-18-2012, 05:35 PM
  3. Want to do a while-loop but get error message: run error '6' overflow
    By danzi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-07-2011, 01:48 PM
  4. Nesting a loop within a loop error
    By PunPryde in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2011, 08:07 PM
  5. Error '1004', application/object error, Do Loop
    By farzyness in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2011, 12:03 PM

Tags for this Thread

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