【问题标题】:Excel VBA If/Then Statement with UserForm带有用户窗体的 Excel VBA If/Then 语句
【发布时间】:2013-11-22 06:58:33
【问题描述】:

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

这就是我所拥有的:

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

【问题讨论】:

  • 那么问题是什么?你已经做了.Range(1, 8).Formula = "=F5*E5",例如也许您必须告诉UserForm 中的哪个对象要从中获取值(您也已经这样做了)。
  • I know I should know how to do this but the solution has escaped me. 你做研究了吗?你检查过 msdn 或内置的 Excel 帮助或谷歌 IF 的语法吗?

标签: vba excel


【解决方案1】:

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

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

顺便说一句,您当前的代码还缺少End With(有两行With,但只有一个End With)和table_list_object 的显式声明。

【讨论】:

    【解决方案2】:

    你从来没有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
    

    【讨论】:

    • but don't have that ability yet 那你应该等到你有足够的声望的时候。
    • 我看到你已经编辑了你的帖子 :) 为什么不让它成为一个完整的答案:) 这样如果它被接受或赞成,那么你的声誉也会增加? :)
    【解决方案3】:

    这里有三个选项。

    1. 使用经典的FormulaR1C1,所以在你的代码中它会是:

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

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

    3. 将FormulaR1C1 与twist 一起使用,您可以在其中通过表名和列名引用值。例如,如果您的表名为 Table1 并且列(在标题中)是“Col1”和“Col2”,您可以输入:

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

    或更简单:

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

    我通常选择第二个或第三个选项。

    【讨论】:

    • 谢谢大家现在似乎正在工作。不过新问题。当前代码存在限制,因为当我插入新列时,方程不会针对新行进行调整。因此,如果它在第 3 行和第 4 行插入,我需要相应地调整该数字。
    • 尝试第三种方法 - 它会调整,因为它使用列名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多