Excel VBA如果/然后声明与用户窗体

所以我已经做了一些JavaScript编辑,但从那时起已经有一段时间了。 我知道我应该知道如何做到这一点,但解决scheme已经逃脱了我。 所有我想要做的是使一个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 

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

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

table_list_object ,你当前的代码也缺less一个End With (有两个With行,但只有一个End With )和一个显式的table_list_object声明。

你永远不会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 

这里有三个选项。

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

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

  2. 使用Excel Table(VBA中的List Object)的内置行为,其中函数保留在表中。 所以如果你添加一个新行,而不填充一个字段,公式会自动添加给你。

  3. 使用FormulaR1C1和twist,其中按表和列名称引用值。 例如,如果您的表名为Table1而列(在标题中)为“Col1”和“Col2”,则可以将:

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

或更简单:

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

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