使用 Excel 用户表单输入条件值

Enter Condition Value using Excel User Form

我正在尝试对用户表单进行编码,以在与指定值关联的行中添加输入的值。我想要一个带有两个文本框 "Task Name" 和 "Task Quantity" 的表单。

然后我希望 excel 搜索用输入框 "Task Column"(通常是 a 或 B)指定的列,然后将用户表单 "Task Quantity" 中的值粘贴到用另一个输入框指定的列 "Unit Column"

User Form Image

到目前为止,这是我的代码:

Public Sub UserForm_Initialize()
    Dim tskCol As String
        tskCol = Application.InputBox("Enter Column Letter for Task Names", , , , , , , 2)
    Dim unitCol As String
        unitCol = Application.InputBox("Enter Column for Number of Units", , , , , , , 2)
End Sub

Private Sub cmdAdd_Click()
    Dim LastRow As Long
    Dim i As Long
    LastRow = Range("tskCol" & Rows.Count).End(xlUp).Row
        'Copy input values to sheet.
    For i = 2 To LastRow
        If Range("tskCol" & i).Value = Me.txtTask Then
            Range("unitCol" & i).Replace What:="", Replacement:=Me.txtQuantity, LookAt:=xlPart _
            , SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        End If
    Next i

    'Clear input controls.
    Me.txtTask.Value = ""
    Me.txtQuantity.Value = ""


End Sub

已编辑代码

    Option Explicit
Public tskCol As String
Public unitCol As String
Sub UserForm_Initialize()
    tskCol = Application.InputBox("Enter Column Letter for Task Names", , , , , , , 2)
    unitCol = Application.InputBox("Enter Column for Number of Units", , , , , , , 2)
End Sub

Private Sub cmdAdd_Click()
    Dim LastRow As Long
    Dim i As Long
    LastRow = ActiveSheet.Range(tskCol & Rows.Count).End(xlUp).Row
        'Copy input values to sheet.
    For i = 2 To LastRow
        If ActiveSheet.Range(tskCol & i).Value = Me.txtTask Then
            ActiveSheet.Range(unitCol & i).Value = Me.txtQuantity.Value
        End If
    Next i

    'Clear input controls.
    Me.txtTask.Value = ""
    Me.txtQuantity.Value = ""


End Sub

为什么要使用 Replace?你能不能只设置值:

If cStr(ActiveSheet.Range(tskCol & i).Value) = cStr(Me.txtTask) Then
    ActiveSheet.Range(unitCol & i).Value = Me.txtQuantity.Value
End If

{EDIT} 刚刚注意到您已经在 Sub 中定义了变量 tskCol 和 unitCol。一旦 Sub 结束,这些内容就会从内存中删除。您应该在代码模块的开头,在 Sub 外部初始化它们。

Option Explicit 'This line, which you can enable by default, means that any variable you have not defined will result in an error.  ALWAYS INCLUDE IT
Private tskCol As String 'Private means only code in this module can see/ use it
Private unitCol As String 'Any Sub or Function can in this module can now use these variables

{编辑:完整代码}

Option Explicit
Private tskCol As String
Private unitCol AS String

Public Sub UserForm_Initialize()
    tskCol = Application.InputBox("Enter Column Letter for Task Names", Type:=2)
    unitCol = Application.InputBox("Enter Column for Number of Units", Type:=2)
End Sub

Private Sub cmdAdd_Click()
    Dim LastRow As Long, i As Long

    LastRow = ActiveSheet.Range(tskCol & Rows.Count).End(xlUp).Row
        'Copy input values to sheet.
    For i = 2 To LastRow
        If cStr(ActiveSheet.Range(tskCol & i).Value) = cStr(Me.txtTask.Value) Then
            ActiveSheet.Range(unitCol & i).Value = Me.txtQuantity.Value
        End If
    Next i

    'Clear input controls.
    Me.txtTask.Value = ""
    Me.txtQuantity.Value = ""
End Sub