用户表单中的一些错误,用于将数据添加到多个工作表/表中

时间:2017-02-16 00:44:26

标签: excel vba userform

你能帮我解决这些错误吗? 代码运行但是将数据放在错误的位置,我在下面的评论中将注释放在所有工作表上... 在添加userform值之前,我需要检查重复的名称 这是我的用户表单代码:

Private Sub cmdAddVol_Click()

    Dim lRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("VOLUNTEERS")
    Dim wsArray As Worksheet

    For Each wsArray In Sheets(Array("VOLUNTEERS", "OCTOBER", "NOVEMBER", "DECEMBER", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "FY TOTALS"))
    With wsArray

    'first clear all filters *(this is not working)*
        'This removes any filtering in order to display all of the data but it does not remove the filter arrows
        If .AutoFilterMode Then
            If .FilterMode Then
                .ShowAllData
            End If
        Else
            If .FilterMode Then
                .ShowAllData
            End If
        End If

        'find  first empty row in database
    lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

          'check for a Name
    If Trim(Me.txtLastName.Value) = "" Then
      Me.txtFirstName.SetFocus
      MsgBox "Please enter First and Last Name"
      Exit Sub
    End If

         'validate name is not in database
         *'need code for error duplicate check*

           'copy the data to the database in all sheets(14)
           *' this is only working for VOLUNTEERS sheet, all others are not putting row in table end, but in 2 rows below table*
      .Cells(lRow, 1).Value = Me.cbStatus.Value
      .Cells(lRow, 2).Value = Me.txtLastName.Value
      .Cells(lRow, 3).Value = Me.txtFirstName.Value
    End With
    Next

     'copy only values below in VOLUNTEERS sheet col 4 & 5 at newROw
     *'this in not working--its putting data in next row after 1row* 
    With ws
       .Cells(lRow, 4).Value = Me.cbVolType.Value
       .Cells(lRow, 5).Value = Me.txtDateStart.Value

    'clear the data in box
    Me.txtFirstName.Value = ""
    Me.txtLastName.Value = ""
    Me.cbStatus.Value = ""
    Me.txtFirstName.SetFocus
    End With
    End Sub

    Private Sub cmdClose_Click()
        Unload Me
    End Sub

    Private Sub UserForm_Initialize()
    Dim cStatus As Range
    Dim cType As Range
    Dim ws As Worksheet
    Set ws = Worksheets("Lists")

    For Each cStatus In ws.Range("lst_Status")
      With Me.cbStatus
        .AddItem cStatus.Value
    ''.List(.ListCount - 1, 1) = cStatus.Offset(0, 1).Value
      End With
    Next cStatus

    For Each cType In ws.Range("lst_Type")
      With Me.cbVolType
        .AddItem cType.Value
      End With
    Next cType

    Me.txtDateStart.Value = Format(Date, "Medium Date")

    Me.cbStatus.SetFocus

    End Sub

    Private Sub UserForm_QueryClose(Cancel As Integer, _
      CloseMode As Integer)
      If CloseMode = vbFormControlMenu Then
        Cancel = True
        MsgBox "Please use the Close Form button!"
      End If
    End Sub

0 个答案:

没有答案