wootthepants
New Member
- Joined
- Apr 30, 2019
- Messages
- 8
Hello!
I was wondering if someone could help me with the following (please keep in mind that I'm a bit new to VBA):
I have a spreadsheet where I want to create one combobox per cell so the user can type in a keyword selection, pick from the drop down, which will return results and auto populate into other fields. I've pretty much figure out how to do most of this but, since my sheet could possibly have several hundred rows, how do you copy the original combo box multiple times, with the LinkedCell reference automatically changing to the next cell (B3 to B4 etc) without having to do this manually one by one?
I found the following code which is supposed to do this exact thing except it seems to just move the original combo box rather than make copies. It also changes the cell reference to be one more than it should (combobox is hovering over B3 but the LinkedCell is B4).
Essentially, I have a combo box hovering over cell B2 and linked to cell B2. I want the code to copy that combo box, past the copy immediately below the original box (hovering over B3), and change the LinkedCell to B3. Then I want this repeated for however many copies I want to make (I've noted 10 for testing purposes here). Oh and a data validation drop down won't work as I need the combo box to auto fill in real time and to display values from more than one column. Thank you!!
Sub AddFormsComboBoxes()
'assumes 1 combo box named 'Drop Down 1' has
'been placed on the sheet and set up with
'the ListFillRange information so all that is
'needed to do is copy that control and
'change the Link cell address
'
'we can control everything needed for the
'process here
'these describe the LinkedCell address
'for the first/source control
Const sourceControlName = "ComboBox1" ' change as required
Const linkCellCol = "B"
Const firstLinkRow = 2 ' row for original control
'control how many copies to make
Const copiesToMake = 10 ' original + 99 = 100
'you can make this a positive number to
'add spacing between the new controls
'as set to 0 the controls will be
'placed very tightly on the sheet
Const vSpaceBetweenControls = 0
'variables needed to perform the copying and positioning
Dim leftPosition As Single
Dim topPosition As Single
Dim ctlHeight As Single
Dim linkCellRow As Long
Dim LC As Long
ActiveSheet.Shapes.Range(Array(sourceControlName)).Select
leftPosition = Selection.Left
topPosition = Selection.Top
ctlHeight = Selection.Height
linkCellRow = firstLinkRow
Application.ScreenUpdating = False ' speeds up process
Selection.Copy
For LC = 1 To copiesToMake
topPosition = topPosition + ctlHeight + vSpaceBetweenControls
linkCellRow = linkCellRow + 1
ActiveSheet.Paste ' new control becomes selected
With Selection
.Top = topPosition
.Left = leftPosition ' aligned vertically
.LinkedCell = linkCellCol & linkCellRow
End With
Next
End Sub
I was wondering if someone could help me with the following (please keep in mind that I'm a bit new to VBA):
I have a spreadsheet where I want to create one combobox per cell so the user can type in a keyword selection, pick from the drop down, which will return results and auto populate into other fields. I've pretty much figure out how to do most of this but, since my sheet could possibly have several hundred rows, how do you copy the original combo box multiple times, with the LinkedCell reference automatically changing to the next cell (B3 to B4 etc) without having to do this manually one by one?
I found the following code which is supposed to do this exact thing except it seems to just move the original combo box rather than make copies. It also changes the cell reference to be one more than it should (combobox is hovering over B3 but the LinkedCell is B4).
Essentially, I have a combo box hovering over cell B2 and linked to cell B2. I want the code to copy that combo box, past the copy immediately below the original box (hovering over B3), and change the LinkedCell to B3. Then I want this repeated for however many copies I want to make (I've noted 10 for testing purposes here). Oh and a data validation drop down won't work as I need the combo box to auto fill in real time and to display values from more than one column. Thank you!!
Sub AddFormsComboBoxes()
'assumes 1 combo box named 'Drop Down 1' has
'been placed on the sheet and set up with
'the ListFillRange information so all that is
'needed to do is copy that control and
'change the Link cell address
'
'we can control everything needed for the
'process here
'these describe the LinkedCell address
'for the first/source control
Const sourceControlName = "ComboBox1" ' change as required
Const linkCellCol = "B"
Const firstLinkRow = 2 ' row for original control
'control how many copies to make
Const copiesToMake = 10 ' original + 99 = 100
'you can make this a positive number to
'add spacing between the new controls
'as set to 0 the controls will be
'placed very tightly on the sheet
Const vSpaceBetweenControls = 0
'variables needed to perform the copying and positioning
Dim leftPosition As Single
Dim topPosition As Single
Dim ctlHeight As Single
Dim linkCellRow As Long
Dim LC As Long
ActiveSheet.Shapes.Range(Array(sourceControlName)).Select
leftPosition = Selection.Left
topPosition = Selection.Top
ctlHeight = Selection.Height
linkCellRow = firstLinkRow
Application.ScreenUpdating = False ' speeds up process
Selection.Copy
For LC = 1 To copiesToMake
topPosition = topPosition + ctlHeight + vSpaceBetweenControls
linkCellRow = linkCellRow + 1
ActiveSheet.Paste ' new control becomes selected
With Selection
.Top = topPosition
.Left = leftPosition ' aligned vertically
.LinkedCell = linkCellCol & linkCellRow
End With
Next
End Sub