所以我想弄清楚如何分开下面的代码,以便:
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 =“Low”,那么它将把它放在“LI Project Database”表中(具有相同的要求)。
当我尝试这样做时,它说我有一堆重复。我是VBA的新手(一天前开始),所以任何指导都会很棒,谢谢!
答案 0 :(得分:0)
这是一种可能的重构(希望)使您的代码具有灵活性和可维护性:
Option Explicit
Private Sub enterButton_Click()
If Not CheckInputs Then Exit Sub 'check for fields to have values
Process GetWs(Me.impactCombobox.Value) ' process data passing the proper worksheet got from GetWs() function
MsgBox "Project Entered Successfully"
ClearUFData 'clear the data
End Sub
Private Sub Process(ws As Worksheet)
Dim iRow As Long
Dim MonthNumber As Byte
Dim ColumnNumber As Long: ColumnNumber = 4
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'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
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 MonthNumber
End With
End Sub
Function CheckInputs() As Boolean
If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
If Not CheckControl(Me.audienceCombobox, "Please select an Audience") Then Exit Function
CheckInputs = True
End Function
Function CheckControl(ctrl As MSForms.Control, errMsg As String) As Boolean
Select Case TypeName(ctrl)
Case "TextBox"
CheckControl = Trim(ctrl.Value) <> ""
Case "ComboBox"
CheckControl = ctrl.ListIndex <> -1
' Case Else
End Select
If CheckControl Then Exit Function
ctrl.SetFocus
MsgBox errMsg
End Function
Function GetWs(impact As String) As Worksheet
Select Case impact
Case "High"
Set GetWs = Worksheets("HI Project Database")
Case "Low"
Set GetWs = Worksheets("LI Project Database")
' Case Else
End Select
End Function
Sub ClearUFData()
Dim i As Integer
'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
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