this is the first I've worked with Excel's VBA UserForm and I'm experiencing an issue i don't understand. currently I'm receiving a 1004 error coming from ComboBox1_Change()
when ComboBox1
's selection is not the same as the activesheet
that I'm looking at.
Is there an easy way to fix that? I need to be able to select any sheet without directly looking at it. ComboBox2
's selection is based on the column headings of the sheet picked in ComboBox1
. Any help would be greatly appreciated! Below is the code as I've developed thus far.
Private m_Cancelled As Boolean
Public Property Get Cancelled() As Variant
Cancelled = m_Cancelled
End Property
Private Sub ComboBox1_Change()
ComboBox2.List = WorksheetFunction.Transpose(Worksheets(CStr(ComboBox1.Value)).Range(Cells(1, 1), Cells(1, GetColSize)).Value)
End Sub
Private Sub ComboBox2_Change()
ComboBox3.List = WorksheetFunction.Transpose(Worksheets(CStr(ComboBox1.Value)).Range(Cells(1, 1), Cells(1, GetColSize)).Value)
End Sub
Private Sub CommandButton1_Click()
Hide
End Sub
Private Sub CommandButton2_Click()
' Hide the Userform and set cancelled to true
Hide
m_Cancelled = True
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
With UserForm_1
.Width = Application.Width * 0.24
.Height = Application.Height * 0.47
End With
ComboBox1.Clear
'Initialize ComboBox1
Dim WB As Workbook: Set WB = ThisWorkbook
Dim wCount As Long: wCount = WB.Worksheets.Count
Dim wsNames() As String: ReDim wsNames(1 To wCount)
Dim WS As Worksheet, w As Long
For Each WS In WB.Worksheets
If WS.Visible = xlSheetVisible Then
w = w + 1
wsNames(w) = WS.Name
End If
Next WS
If w < wCount Then ReDim Preserve wsNames(1 To w)
ComboBox1.List = wsNames
End Sub
Private Sub UserForm_Activate()
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer _
, CloseMode As Integer)
' Prevent the form being unloaded
If CloseMode = vbFormControlMenu Then Cancel = True
' Hide the Userform and set cancelled to true
Hide
m_Cancelled = True
End Sub
Function GetColSize() As Integer
Dim LastColumn As Integer
LastColumn = Sheets(ComboBox1.Value).Rows(1).Columns(Sheets(ComboBox1.Value).Columns.Count).End(xlToLeft).Column
' Debug.Print LastColumn; " Last Column"
GetColSize = LastColumn
End Function
Function GetRowSize() As Variant
Dim LastRow As Variant
LastRow = Sheets(ComboBox1.Value).Cells(Rows.Count, GetBox2Col).End(xlUp).Row
' Debug.Print LastRow; " Last Row"
GetRowSize = LastRow
End Function
Function GetComboBox1() As String
GetComboBox1 = ComboBox1.Value
End Function
Function GetComboBox2() As String
GetComboBox2 = ComboBox2.Value
Debug.Print ComboBox2.Value; " is in column "; GetBox2Col
End Function
Function GetBox2Col() As Integer
Dim i As Integer
For i = 1 To GetColSize
If Cells(1, i) = ComboBox2.Value Then
GetBox2Col = i
Exit For
End If
Next i
End Function
Function GetComboBox3() As String
GetComboBox3 = ComboBox3.Value
Debug.Print ComboBox3.Value; " is in column "; GetBox3Col
End Function
Function GetBox3Col() As Integer
Dim i As Integer
For i = 1 To GetColSize
If Cells(1, i) = ComboBox3.Value Then
GetBox3Col = i
Exit For
End If
Next i
End Function