+ Reply to Thread
Results 1 to 36 of 36

After copying in Macro - come back to previous sheet

  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.

    Please Login or Register  to view this content.
    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
    Please Login or Register  to view this content.
    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:
    Please Login or Register  to view this content.
    Or you can pass the name into the routine as a parameter based on however you determine the source; e.g. call like this:
    Please Login or Register  to view this content.
    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
    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

    Please Login or Register  to view this content.

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

  9. #9
    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 ...
    Please Login or Register  to view this content.
    Hope that helps ... MM.

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

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

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

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

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

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

    Please Login or Register  to view this content.

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

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

    Please Login or Register  to view this content.

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

  20. #20
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Please Login or Register  to view this content.
    Versus:
    Please Login or Register  to view this content.
    The 2nd one should be
    Please Login or Register  to view this content.
    ... so it goes to the right sheet, otherwise you're cutting & pasting to the same place in the active sheet ..

    Or - since you've got different variables for the same range address (as far as I can tell from the snippet) then set Rng2 explicitly such as:
    Please Login or Register  to view this content.
    And then you can:
    Please Login or Register  to view this content.
    Hope that helps

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

    get error

    When I do change to this:
    HTML Code: 
    I get the error: Method or data member not found. Is it assinged or DIm correctly??

    I also thought that would work.

  22. #22
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Ah ha! I think it may be because Rng2 is only set if a particular case condition is met ... When stepping through, hover over Rng2 and if it says "Nothing", then that's your problem

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

    Not sure still

    I tried to assign Rng2 to
    HTML Code: 
    and it still gives me the error. I would think that would have worked.

    ?????

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

    Just noticed

    When I remove the Ws part in:
    HTML Code: 
    It will or copy from the active sheet to the active sheet. Something must be wrong with the Ws part then????

  25. #25
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770
    Please Login or Register  to view this content.
    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.

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

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

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

  29. #29
    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:
    Please Login or Register  to view this content.
    But it doesn't like that either, which I didn't expect ... sorry

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

  31. #31
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Rng doesn't need anything in front of it, it's already completely parented: It refers to a range on a worksheet in a workbook.
    Entia non sunt multiplicanda sine necessitate

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

    It copies to same Page

    It does does not copy to the page desired though. only to the active page this way??

  33. #33
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Sorry, this thread's gone on way too long to read all of it.

    I'm happy to help if it can be explained clearly in a nutshell.

  34. #34
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Post

    I don't believe you can 'Set' a range, and THEN try to reference that to a worksheet in the way you have tried with:

    Please Login or Register  to view this content.
    The following code works fine:

    Please Login or Register  to view this content.
    If you want Rng1 to be FORCED to be on the active sheet, then on the line where Rng1 is set you should do;

    Please Login or Register  to view this content.
    I'm still not clear on what exactly you are trying to achieve, but the above code DOES work, and hopefully it can help you to solve your problem yourself.

  35. #35
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You need to think through how you are setting your values


    Please Login or Register  to view this content.
    This code was only based on what you originally posted & was to demonstrate how the Select Case is set up.

    I would think you first need to determine a value for the worksheet to paste to Then the range on that sheet to copy to.

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

+ 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