+ Reply to Thread
Results 1 to 18 of 18

Simple copying, but getting stuck in macro

  1. #1
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Exclamation Simple copying, but getting stuck in macro

    I have multiple worksheets where tables of information are created from a macro and formulas. These tables are of varying row lengths and I want to copy these tables to another sheet (as values). I have set up a cell that shows a count for the number of rows and I am trying to use the information to define the range to be copied and then pasted. Ultimately I want to run this same macro on each of the sheets that have a table so that all the tables are eventually pasted additively to a separate sheet (which is why I have the cumbersome series of keystrokes once I get to the destination sheet for pasting to get the cell to the next empty one under the last paste). For now I'm just trying to get one set of tables copied and pasted. There are two tables on each sheet, one for buys and one for sells, and the pasted tables for each go to two separate sheets (buys and sells). I have copied the macro I have so far, but am getting an error with respect to how I'm defining the ranges ("buyrange" and "sellrange"). I am a little new to VB so would appreciate any help.

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 04-13-2013 at 01:20 AM. Reason: Added code tags, as per Forum Rules, take a moment to read them, link above in the menu bar. Thanks!

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Simple copying, but getting stuck in macro

    Please read the rules on using code tags.
    When you have, add them to your first post. (JBeaucaire has added them for you this time)

    Try this on a copy of your workbook.


    Please Login or Register  to view this content.
    Last edited by jolivanes; 04-13-2013 at 01:35 AM. Reason: JBeaucaire added the code tags

  3. #3
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    Thanks and sorry for the delay in getting back to the thread. The only problem with the above is that the table that the macro is to draw from is a collection of If formulas (showing blank if the referenced cells contain nothing) so if I use end.down I will end up grabbing all of the rows in the table, including the blank ones. This is why I set a counter cell above the table that gives the actual number of rows with data. My problem was in trying to define a range that will have fixed columns but where the number of rows will change depending on what is in the counter cell.

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Simple copying, but getting stuck in macro

    Are the blank looking cells just at the end of a range or scatterd troughout the length of the column?
    Either way, you could use Autofilter >0 and copy the visible cells only.
    A stripped down copy of a workbook would greatly assist.
    Change information that is not to be seen by others in the workbook.

  5. #5
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    I have done some re-working but still getting errors (object variable or With block variable not set). Using the code above I changed my macro but "buyrange" is still not getting defined right. BA4 is where I have the counter for the number of rows that actually contain data (and not just empty formulas). I also defined a cell on the target sheet (buyblock) where I want to copypastevalue the data from the buyrange. I think I also have a problem in defining where to start on buyblock (the counter on that sheet, located in W1, changes as data is copied in there and I simply need to have the target cell as "A" + buystartrow, or the value in W1). Here it is:

    Dim maxrows_buy As Long
    Dim buyrange As Range
    Dim buystartrow As Long
    Dim buystart As Range
    maxrows_buy = Range("BA4").Value
    buyrange = Range("AV8:BH" & maxrows_buy)
    buystartrow = Sheets("Buyblock").Cells("W1").Value
    buystart = Sheets("BuyBlock").Range("A" & buystartrow)
    buyrange.Select
    Selection.Copy
    Sheets("BuyBlock").Select
    Range(buystart).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    End Sub

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Simple copying, but getting stuck in macro

    @agpyle,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    HTH
    Regards, Jeff

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Simple copying, but getting stuck in macro

    @agpyle
    I have an example file for you to have a look at but we're not allowed to answer threads where people are not adhering to the rules. And so it should be.
    See jeffreybrown's comment.

  8. #8
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    Sorry, didn't know about having to put tags in. Here is the code again, I assume I don't have to repeat my comments in the earlier posts.

    Please Login or Register  to view this content.
    Dim maxrows_buy As Long
    Dim buyrange As Range
    Dim buystartrow As Long
    Dim buystart As Range
    maxrows_buy = Range("BA4").Value
    buyrange = Range("AV8:BH" & maxrows_buy)
    buystartrow = Sheets("Buyblock").Cells("W1").Value
    buystart = Sheets("BuyBlock").Range("A" & buystartrow)
    buyrange.Select
    Selection.Copy
    Sheets("BuyBlock").Select
    Range(buystart).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    End Sub
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    Please Login or Register  to view this content.
    Last edited by arlu1201; 05-27-2013 at 03:40 PM. Reason: Corrected code tags.

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Simple copying, but getting stuck in macro

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found

    http://www.excelforum.com/misc.php?do=bbcode

    About 2/3rds down is the explanation about code tags

  11. #11
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    Would it help if I uploaded the workbook as well, though I have already populated tables manually.

  13. #13
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    I highlighted the code as suggested and clicked on #, showing <CODE> at the start and end, but I don't see the CODE tags in my post when I view it now.

  14. #14
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Simple copying, but getting stuck in macro

    Did you see how your code is in a blue separate window now? That's the way.

    Have a look at this simple example.
    The pasting can be done anywhere you want.
    This is for Excel 2007. I hope that's what you've got.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    I opened the file but I'm not sure what this is pertaining to.

    It is for Excel 2007, correct.

  16. #16
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Simple copying, but getting stuck in macro

    Re post #12
    Yes, attach your workbook with before and after.

  17. #17
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    I didn't receive a reply to my last question. Was my previous post and highlighted code not sufficient to explain what I am trying to do?

  18. #18
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Simple copying, but getting stuck in macro

    Thanks but I managed to solve the problem by iteration.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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