【发布时间】: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的语法吗?