You are here:

Excel/VBA to copy ComboBox to Range of cells on multiple sheets

Advertisement


Question
QUESTION: Using Excel 2003

Hi Tom,

Iím using VBA to create a ComboBox (ActixeX Control) and its associated properties in J2 of each sheet in my workbook except for two specific sheets named Actions and Daily_Rejections.

This code works fine but now I want to go through each sheet in the Workbook except for the Actions and Daily_Rejections sheets and copy the ComboBox I created in J2 down to J15.

I tried using this code but it only works on the active sheet can this be modified to work on all the sheets except the Actions and Daily_Rejections sheets ?

Appreciate any assistance you can provide.
Thanks, Anthony

Sub CpyComboBox()

   ActiveSheet.Shapes("ComboBox1").Copy
   Range("J3").Select
   ActiveSheet.Paste
   Range("J4").Select
   ActiveSheet.Paste
   Range("J5").Select
   ActiveSheet.Paste
   Range("J6").Select
   ActiveSheet.Paste
   Range("J7").Select
   ActiveSheet.Paste
   Range("J8").Select
   ActiveSheet.Paste
   Range("J9").Select
   ActiveSheet.Paste
   Range("J10").Select
   ActiveSheet.Paste
   Range("J11").Select
   ActiveSheet.Paste
   Range("J12").Select
   ActiveSheet.Paste
   Range("J13").Select
   ActiveSheet.Paste
   Range("J14").Select
   ActiveSheet.Paste
   Range("J15").Select
   ActiveSheet.Paste
 
End Sub

ANSWER: Anthony,

Sub CpyComboBox()
for each sh in worksheets
 if Trim(lcase(sh.name)) <> "actions" and _
     Trim(lcase(sh.name)) <> "daily_rejections" then
  sh.Activate
  ActiveSheet.Shapes("ComboBox1").Copy
  Range("J3").Select
  ActiveSheet.Paste
  Range("J4").Select
  ActiveSheet.Paste
  Range("J5").Select
  ActiveSheet.Paste
  Range("J6").Select
  ActiveSheet.Paste
  Range("J7").Select
  ActiveSheet.Paste
  Range("J8").Select
  ActiveSheet.Paste
  Range("J9").Select
  ActiveSheet.Paste
  Range("J10").Select
  ActiveSheet.Paste
  Range("J11").Select
  ActiveSheet.Paste
  Range("J12").Select
  ActiveSheet.Paste
  Range("J13").Select
  ActiveSheet.Paste
  Range("J14").Select
  ActiveSheet.Paste
  Range("J15").Select
  ActiveSheet.Paste
 End if
Next
End Sub

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Hi Tom,
Your answer works great.   I have a follow-up question.
Is it possible to copy the ComboBox and Paste it to a range of cells instead of each cell one at a time?  
Something like
ActiveSheet.Shapes("ComboBox1").Copy  
Range("J3:J15").Select
ActiveSheet.Paste

I used the macro recorder to try and paste the ComboBox to a range but it only pasted the ComboBox to J3.

Sub MacroRecorder()
   ActiveSheet.Shapes("ComboBox1").Select
   Selection.Copy
   Range("J3:J15").Select
   ActiveSheet.Paste
End Sub
Thanks, Anthony

Answer
Anthony,

You are not really copying it to the cells.  You are copying it to the object/shape layer above the selected cell.  So even if multiple cells are selected, it pastes it using the location up the upper left cell in the selection.  Thus the answer is no.

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.