+ Reply to Thread
Results 1 to 14 of 14

Macro to copy and paste a range of cells based on ComboBox selection

  1. #1
    Registered User
    Join Date
    01-11-2015
    Location
    Leeds
    MS-Off Ver
    2010
    Posts
    35

    Macro to copy and paste a range of cells based on ComboBox selection

    Hi,
    I have a workbook with a named range called "Client1" which has 2 columns - column 1 is the clients name and column 2 is just a number. This named range is listed in a combobox in Sheet1 and upon what client is selected I would like to copy cell range B13:H39 (Sheet1) and paste in Sheet2 at the relevant section in row7 based on the cell number to the left of clients name in sheet2 via a command button. I thought I may be able to work something out myself but I am getting nowhere. Basically I do invoicing in excel and have several customers that require their invoices to be phased and most of these invoices are just a repeat of the last invoices but with small changes. The object is to copy just the main details of an invoice to another sheet then, as required copy the invoice details back to my template without leaving Sheet1. Below is the last code I tried and I attach a workbook which may help clarify things a little. Any ideas will be most welcome.
    Attached Files Attached Files
    Last edited by Paul103; 02-03-2015 at 05:24 AM.

  2. #2
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    First, delete the code you currently have in Module1. Then in Sheet1, paste the following code.
    Please Login or Register  to view this content.
    Assign your Recall Invoice button to the RecallPhase macro, and the Memorize button to the CopyPhase macro.

    You'll also need to deal with another problem. Sheet2 has a ComboBox1_Change event behind it, which apparently gets triggered every time a value is changed anywhere in the workbook (even on another sheet). So when the CopyPhase or RecallPhase macros hit the lines that copy the data, it triggers the event, which blows up because Sheet2 isn't the current sheet. This apparently occurs because of your dynamic range (Client1) ListFillRange for your ComboBoxes. Any value change causes the dynamic range to be recalculated, which reloads the combobox, triggering the event. I learned of this from the following: http://www.mrexcel.com/forum/excel-q...worksheet.html. Perhaps you can fix it by changing OFFSET to INDEX as was suggested, or by making it a normal named range instead of dynamic. If you'd rather not, you can update the Change event to ignore the error with the following code:
    Please Login or Register  to view this content.
    I hope that gets you what you need.
    Acts 4:12
    Salvation is found in no one else, for there is no other name under heaven given to mankind by which we must be saved.

  3. #3
    Registered User
    Join Date
    01-11-2015
    Location
    Leeds
    MS-Off Ver
    2010
    Posts
    35

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    natefarm, you are a genius! Your code works perfectly after making your suggested changes. I altered the dynamic named range to a normal named range and changed the event on the ComboBoxes from change to click and this has solved the problem with triggering. Many thanks for your valued time and effort in helping me with this project.

  4. #4
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    Excellent! You may also want to consider just putting each client on a separate tab. In my opinion, it would simplify future maintenance, and you could go to each client by just selecting the tab, rather than going to sheet2 and selecting it from the combo box. It wouldn't take much code changing to accommodate.

  5. #5
    Registered User
    Join Date
    01-11-2015
    Location
    Leeds
    MS-Off Ver
    2010
    Posts
    35

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    natefarm, I will give your suggestions some thought but I have my reasons for not creating tabs for each client. In the real world all my accounts, stock and despatch are controlled in the one workbook and there are already too many tabs for my liking, but once again thanks for your help.

  6. #6
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    It's certainly up to you. A separate workbook for clients is also an option.

  7. #7
    Registered User
    Join Date
    01-11-2015
    Location
    Leeds
    MS-Off Ver
    2010
    Posts
    35

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    Natefarm, now I have copied your code to my main workbook something strange is happening in that the macros are not showing up so I cannot assign my buttons. I have not put any code in for the comboboxes as previous and they only work off a normal named range. Now having gone back to the workbook I uploaded when I look in at the macros they are shown as Sheet1.RecallPhase and Sheet1.CopyPhase. The macros work fine but when I close the uploaded workbook it becomes unresponsive - any ideas?

  8. #8
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    First, delete the code you currently have in Module1. Then in Sheet1, paste the following code.
    Did you follow the above step?

  9. #9
    Registered User
    Join Date
    01-11-2015
    Location
    Leeds
    MS-Off Ver
    2010
    Posts
    35

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    natefarm, this is exactly what I did and the order I did it in.
    1) Deleted dynamic named range Client1 and replaced with normal named range
    2) Removed coding for combobox2 as not required
    3) Changed event of combobox1 from change to click
    4) Removed all code in module1
    5) Copied all your code into sheet1
    6) Went to assign buttons but no code showing under macros
    7) Removed Private from 2 Subs
    8) Checked macros again and this time they showed but not in the usual way i.e. (Sheet1).RecallPhase and (Sheet1).CopyPhase.
    9) Assigned buttons and both macros worked beautifully

    The workbook did take a little while to close but it was late last night and so did not think to much of it but today when I tried it again my work computer just hung for ages before closing. Anyway I put the code in on my accounts workbook and once again the macros do not show whether private is
    used or not.

  10. #10
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    Definitely shouldn't have been Private. Sorry about that. I had discovered that, but apparently hadn't corrected it on code I provided.

    The (Sheet1) appearance you described in step 8 is correct. I started from scratch on your attachment and everything seems to work, so there must be something else going on in your workbook. Removing Private should have taken care of missing macros.

    Is your workbook especially large that might cause slow saving/opening? I've had cases where there are excessive blank rows or columns that need to be deleted. Go to each sheet and do a Ctrl-End to see where it thinks the sheet boundaries are. Otherwise, if you haven't completely closed excel or rebooted since experiencing the problems, that can often solve goofy problems that may build up when testing macros.

  11. #11
    Registered User
    Join Date
    01-11-2015
    Location
    Leeds
    MS-Off Ver
    2010
    Posts
    35

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    natefarm, I've sorted it all out and the macros work perfectly but what I had finished up doing seems to go against VBA logic. Firstly I removed all the macros in sheet1, closed everything down and rebooted my computer. After pasting in your macros again and removing the 'Private' I assigned the buttons but neither macro would run and a message kept coming up 'Missing Object' with no error code. The debugger kept highlighting Sheet2 so I renamed sheet2 (Mem) and have not had a problem since. Weard!

  12. #12
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    I don't know if it has anything to do with it, but I noticed that you used the syntax of Sheet2.Rows(6).Find. I'd never seen that before and have always used Sheets("Sheet2").Rows(6).Find, but it seemed to work either way on your sample.

  13. #13
    Registered User
    Join Date
    01-04-2015
    Location
    india
    MS-Off Ver
    2003
    Posts
    1

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    I want copy certain cells and save them as txt file name current date and time

  14. #14
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Macro to copy and paste a range of cells based on ComboBox selection

    madhuramji, please don't hijack someone else's post. You need to start a new one of your own.

+ 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. Change cells in row based on row of Combobox selection
    By SHUTTEHFACE in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-09-2014, 03:58 PM
  2. [SOLVED] Delete Row based on combobox selection deleting entire range
    By SHUTTEHFACE in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-19-2014, 09:40 AM
  3. VBA Code to copy and paste a row depending on selection made in a combobox
    By ssu in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-20-2013, 05:37 AM
  4. Copy & Paste Range of Cells Based On Combo Box Value
    By medihx in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-30-2011, 04:43 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