根据 2 个组合框填充文本框

populate a textbox based on 2 comboboxes

我的用户表单中有两个 "combo boxes" 和一个 "txtbox",在工作簿 "sheet1" 中,我在 A 列上有名称,B 列上有月份,C 到 N 列是一月。到 12 月,其中包含每个 name/specific 个月

的生产时间
-cboName
-cboMonth
-txtHours

我使用下面的代码来填充 txtHours

Private Sub cboName_Change()
    Dim EName As String
    Dim Row, Col As Integer
    EName = Me.cboName.Text        
    If EName <> "" Then
        With Application.WorksheetFunction
           Row = .Match(EName, Sheets("sheet1").Range("A2:A100"), 0)  
           GetMonthNum (Me.cboMonth.Text)
           txtShiftHours.Value = Sheets("sheet1").Cells(Row + 1, Col + 3)
        End With
    End If
End Sub

Private Sub GetMonthNum(Month As String)

    Select Case Month
        Case Jan
            Col = 3
        Case Feb
            Col = 4
        Case Mar
            Col = 5
        Case Apr
            Col = 6
        Case May
            Col = 7
        Case June
            Col = 8
        Case July
            Col = 9
        Case Aug
            Col = 10
        Case Sept
            Col = 11
        Case Oct
            Col = 12
        Case Nov
            Col = 13
        Case Dec
            Col = 14
        End Select
        End Sub

但无论 cboMonth 上的月份选择如何,txtProduct 都会填充第 3 列,因为这一行

txtShiftHours.Value = Sheets("sheet1").Cells(Row + 1, Col + 3)

请帮帮我 谢谢

您有几个问题:

  • 您的 Case 语句正在检查字符串变量 Month 与未定义变量(例如 JanFeb 等)的值。这应该是针对未定义的变量进行检查字符串文字,例如 "Jan""Feb"
  • 在您的 GetMonthNum 子例程中,您正在为未定义的变量赋值 Col
  • 在您的 cboName_Change 子例程中,您使用的变量 Col 从未被赋值,因此它的默认值为零。

您还有一些小问题,这些问题不会阻止您的代码正常工作,但可能会导致后续问题:

  • 您使用了几个变量名称(RowMonth),它们与 VBA 中的内置函数/属性相同。这通常是一个非常糟糕的主意。
  • 您将 Row 声明为 Variant,尽管将 Col 声明为 Integer
  • 最好将行和列变量定义为 Long 而不是 Integer - Excel 中的最大行数现在是 1048576,但是 Integer 最多只能容纳 65536 个数字。

始终将 Option Explicit 语句作为每个代码模块的第一行包含在内也是一个好主意。这告诉编译器检查是否已声明所有变量,从而防止许多错别字和尝试在一个子例程中使用对另一个子例程来说是本地变量的尝试。

我已经重构了您的代码,希望它现在应该可以工作了。

Option Explicit

Private Sub cboName_Change()
    Dim EName As String
    Dim RowNum As Long, ColNum As Long
    EName = Me.cboName.Text        
    If EName <> "" Then
        With Application.WorksheetFunction
           RowNum = .Match(EName, Sheets("sheet1").Range("A2:A100"), 0)  
           ColNum = GetMonthNum(Me.cboMonth.Text) + 2
           txtShiftHours.Value = Sheets("sheet1").Cells(RowNum + 1, ColNum)
        End With
    End If
End Sub

Private Function GetMonthNum(Mth As String) As Long
    Select Case Mth
        Case "Jan":  GetMonthNum = 1
        Case "Feb":  GetMonthNum = 2
        Case "Mar":  GetMonthNum = 3
        Case "Apr":  GetMonthNum = 4
        Case "May":  GetMonthNum = 5
        Case "June": GetMonthNum = 6
        Case "July": GetMonthNum = 7
        Case "Aug":  GetMonthNum = 8
        Case "Sept": GetMonthNum = 9
        Case "Oct":  GetMonthNum = 10
        Case "Nov":  GetMonthNum = 11
        Case "Dec":  GetMonthNum = 12
    End Select
End Function

您可以使用 Excel 的一些 Date & Time 内置函数,将整个 Private Sub GetMonthNum(Month As String) 替换为下面的第 1 行代码:

ColNum = Month(DateValue("1/" & Me.cboMonth.Text & "/2017")) + 2

说明:因为您的 cboMonth 组合框具有 mmm 月份格式的月份字符串。如果您 select "Feb",那么当您到达此部分时 ("1/" & Me.cboMonth.Text & "/2017") 您将得到“1/Feb/2017”。

前面加上DateValue得到1/Feb/2017,前面加上Month得到2.