+ Reply to Thread
Results 1 to 3 of 3

Running Macro on same row in two worksheets

Hybrid View

dale040205 Running Macro on same row in... 12-11-2012, 04:37 PM
HaHoBe Re: Running Macro on same row... 12-12-2012, 12:51 AM
dale040205 Re: Running Macro on same row... 12-12-2012, 08:04 AM
  1. #1
    Registered User
    Join Date
    12-11-2012
    Location
    Columbia, South Carolina
    MS-Off Ver
    Excel 2007
    Posts
    4

    Running Macro on same row in two worksheets

    I know this has been asked many times on many different forums, which is where I got the code I'm using, but I just can't seem to get it to work correctly. My workbook has five worksheets, and my code is supposed to insert a blank row at the same spot on two of them, copying the formulae from the row above. However, all it ends up doing is inserting five blank rows with the formulae in the worksheet the user is viewing. Now, I did combine two codes from two different places, so I'm sure the problem is I didn't combine them properly (one for inserting the blank row, the other for doing it across multiple worksheets). The two worksheets I want this code to run on are labeled "Register" and "Budget". Here's my code:

    Sub Insert_Rows()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Register" Or ws.Name <> "Budget" Then
       If Selection.Rows.Count <> 1 Then Exit Sub
       If Selection.Areas.Count <> 1 Then Exit Sub
       With Selection.EntireRow
       .Offset(-1).Copy
       .Insert
       On Error Resume Next
       .Offset(-1).SpecialCells(xlCellTypeConstants, 23).ClearContents
       On Error GoTo 0
       End With
    End If
    Next ws
    End Sub
    Thanks for the help!
    Last edited by dale040205; 12-12-2012 at 08:03 AM. Reason: added code tags

  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: Running Macro on same row in two worksheets

    Hi, dale040205,

    although you loop through all sheets the code will always be performed on the active sheet due to the fact that there is no indication for VBA to apply it to different worksheets.

    Maybe give this code a try:
    Sub Insert_Rows()
    Dim ws As Worksheet
    Dim lngRow As Long
    
    If Selection.Rows.Count <> 1 Then Exit Sub
    If Selection.Areas.Count <> 1 Then Exit Sub
    lngRow = Selection.Row
    
    For Each ws In ActiveWorkbook.Worksheets
      If ws.Name <> "Register" Or ws.Name <> "Budget" Then
        With ws.Rows(lngRow)
          .Offset(-1).Copy
          .Insert
          On Error Resume Next
          .Offset(-1).SpecialCells(xlCellTypeConstants, 23).ClearContents
          On Error GoTo 0
        End With
      End If
    Next ws
    
    End Sub
    And please use Code-Tags to display the code as requested per Forum-Rules.

    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
    12-11-2012
    Location
    Columbia, South Carolina
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Running Macro on same row in two worksheets

    That worked great, thank you. Added code tags to original post!

+ 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