+ Reply to Thread
Results 1 to 36 of 36

After copying in Macro - come back to previous sheet

Hybrid View

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

    After copying in Macro - come back to previous sheet

    I want to copy info from one sheet to another, come back and copy info to another sheet. I want to do this from alot of sheets. My goal is to remember the first page as a variable so if I do this on other sheets it will remember the sheet to come back to without using the tab name.

    example of what I want
    HTML Code: 

    then copy again to another sheet...but I do not want to use the name because if the macro is on a different sheet than I need that sheet name which would be way too much to do

    HTML Code: 
    Last edited by JK1234; 09-27-2008 at 10:20 PM. Reason: Changing title -too vague

  2. #2
    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.

  3. #3
    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.

  4. #4
    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

  5. #5
    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.

  6. #6
    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

  7. #7
    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.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You can get the required raange by recording a macro

        Range("A1:Z100, C1:C5").Copy

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

    Thank You

    Thank you.

    You guys here are so knowledgeable about Excel......thank you for helping us!!!!

    This should work nicely!!!

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I'm not sure why you are doing this, can you explain? There might be a simpler way to do it. You could use Named Ranges for example.

    I would start by using Data validation instead of the comboboxes
    Last edited by royUK; 09-28-2008 at 01:10 PM.

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

    Macro

    I used the pull downs because there wasn't any room in the cells, the cells below them had different data and I had to resize the column widths and row heights. The pulldowns were an after thought so they fit better.

    My goal is to be able to copy from any sheet any section to any other sheet (many) and in any section. The good things are the sections are all the same amount of rows and columns.

    After playing with code the last several months, I am starting to understand "real" code more and appreciate that.

    I can actually write the code now with your guys help, but it would be alot of If pulldown 1 = ? and pulldown 2 = then. As you can see with 21 pages this could be alo ifs/then.

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Also get rid of the merged cells, they should be avoided because they will cause problems sooner or later.

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Instead of multiple Ifs, use Select Case, it's easier to follow.

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

    How about this

    I still need to look up how to use CASE properly,

    but besides that, this is something I was trying to do. Of course it does not work yet? Suggestions??
    Attached Files Attached Files

  15. #15
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Here's how your case should look, but I don't understand what you mean by "for each selection" ... each selection of what?

        Select Case Range("N5")
            Case 1 To 5
                Set Ws = Worksheets("Sheet2")
            Case 2, 8, 14
                Set Rng2 = Range("C36:C41")
        End Select
    
        Select Case Range("Q5").Value
            Case 1
                Set Rng1 = Range("C28:C33")
        End Select

  16. #16
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Not sure if this is what you want, sheet references might need changing, but I have attempted to change your code to use Select Case

    Sub AutoShape1_Click()
    
        With Application
            .ScreenUpdating = False
    
            Dim Ws As Worksheet
            Dim Rng1 As Range
            Dim Rng2 As Range
    
    
            Select Case Range("N5").Value
                Case 1 To 5: Set Ws = Worksheet("Sheet2")
                Case 2, 8, 14: Set Rng2 = Range("C36:C41")
                Case Else: MsgBox "Incorrect entry"
            End Select
    
            Select Case Range("Q5").Value
                Case 1: Set Rng1 = Range("C28:C33")
                Case Else: MsgBox "Incorrect entry"
            End Select
    
            'The next part would do the copying and pasting depending upon what I selected
    
            Rng1.Copy Destination:=Rng2
            .ScreenUpdating = False
            .CutCopyMode = False
        End With
    End Sub

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

    Close

    I think we are close.

    the one line
    HTML Code: 
    gives me an error
    if I do this:
    HTML Code: 
    It works with assigning a value to the rng2

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

    More Help

    I was looking for more examples as how to fix my macro but I am still in need of more expertise.

    I attached my last go around. I did manage to change another macro by using variables. Yeally shortens the code alot! So I am learning.

  19. #19
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I have simplified this, it shoulod give you an idea what you need to do.
    Attached Files Attached Files

  20. #20
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770
            Select Case Range("N5").Value
                Case 1 To 5: Set Ws = Worksheets("Sheet2")
                Case 1, 7, 13: Set Rng2 = Range("C28:C33")
                Case Else: MsgBox "Incorrect entry"
            End Select
    
            Select Case Range("Q5").Value
                Case 1: Set Rng1 = Range("C28:C33")
                Case Else: MsgBox "Incorrect entry"
            End Select
    
            'The next part would do the copying and pasting depending upon what I selected
    
            Rng1.Copy Destination:=Ws.Range("C28:C33")
    I'm not entirely familiar with how these 'case' statments are arranged, as I would have thought that having a "Case 1, 7, 13:" after a "Case 1 To 5:" would be slightly pointless. Does a case of "1" get caught by BOTH statements then?

    Anyhoo, enough of my own confusion. Looking at the above code it would seem that 'Ws' is only set if "N5" has a value of 1 to 5. If it is 7 or 13 then Rng2 is set instead.
    Check it in debug and see if 'Ws' is nothing when you get to the 'copy' line.

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

    I changed the macro to show what works

    The macro can work with either Ws or Rng2 but not together in the same command??

    I assigned Rng2 so there would be no confusion.


    HTML Code: 

  22. #22
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    I think you may have missed what Phil_V and I were saying ... It is possible that if your conditions are not met in the case statements, that neither Ws or Rng1 or Rng2 are ever set (so their value would be 'Nothing'). If this is right, then the attempt to copy/paste at the end (which is not conditional) is hit and your objects are not set ... Make sense? Think about running through this where N5 = 6 ...

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

    Hmm

    I understand what you are saying, but the conditions are met. I made sure N5 = 1 and Rng2 is always set to a range.

    I did play with the them with different numbers and I see what you are saying and it does give error as expected.

    What I assume then is both the Ws are correct (Ws = Worksheets("Sheet2") and the Rng2 is set to ("D28:D33"). Alone in the code they both work fine. But when I put them two togther they do not. Thats whats weird...or am I still missing something???

  24. #24
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    ... Actually ... that's not it ... I simplified the code to test as:
    Sub AutoShape4_Click()
    
    Dim Ws As Worksheet
    Dim Rng1 As Range, Rng2 As Range
    
        Range("N5").Value = 1
        Range("Q5").Value = 1
        Set Ws = Worksheets("Sheet2")
        Set Rng1 = Range("C28:C33")
        Set Rng2 = Ws.Range("C28:C33")
        
        ActiveSheet.Rng1.Copy Destination:=Rng2
    
    End Sub
    But it doesn't like that either, which I didn't expect ... sorry

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

    Observation

    The code does not seem to like anything in front of any of the Rng statments.

+ 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