+ Reply to Thread
Results 1 to 3 of 3

Making Code Loop

Hybrid View

  1. #1
    Registered User
    Join Date
    11-09-2018
    Location
    USA
    MS-Off Ver
    365
    Posts
    59

    Making Code Loop

    Hi,

    I have this code which works perfectly. However, I want it to loop, but I can't figure out where to adjust the code. The code takes a worksheet from one workbook and replaces a worksheet, with the same name, on another workbook. On my source workbook, I have multiple tabs that I would like this to occur with.

    Here's the code:
    Sub CopyReplaceWorksheet()
     '--copies activesheet into specified destination workbook
     '  if sheet with same name exists in destination workbook, then
     '    deletes existing sheet and locates copy in same order
    
     Dim lSheetIndex As Long
     Dim sErrMsg As String
     Dim wkbDestination As Workbook
     Dim wksSource As Worksheet, wksTemp As Worksheet
     
     On Error GoTo ErrProc
     Application.EnableCancelKey = xlErrorHandler
     Application.EnableEvents = False
     
     '--modify to actual workbook name
     Set wkbDestination = Workbooks("my workbook.xlsx")
     
     Set wksSource = ActiveSheet
     '--validate destination workbook is not activeworkbook
     If ActiveWorkbook.name = wkbDestination.name Then
       MsgBox "This macro won't copy Active Sheet in destination workbook."
       GoTo ExitProc
     End If
     
     lSheetIndex = lGetSheetIndex(sSheetName:=wksSource.name, wkb:=wkbDestination)
     
     If lSheetIndex Then
       If lSheetIndex = wkbDestination.Sheets.Count Then
          '--if existing sheet is last in workbook, add temp sheet.
          '  this handles problem of trying deleting only sheet and
          '  simplifies ordering of copied sheet.
          Set wksTemp = wkbDestination.Worksheets.Add( _
             After:=wkbDestination.Sheets(lSheetIndex))
       End If
       
       '--delete existing worksheet with same name
       Application.DisplayAlerts = False
       wkbDestination.Sheets(wksSource.name).Delete
       Application.DisplayAlerts = True
     Else
       '--if no existing sheet, order copy as first sheet
       lSheetIndex = 1
     End If
       
     wksSource.Copy Before:=wkbDestination.Sheets(lSheetIndex)
     
    ExitProc:
     On Error Resume Next
     '--delete temp worksheet if exists
     If Not wksTemp Is Nothing Then
       Application.DisplayAlerts = False
       wkbDestination.Sheets(wksTemp.name).Delete
       Application.DisplayAlerts = True
     End If
     
     Application.EnableEvents = True
     If Len(sErrMsg) Then MsgBox sErrMsg
     Exit Sub
    
    ErrProc:
     sErrMsg = Err.Number & ": " & Err.Description
     Resume ExitProc
    
    End Sub
    Here's the function for the code:
    Function lGetSheetIndex(sSheetName As String, wkb As Workbook) As Long
     '--returns sheet index within workbook if found, else returns 0
     On Error Resume Next
     lGetSheetIndex = wkb.Sheets(sSheetName).Index
    End Function

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,809

    Re: Making Code Loop

    I have multiple tabs that I would like this to occur with.
    Do you want to do this with all existing tabs, or just certain ones? If just certain ones, we'll need a list of names.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    11-09-2018
    Location
    USA
    MS-Off Ver
    365
    Posts
    59

    Re: Making Code Loop

    Hi, I think I was overthinking this. I was able to solve it by doing a "for each ws in..." loop. Thanks!

+ 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. making optionbutton visible or invisible by loop
    By lettersofberk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2013, 12:33 PM
  2. [SOLVED] Making a For loop to loop through a set of cells
    By Smeddlesboy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-06-2012, 08:52 PM
  3. Making all pivot items invisible before doing Do Loop
    By hattrick_123a in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-21-2011, 08:50 PM
  4. Trouble making scatter plot loop
    By henboffman in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-22-2011, 09:37 AM
  5. Making a simple loop faster
    By gummi in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-23-2008, 07:07 PM
  6. [SOLVED] Making macros loop
    By McKCollins in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-10-2006, 08:59 AM
  7. Need Help on Making a Loop
    By japorms in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2005, 09:05 AM

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