+ Reply to Thread
Results 1 to 36 of 36

After copying in Macro - come back to previous sheet

Hybrid View

  1. #1
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    There is no need to select the range and then copy the range. You can specify the range to be copied in one step. (the blue line below)

    The following will copy the A1:Z100 range and then paste that information into sheets 2 and 3 in the same range of cells.

    Sub MoveToMultipleSheets()
    
    Worksheets("Sheet1").Range("A1:Z100").Copy
    With Worksheets(1)
        .Paste Destination:=Worksheets("Sheet2").Range("A1")
        .Paste Destination:=Worksheets("Sheet3").Range("A1")
    End With
    
    Application.CutCopyMode = False
    
    End Sub
    Last edited by mdbct; 09-27-2008 at 10:25 PM. Reason: typo and highlighting.

  2. #2
    Forum Contributor
    Join Date
    07-06-2008
    Location
    NC
    Posts
    149

    Other sheets

    This should work but my source page may change from sheet1 to another sheet and I was hoping to use this same macro on each sheet. So then sheet1 would be the active sheet. Then after it copied into the other two sheets then come back to this same active sheet.
    So depending upon what sheet it is on would depend what is in the macro.

    But I like the new expression, going to start using that instead.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Quote Originally Posted by JK1234 View Post
    This should work but my source page may change from sheet1 to another sheet and I was hoping to use this same macro on each sheet. So then sheet1 would be the active sheet. Then after it copied into the other two sheets then come back to this same active sheet.
    So depending upon what sheet it is on would depend what is in the macro.

    But I like the new expression, going to start using that instead.
    Maybe something like this change to mdbct's code
    Option Explicit
    
    Sub MoveToMultipleSheets()
        Dim i      As Integer
        i = ActiveSheet.Index
    
        ActiveSheet.Range("A1:Z100").Copy
        With Worksheets(i)
            .Paste Destination:=Worksheets(i + 1).Range("A1")
            .Paste Destination:=Worksheets(i + 2).Range("A1")
        End With
        Application.CutCopyMode = False
    
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    If I understand correctly from this:
    This should work but my source page may change from sheet1 to another sheet and I was hoping to use this same macro on each sheet. So then sheet1 would be the active sheet. Then after it copied into the other two sheets then come back to this same active sheet. So depending upon what sheet it is on would depend what is in the macro.
    ... you're saying that you want the same code to run for different source sheets? You seem to be thinking that as you copy to other sheets those become active, but with the code mcbct provided, this is not the case and the source remains the active sheet. So - if this understanding is right, you want it to run where the source is the activesheet ... try this:
    Activesheet.Range("A1:Z100").Copy
    With Activesheet
        .Paste Destination:=Worksheets("Sheet2").Range("A1")
        .Paste Destination:=Worksheets("Sheet3").Range("A1")
    End With
    Or you can pass the name into the routine as a parameter based on however you determine the source; e.g. call like this:
    Sub YourControlRoutine()
    Dim wshSource As Worksheet
        ' your code to the point where you initiate the copy, then ...
        
        Set wshSource = ActiveWorkbook.Worksheets(1)    'or whatever ...
        Call MoveToMultipleSheets(wshSource)
        
        ' etc etc etc ...
    End Sub
    
    Sub MoveToMultipleSheets(sht As Worksheet)
        sht.Range("A1:Z100").Copy
        With sht
            .Paste Destination:=Worksheets("Sheet2").Range("A1")
            .Paste Destination:=Worksheets("Sheet3").Range("A1")
        End With
        Application.CutCopyMode = False
    End Sub
    Hope that helps.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  5. #5
    Forum Contributor
    Join Date
    07-06-2008
    Location
    NC
    Posts
    149

    These shold work - what if need more copy

    What if I wanted to copy say two sets of items,

    like from A1:Z100 and C1:C5

    would I need to do this for each or can I somehow combine them??

    Thanks for the help

  6. #6
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Hi - I've changed the code below just to show you some options about this sort of thing; I do recommend though that you have a think about how your ranges are defined (static or dynamic) and you might be able to create a loop that does the same thing. I've made this as generic as I can to demonstrate and you can choose the most appropriate option for what you're doing ...
    Sub YourControlRoutine()
    Dim wshSource As Worksheet, wshTarget1 As Worksheet, wshTarget2 As Worksheet
    Dim strSourceRange1 As String, strSourceRange2 As String
        'your code to where you initiate the copy/paste, then ...
        
        'if these are static then just set them:
        strSourceRange1 = "A1:A5"
        strSourceRange2 = "C1:C5"
        '(... but if they aren't static, and (say) you select the ranges
        'before copying, then you could use just one variable (setting
        'it as the range is selected or the address is otherwise determined,
        'such as:
        'strSourceRange = Selection.Address).
        
        Set wshTarget1 = ActiveWorkbook.Worksheets(2)
        Set wshTarget2 = ActiveWorkbook.Worksheets(3)
        
        Set wshSource = ActiveWorkbook.Worksheets(1)
        
        Call MoveToMultipleSheets(wshSource, wshTarget1, wshTarget2, strSourceRange1)
        Call MoveToMultipleSheets(wshSource, wshTarget1, wshTarget2, strSourceRange2)
        
        'depending on what you're doing, you could call the other sub 4 times and
        'just pass in a source & one target each time, which would look like:
        'Call MoveToMultipleSheets(wshSource, wshTarget1, strSourceRange1)
        'Call MoveToMultipleSheets(wshSource, wshTarget2, strSourceRange1)
        'Call MoveToMultipleSheets(wshSource, wshTarget1, strSourceRange2)
        'Call MoveToMultipleSheets(wshSource, wshTarget2, strSourceRange2)
        
        ' etc etc etc ...
        
    End Sub
    
    Sub MoveToMultipleSheets(shtS As Worksheet, shtT1 As Worksheet, shtT2 As Worksheet, strAddress As String)
        shtS.Range(strAddress).Copy
        With shtS
            .Paste Destination:=shtT1.Range(strAddress)
            .Paste Destination:=shtT2.Range(strAddress)
        End With
        Application.CutCopyMode = False
    End Sub
    Hope that helps ... MM.

  7. #7
    Forum Contributor
    Join Date
    07-06-2008
    Location
    NC
    Posts
    149

    My File

    Here is an example:

    There are alot of combinations to copy each section into each section of another page. Altogther there are 5 sections that could be copied into any section of another page.

    I can write the code but it would be alot if this, then this.

    The last one is different that what I am used to (as I am not a programmer)

    I am trying to write and learn about real code as possible...seems to work better and can be easier changed. I could write a macro that woould work but it would take so long. I like your guys ideas. Here is an example file that i tried to make as simple as possible. I get an error when running with combining the paste parts.
    Attached Files Attached Files

+ 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. Macro to populate a cell and then execute another macro
    By andrewc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-29-2008, 02:19 PM
  2. macro to create a macro?
    By jojotherider in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-21-2008, 08:34 PM
  3. Macro for copy/insert into expanding table
    By Soslowgt in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-06-2008, 07:24 PM
  4. How to splitt texts into words? (collecting word and compounds)
    By wali in forum Excel Programming / VBA / Macros
    Replies: 53
    Last Post: 02-03-2008, 04:06 AM
  5. Conditional formatting macro (highlight macro)
    By c991257 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-09-2007, 02:46 PM

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