+ Reply to Thread
Results 1 to 5 of 5

VBA Macro: Copy range of data to another sheet

  1. #1
    Registered User
    Join Date
    06-26-2015
    Location
    Brazil
    MS-Off Ver
    O365 Win
    Posts
    63

    VBA Macro: Copy range of data to another sheet

    Hi Everyone,

    I am slowly making a new sheet that includes:
    1. A cascading dropdown list done with the help of Dflak here, the cascading dropdown is where I choose the buyer and seller as parent and trader is the person whom I talked to, one company can have multiple traders. (working)
    2. A VBA macro to e-mail PDF through mac Mail, where it will automatically send PDF to my team and also to the buyer (working)
    3. A VBA macro to copy the data (Date, Contract Nr.*, Buyer, Seller, Quality, Price, Volume* and Shipment*)

    Where I am stuck now is on copying the data on the Bold items with *

    We most of the times make sales that are spread throughout different months, and volume can be changed too, so I have:
    • Last Contract number is on cell N5
    • New contract numbers are on range (K18:K31)
    • Volume range(F18:F31)
    • Shipment months range(B18:B31)

    Let's say for instance I am on contract number 10, and I make a new contract that goes from January through March (3 months) and each months has 300 bags. (ranges with data are only 3 columns deep out of 14)

    I have gotten this VBA to copy the data, but how can I make it copy only if there is data, and also include items which not as a range?

    Please Login or Register  to view this content.
    Attached the full sheet in case it helps you help me :D

    Thanks in advance
    Last edited by danwoltrs; 12-23-2015 at 09:14 AM. Reason: new excel sheet

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: VBA Macro: Copy range of data to another sheet

    I need a bit more information on how you get the data to the sales page. I think some of your issues can be uncomplicated if the data (at least rows 1 through 31) were in an Excel table. Then you could run a "pointer" down the column. A table knows how big it is, so the pointer will know where to start and stop.

    I'm still not all that clear what you want to copy from the Sale page to the Historico page. In some cases it looks like it is copying blank cells.

    Also on the sales page, the formulas in column G could be simplified by doing a lookup. What you have seems to work fine. If I have to nest more than two if statements, I go for a lookup. However, that's my personal preference - either way works.

    In the attached, I made a table on the Sale page. I had to hide the header since you have a dropdown value on row 17. Excel tables must have fixed headers. The real headers are on row 18 which is hidden. I also changed the dropdown for the units of measure from the hard coded that you had to read it from a lookup table. If you are using a list validation against a column in a table you can use the following syntax:

    =INDIRECT("TableName[TableColumn]")

    The validation will grow and shrink with the data in the table.

    I also took the if statement out of column G and replaced it with: =IF(B19="","",VLOOKUP($G$17,Table_Units,2,FALSE)*[@Volume]) This looks up the value to multiply. It also means you do not have to recode anything should a new unit of measure come along. Just add the new unit to the table on the Lookups page.

    With this setup I was able to run a pointer, cl, down the first column on Table_Sales. cl is a range. When used with For Each, it starts with the first cell in the range and moves on to the next cell in the range until it reaches the end. That's why I call it a pointer since it points to each cell in order.

    This is where I am confused. it looks like you are copying static data, basically replicating the same row after row. I took a best guess as to what you wanted.

    About the only thing to explain about the code is the use of offset. cl.Offset(0,5).Value means go zero rows down and 5 columns over from the cell where cl is pointing and get its value.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    06-26-2015
    Location
    Brazil
    MS-Off Ver
    O365 Win
    Posts
    63

    Re: VBA Macro: Copy range of data to another sheet

    Hi Dflak, first of all, thank you very much for your help once again, really appreciate it, and merry Christmas!

    Quote Originally Posted by dflak View Post
    I need a bit more information on how you get the data to the sales page. I think some of your issues can be uncomplicated if the data (at least rows 1 through 31) were in an Excel table. Then you could run a "pointer" down the column. A table knows how big it is, so the pointer will know where to start and stop.

    I'm still not all that clear what you want to copy from the Sale page to the Historico page. In some cases it looks like it is copying blank cells.
    I will always manually input:

    Commission: D9 (if buyer pays) and K9 (if seller pays)
    Shipments: range(B18:B31)
    Volume: range(E18:E31)

    What I need to copy:
    Contract nr.: range (I19:I32) Contract number will always be a unique number, it is based on last contract number +1, for now I manually input last contract number on N5, and have a basic formula on cell I18 that is N5+1, and the rest is just above cell +1. Maybe you can come with a smarter way than that, like getting the last value on the Historico table (called it tbl_historico now), column B instead of manually doing it?
    Buyer: D5
    Seller: I5
    Quality: D14
    Price: D15
    Shipments: range(B19:B32)
    Volume: range(E19:E32)
    Commission S: D9
    Commission B: I9

    Quote Originally Posted by dflak View Post
    Also on the sales page, the formulas in column G could be simplified by doing a lookup. What you have seems to work fine. If I have to nest more than two if statements, I go for a lookup. However, that's my personal preference - either way works.

    In the attached, I made a table on the Sale page. I had to hide the header since you have a dropdown value on row 17. Excel tables must have fixed headers. The real headers are on row 18 which is hidden. I also changed the dropdown for the units of measure from the hard coded that you had to read it from a lookup table. If you are using a list validation against a column in a table you can use the following syntax:

    =INDIRECT("TableName[TableColumn]")

    The validation will grow and shrink with the data in the table.

    I also took the if statement out of column G and replaced it with: =IF(B19="","",VLOOKUP($G$17,Table_Units,2,FALSE)*[@Volume]) This looks up the value to multiply. It also means you do not have to recode anything should a new unit of measure come along. Just add the new unit to the table on the Lookups page.
    Thanks, that volume formula worked much better, I went ahead and also changed the validation list to the first column on lookups.

    As of the rest of the VBA code, one thing I noticed is the clearTable, which we won’t need, I am doing this in order to save all the data of all the contracts that we made, just as a control method - I was not able to look yet into the rest of the VBA, but will do that when I can – have to get on with the christmass foods now

    Quote Originally Posted by dflak View Post
    With this setup I was able to run a pointer, cl, down the first column on Table_Sales. cl is a range. When used with For Each, it starts with the first cell in the range and moves on to the next cell in the range until it reaches the end. That's why I call it a pointer since it points to each cell in order.

    This is where I am confused. it looks like you are copying static data, basically replicating the same row after row. I took a best guess as to what you wanted.

    About the only thing to explain about the code is the use of offset. cl.Offset(0,5).Value means go zero rows down and 5 columns over from the cell where cl is pointing and get its value.

    Please Login or Register  to view this content.

    Wish you a merry Christmas DFlaq, thanks for the help once again.

  4. #4
    Registered User
    Join Date
    06-26-2015
    Location
    Brazil
    MS-Off Ver
    O365 Win
    Posts
    63

    Re: VBA Macro: Copy range of data to another sheet

    I have now been testing and trying to get it to work, but the VBA macro you made copies and keeps getting the data on to the same row, I want it to keep all the historic data, in case I need to trace back to a sale I did two months ago for instance.

    So I need it to keep adding rows with all the data, while not creating duplicates, which is why I had the following code:

    Please Login or Register  to view this content.
    This code copies the data and adds it to the table, creating new rows for each contract, it works if it's for one single cell, but I can't seem to figure out how to make it work if there are any data in a range from multiple columns, in case there is a new contract number (if the contract nr. is the same, it will just update the other data)
    Last edited by danwoltrs; 12-28-2015 at 02:29 PM.

  5. #5
    Registered User
    Join Date
    06-26-2015
    Location
    Brazil
    MS-Off Ver
    O365 Win
    Posts
    63

    Re: VBA Macro: Copy range of data to another sheet

    I now made a macro through some research on the internet for the other function I needed on this excel, a button to be pressed when creating a new contract that will get the last contract number so the new contracts will automatically have the correct contract number, plus clearing the data that I will have to manually input:

    Please Login or Register  to view this content.
    Now I really just need help with that previous request

    If you want to check it, here validation with validation and unique names.xls
    Last edited by danwoltrs; 12-28-2015 at 02:31 PM.

+ 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. Replies: 2
    Last Post: 01-22-2014, 05:36 PM
  2. [SOLVED] How to copy data range form one sheet to other sheet with desire Reverse Transpose ?
    By nur2544 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-18-2013, 12:30 PM
  3. Macro to Copy a range from one sheet and sellect where to paste in another sheet
    By Bud Wilkinsonn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-23-2013, 06:49 PM
  4. Macro to copy and paste range form one sheet to next available row in 2nd sheet
    By bajdr47 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-21-2012, 11:47 PM
  5. Replies: 4
    Last Post: 07-24-2012, 07:09 AM
  6. [SOLVED] Search for heading in data sheet and copy range to corresponding sheet in master workbook
    By sans in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-07-2012, 10:02 AM
  7. [SOLVED] Macro: Dynamic creation of sheet based on cell value then copy range to the new sheet
    By BeachRock in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-15-2012, 11:31 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