所以我试图找出如何将下面的代码分离,使得:如果和else条件分成不同的表
if Me.impactCombobox.Value = "High" then
Private Sub enterButton_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("HI Project Database")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).row + 1
'check for fields to have values
If Trim(Me.nameTextbox.Value) = "" Then
Me.nameTextbox.SetFocus
MsgBox "Please enter your name"
Exit Sub
End If
If Trim(Me.projectTextbox.Value) = "" Then
Me.projectTextbox.SetFocus
MsgBox "Please enter a Project Name"
Exit Sub
End If
If Trim(Me.audienceCombobox.ListIndex) = -1 Then
Me.audienceCombobox.SetFocus
MsgBox "Please select an Audience"
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
'.Unprotect Password:="password"
.Cells(iRow, 1).Value = Me.nameTextbox.Value
.Cells(iRow, 2).Value = Me.projectTextbox.Value
.Cells(iRow, 3).Value = Me.audienceCombobox.Value
.Cells(iRow, 16).Value = Me.impactCombobox.Value
Dim MonthNumber As Byte
Dim ColumnNumber As Integer: ColumnNumber = 4
For MonthNumber = 0 To 11
If audienceListbox.Selected(MonthNumber) Then
.Cells(iRow, ColumnNumber).Value = "Yes"
Else
.Cells(iRow, ColumnNumber).Value = "No"
End If
'Increase the column Index for each time through the loop
ColumnNumber = ColumnNumber + 1
Next
End With
MsgBox "Project Entered Successfully"
'clear the data
Me.nameTextbox.Value = ""
Me.projectTextbox.Value = ""
Me.nameTextbox.SetFocus
Me.audienceCombobox.Value = Null
Me.impactCombobox.Value = Null
Me.q1Checkbox.Value = False
Me.q2Checkbox.Value = False
Me.q3Checkbox.Value = False
Me.q4Checkbox.Value = False
Dim i As Integer
For i = audienceListbox.ListCount - 1 To 0 Step -1
If audienceListbox.Selected(i) = True Then
audienceListbox.Selected(i) = False
End If
Next i
End Sub
或
否则,如果me.impactCombobox.Value = “低”,然后将它放入“LI项目数据库”表(具有相同的要求)。
当我试图这样做,它说我有一堆重复。我是VBA的新手(一天前开始),所以任何指导都会很棒,谢谢!
你只是复制了别人的整个代码me.impactCombobox.Value =“低”?如果是这样,您可能会遇到一个问题,即无法在同一个子例程中对相同的变量进行两次调整。 –
是的,这是我在做什么,将解决方案是删除变量或我必须重命名他们,然后更改上面的代码中的变量匹配? – adrenom
你实际上只能删除DIM语句(只有一次DIM),但我的建议是将变量名称更改为更具描述性的术语,如wsLow和wsHigh以及DIM。由你决定! –