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

时间:2018-01-30 19:12:53

标签: excel forms userform

我正在尝试对用户表单进行编码,以便在与指定值关联的行中添加输入的值。我希望表单显示两个文本框“任务名称”和“任务数量”。

然后我希望excel搜索使用输入框“任务列”(通常为a或B)指定的列,然后将用户表单“任务数量”中的值粘贴到另一个输入框“Unit”指定的列中列“

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

1 个答案:

答案 0 :(得分:0)

为什么使用替换?你能不能只设置值:

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