使用UserForm的Excel VBA If / Then语句

时间:2013-11-21 16:25:45

标签: excel vba excel-vba

所以我之前做过一些Javascript编辑,但从那以后它已经有一段时间了。我知道我应该知道如何做到这一点,但解决方案已经逃过了我。我要做的就是创建一个ElseIf命令,以便根据UserForm中的值将某些公式放入需要它们的单元格中。

这就是我所拥有的:

Private Sub addItem_Click()
Dim the_sheet As Worksheet
Dim table_object_row As ListRow
Set the_sheet = Sheets("NewOrder")

'find first empty row in database   
Set table_list_object = the_sheet.ListObjects(1)
Set table_object_row = table_list_object.ListRows.Add


 'check for a part number

If Trim(Me.txtItem.Value) = "" Then
  Me.txtItem.SetFocus
  MsgBox "Please enter an item"
  End If

 If Trim(Me.txtPerc.Value) = "" Then
'copy the data to the database
'use protect and unprotect lines,
'     with your password
'     if worksheet is protected
With table_object_row
'  .Unprotect Password:="password"

  .Range(1, 1).Value = Me.txtItem.Value
  .Range(1, 2).Value = Me.txtSKU.Value
  .Range(1, 3).Value = Me.txtPrice.Value
  .Range(1, 4).Value = Me.txtPerc.Value
  .Range(1, 5).Value = Me.txtAdjust.Value
  .Range(1, 6).Value = Me.txtQTY.Value
  .Range(1, 8).Formula = "=F*E"

 End With

 'clear the data
Me.txtItem.Value = ""
Me.txtSKU.Value = ""
Me.txtPrice.Value = ""
Me.txtPerc.Value = ""
Me.txtAdjust.Value = ""
Me.txtQTY.Value = ""
Me.txtItem.SetFocus

  Else
  If Trim(Me.txtAdjust.Value) = "" Then

  With table_object_row
  .Range(1, 1).Value = Me.txtItem.Value
  .Range(1, 2).Value = Me.txtSKU.Value
  .Range(1, 3).Value = Me.txtPrice.Value
  .Range(1, 4).Value = Me.txtPerc.Value
  .Range(1, 5).Formula = "=(C*(1-D))"
  .Range(1, 6).Value = Me.txtQTY.Value
  .Range(1, 8).Formula = "=F*E"




'  .Protect Password:="password"
End With


'clear the data
Me.txtItem.Value = ""
Me.txtSKU.Value = ""
Me.txtPrice.Value = ""
Me.txtPerc.Value = ""
Me.txtAdjust.Value = ""
Me.txtQTY.Value = ""
Me.txtItem.SetFocus
End If
End If

End Sub

3 个答案:

答案 0 :(得分:1)

您需要使用嵌套的If语句:

Else
  If Trim(Me.txtPerc.Value) = "" Then
    ' code...
  End If
End If

顺便说一句,您当前的代码也缺少End With(有两条With行但只有一条End With)和table_list_object的明确声明。< / p>

答案 1 :(得分:1)

你永远不会End If并且只有一个End With(希望我能对此发表评论,但还没有这个能力)。

If Trim(Me.txtItem.Value) = "" Then
  Me.txtItem.SetFocus
  MsgBox "Please enter an item"


ElseIf Trim(Me.txtPerc.Value) = "" Then
    'copy the data to the database
    'use protect and unprotect lines,
    '     with your password
    '     if worksheet is protected
    With table_object_row
    '  .Unprotect Password:="password"

      .Range(1, 1).Value = Me.txtItem.Value
      .Range(1, 2).Value = Me.txtSKU.Value
      .Range(1, 3).Value = Me.txtPrice.Value
      .Range(1, 4).Value = Me.txtPerc.Value
      .Range(1, 5).Value = Me.txtAdjust.Value
      .Range(1, 6).Value = Me.txtQTY.Value
      .Range(1, 8).Formula = "=F5*E5"
    End With
End If

If Trim(Me.txtAdjust.Value) = "" Then

    With table_object_row
        .Range(1, 1).Value = Me.txtItem.Value
        .Range(1, 2).Value = Me.txtSKU.Value
        .Range(1, 3).Value = Me.txtPrice.Value
        .Range(1, 4).Value = Me.txtPerc.Value
        .Range(1, 5).Formula = "=(C5*(1-D5))"
        .Range(1, 6).Value = Me.txtQTY.Value
        .Range(1, 8).Formula = "=F5*E5"

        '  .Protect Password:="password"
    End With
End If

答案 2 :(得分:0)

这里有三个选择。

  1. 使用经典FormulaR1C1,因此在您的代码中将是:

    .Range(1,8).FormulaR1C1 =“= RC [-1] * RC [-2]”

  2. 使用Excel表(VBA中的列表对象)的内置行为,其中函数保留在表中。因此,如果您添加新行并且不填充字段,则会自动为您添加公式。

  3. 使用带有扭曲的FormulaR1C1,您可以按表名和列名引用值。例如,如果您的表名为Table1,而列(位于标题中)为“Col1”和“Col2”,则可以输入:

    .Range(1,8).FormulaR1C1 =“= Table1 [@ Col1] * Table1 [@ Col2]”

  4. 或更简单:

    .Range(1, 8).FormulaR1C1= "=[@Col1]*[@Col2]" 
    

    我通常选择第二种或第三种选择。