Working in Excel, I have 10 worksheets and each of them has the same ActiveX Combo Box. The Combo Box lists a subset of other worksheets within the workbook.
Once a worksheet is selected in the Combo Box, one can then click a button next to it that runs a macro that fills in data from the selected sheet onto the active sheet.
My issue is that there is currently a single module that initializes the worksheet name by refering to the Combo Box name, which is different on every sheet (i.e. ComboBox1 to Combox10).
What I'm doing right now is changing the Combo Box reference each time I run the macro on each of the 10 sheets that has this Combo Box. Unfortunately, I can't expect my users to update the macro each time they use it.
Is there a way to edit this Combo Box reference in the module using a wildcard, or is there a different way to go about this issue?
I've tried adding the public sub into the private sub for each worksheet, but then I come across the 400 error. I've also tried throwing an * after ComboBox, (i.e. ComboBox*), but that clearly isn't correct either.
Sub FillTable()
Dim lastrow, lastrow2, i As Integer
Dim Searchfor, j, candnumArr, MarksArr As Variant
Dim wsName As String
'Worksheet selected in Combobox which differs across worksheets
' This is the reference that needs to be changed from 1 to 2/3/etc
wsName = ActiveSheet.ComboBox1.Text
With Sheets(wsName)
lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
candnumArr = Range(.Cells(6, 2), .Cells(lastrow, 14))
End With
'Arrays to be filled sheet
With ActiveSheet
lastrow2 = .Cells(Rows.Count, "H").End(xlUp).Row
' load variant array with search variables
searcharr = Range(.Cells(6, 8), .Cells(lastrow2, 8))
' define an output aray
PartNumArr = Range(.Cells(6, 13), .Cells(lastrow2, 13))
MarksArr = Range(.Cells(6, 17), .Cells(lastrow2, 26))
End With
On Error Resume Next
For i = 1 To lastrow2
For j = 1 To lastrow
Searchfor = searcharr(i, 1)
If candnumArr(j, 1) = Searchfor Then
For kk = 1 To 13
PartNumArr(i, kk - 1) = candnumArr(j, kk)
MarksArr(i, kk - 2) = candnumArr(j, kk)
Next kk
Exit For
End If
Next j
Next i
' writeout the output arrays
With ActiveSheet
Range(.Cells(6, 13), .Cells(lastrow2, 13)) = PartNumArr
Range(.Cells(6, 17), .Cells(lastrow2, 26)) = MarksArr
End With
End Sub
While the code works for the first sheet and using its Combo Box, I'm at a loss at how to change it so it works across all 10 sheets that have this Combo Box.
wsName = ActiveSheet.ComboBox1.Text
line to change to something likewsName = ActiveSheet("ComboBox"&"#").Text
so that the reference is dynamic. That specific line doesn't work, but is something is there a way to make something else work? – gbear84