2016-06-21 57 views
0

所以我试图找出如何将下面的代码分离,使得:如果和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的新手(一天前开始),所以任何指导都会很棒,谢谢!

+0

你只是复制了别人的整个代码me.impactCombobox.Value =“低”?如果是这样,您可能会遇到一个问题,即无法在同一个子例程中对相同的变量进行两次调整。 –

+0

是的,这是我在做什么,将解决方案是删除变量或我必须重命名他们,然后更改上面的代码中的变量匹配? – adrenom

+0

你实际上只能删除DIM语句(只有一次DIM),但我的建议是将变量名称更改为更具描述性的术语,如wsLow和wsHigh以及DIM。由你决定! –

回答

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 
+0

这是一组非常漂亮的代码,非常感谢。我已经实现了它,并将其用作正确声明的参考! – adrenom

+0

不客气。请考虑以下编码逻辑问题:1)分离特定任务并将其包装为适当的功能或子2)仅在特定任务需要的地方声明变量3)仅在真正需要时才携带任务 – user3598756

+0

因此,我还添加了另一个名为lengthListbox2的列表框,它是前一个lengthListbox的精确副本,但它是2017年,列是从16开始的,这是否工作? http://pastebin.com/FsPq2i5t - 似乎没有工作到目前为止,不知道我会怎么去做另一个复制 – adrenom