userform

时间:2016-01-29 05:34:32

标签: excel vba excel-vba

基本上,模块Onboarding正在询问我想要更新的跟踪器的路径。我正在更新详细信息 跟踪器的sheet1。 我将userform'OnboardingForm'中的字段值设置为空白(以便上次输入的值为 这次打开表格时,表格不可见。 现在我打开“OnboardingForm”表单并在后续字段中输入值。 我在我的用户表单'OnboardingForm'中放了一个检查按钮,这对前端用户是不可见的。 现在在跟踪器中有一张名为“Project Tracks”的工作表,其中包含所有当前项目的信息 单击提交按钮后,控件将转到跟踪器的“项目跟踪”表。它将验证 跟踪在用户表单“OnboardingForm”中输入,跟踪器的“项目跟踪”表中显示了跟踪。一旦找到,针对该特定轨道的其他细节将被提取到跟踪器的sheet1(我这样做,以便我不必手动输入值到用户表'OnboardingForm',以便表单看起来很简单)。轨道没有机会 匹配。

现在一个命令按钮新曲目已放入我当前的用户表单'OnboardingForm'中。点击后,这将控制到 userform2'ProjectTracksForm'。这基本上是这样的,如果我要添加一个新的轨道,表单将获取详细信息并输入 跟踪器的“项目跟踪”表。

问题1> 我当前的用户表单的“跟踪”按钮是一个组合框。如何在跟踪器的下拉列表中添加值 “项目跟踪器”表到下拉列表。

问题2> 在userform2'ProjectTracksForm'中添加新曲目后,提交然后当我回到当前 添加轨道的userform'OnboardingForm'应显示在Track组合框的下拉列表中。 请在下面找到我的代码。

这是我的入职模块

Public Sub OnBoarding()
    On Error GoTo ErrorHandler
    Dim Owb As Object
    Dim ran As Range
    strTalentTrackerPath = shTracker.Cells(2, 2).Value

    'Default the form values to null
    With OnboardingForm
        .combTrackofWork.Value = ""
        .txtFirstName.Text = ""
        .txtLastName.Text = ""
        .combResCat.Value = ""
        .combBFTE.Value = ""
        .combLevel.Value = ""
        .combLocType = ""
        .txtAccessInfo.Text = ""
    End With
    OnboardingForm.Show
    SetFocus.combTrackofWork

    With OnboardingForm
        'Details to be entered in the form'
        strTOW = Trim$(.combTrackofWork.Value)
        strFN = Trim$(.txtFirstName.Text)
        strLN = Trim$(.txtLastName.Text)
        strResCat = Trim$(.combResCat.Value)
        strBilFTE = Trim$(.combBFTE.Value)
        strLevel = Trim$(.combLevel.Value)
        strLocType = (.combLocType.Value)
        strAccessInfo = (.txtAccessInfo.Text)
    End With

    If OnboardingForm.chkOKButtonClick = True Then
        Set oExcel = New Excel.Application
        strMyFolder = strTalentTrackerPath
        Set Owb = oExcel.Workbooks.Open(strMyFolder)
        IntRowCount = Owb.Sheets(1).UsedRange.Rows.Count
        With Owb.Sheets(1)
            With Owb.Sheets("Project Tracks")
                IntTrackRowCount = .UsedRange.Rows.Count
                For IntCurrentRow = 1 To IntTrackRowCount
                    If .Cells(IntCurrentRow, 1) = strTOW Then
                        Owb.Sheets(1).Cells(IntRowCount + 1, OnboardingFormcolumn.colTrackofWork) _
                                = .Cells(IntCurrentRow, ProjectTrackscolumn.colTrack)
                        Owb.Sheets(1).Cells(IntRowCount + 1, OnboardingFormcolumn.colBPO) = .Cells _
                                                                                            (IntCurrentRow, ProjectTrackscolumn.colBPO)
                        Owb.Sheets(1).Cells(IntRowCount + 1, OnboardingFormcolumn.colCostCenter) _
                                = .Cells(IntCurrentRow, ProjectTrackscolumn.colCostCenter)
                        Owb.Sheets(1).Cells(IntRowCount + 1, OnboardingFormcolumn.colGroup) _
                                = .Cells(IntCurrentRow, ProjectTrackscolumn.colGroup)
                        Exit For
                    End If
                Next
            End With
        End With
        .Cells(IntRowCount + 1, OnboardingFormcolumn.colTrackofWork) = strTOW
        .Cells(IntRowCount + 1, OnboardingFormcolumn.colFirstName) = strFN
        .Cells(IntRowCount + 1, OnboardingFormcolumn.colLastName) = strLN
        .Cells(IntRowCount + 1, OnboardingFormcolumn.colResourceCategory) = strResCat
        .Cells(IntRowCount + 1, OnboardingFormcolumn.colBilledFTE) = strBilFTE
        .Cells(IntRowCount + 1, OnboardingFormcolumn.colLevel) = strLevel
        .Cells(IntRowCount + 1, OnboardingFormcolumn.colLocationType) = strLocType
        .Cells(IntRowCount + 1, OnboardingFormcolumn.colAccessInformation) = strAccessInfo

        Owb.Close True
        Set Owb = Nothing
        Set oExcel = Nothing
    Else
        Exit Sub
    End If
    Exit Sub

ErrorHandler:
    If Owb Is Nothing Then
    Else
        Owb.Close False
    End If
    If oExcel Is Nothing Then
    Else
        Set oExcel = Nothing
    End If
    MsgBox "Unhandled Error. Please Report" & vbCrLf & "Error Description: " & _
           Err.Description, vbExclamation
End Sub

这是Onboarding Form的取消按钮

Private Sub cmdbtn_Cancel_Click()
    OnboardingForm.Hide
    MsgBox ("No data entered")
End Sub

这适用于OnboardingForm提交按钮

Private Sub cmdbtn_Submit_Click()
    If Trim(OnboardingForm.combTrackOfWork.Value) = ""  Then
        OnboardingForm.combTOW.SetFocus
        MsgBox ("Track of Work cannot be blank")
        Exit Sub
    End If
    If Trim(OnboardingForm.txtFirstName.Value) = "" Then
        OnboardingForm.txtFN.SetFocus
        MsgBox ("First name cannot be blank")
        Exit Sub
    End If
    If Trim(OnboardingForm.txtLastName.Value) = "" Then
        OnboardingForm.txtLN.SetFocus
        MsgBox ("Last name cannot be blank")
        Exit Sub
    End If
End Sub

项目跟踪模块

Public Sub prjctTracks()
    On Error GoTo ErrorHandler
    Dim Owb As Object
    strTalentTrackerPath = shTracker.Cells(2, 2).Value
    With ProjectTracksForm
        .txtTOW = ""
        .txtBPO = ""
        .txtCOCE = ""
        .txtSOW = ""
        .txtGroup = ""
    End With
    ProjectTracksForm.Show
    With ProjectTracksForm
        strTOW = Trim$(.txtTOW.Text)
        strBPO = Trim$(.txtBPO.Text)
        strCOCE = Trim$(.txtCOCE.Text)
        strSOW = Trim$(.txtSOW.Value)
        strGroup = Trim$(.txtGroup.Value)
    End With
    ProjectTracksForm.Hide
    If ProjectTracksForm.chkbtn_OKclick = True Then
        Set oExcel = New Excel.Application
        strMyFolder = strTalentTrackerPath
        Set Owb = oExcel.Workbooks.Open(strMyFolder)
        With Owb.Sheets("Project Tracks")
            intUsedRowCount = .UsedRange.Rows.Count
            .Cells(intUsedRowCount + 1, Trackscolumn.colTrack) = strTOW
            .Cells(intUsedRowCount + 1, Trackscolumn.colBPO) = strBPO
            .Cells(intUsedRowCount + 1, Trackscolumn.colCostCenter) = strCOCE
            .Cells(intUsedRowCount + 1, Trackscolumn.colSOW) = strSOW
            .Cells(intUsedRowCount + 1, Trackscolumn.colGroup) = strGroup
        End With
        Owb.Close True
        Set Owb = Nothing
        Set oExcel = Nothing
    Else
        Exit Sub
    End If
    Exit Sub
ErrorHandler:
    If Owb Is Nothing Then
    Else
        Owb.Close False
    End If
    If oExcel Is Nothing Then
    Else
        Set oExcel = Nothing
    End If
    MsgBox "Unhandled Error. Please Report" & vbCrLf & "Error Description: " & _
           Err.Description, vbExclamation
End Sub

1 个答案:

答案 0 :(得分:0)

  

问题1>我当前的userform的Track按钮是一个组合框。怎么做   我在跟踪器的“项目跟踪器”的下拉列表中添加值   表格下拉。

我在这个例子中调用组合框“ComboBox1”

放置在组合框中的范围看起来像这样......

enter image description here enter image description here

填充组合框的代码将位于Userform模块中。

Private Sub UserForm_Initialize()
    Dim LstRw As Long
    Dim Rng As Range
    Dim ws As Worksheet

    Set ws = Sheets("Project Tracker")

    With ws
        LstRw = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set Rng = .Range("A2:A" & LstRw)
    End With

    ComboBox1.List = Rng.Value

End Sub
  

问题2>在userform2中添加新曲目后   'ProjectTracksForm',提交然后当我回到现在   添加了轨道的userform'OnboardingForm'应显示在   跟踪组合框的下拉列表

再次激活用户窗体时,可以清除组合框并使用新列表重新填充它。

Private Sub UserForm_Activate()
    Dim LstRw As Long
    Dim Rng As Range
    Dim ws As Worksheet

    Set ws = Sheets("Project Tracker")

    With ws
        LstRw = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set Rng = .Range("A2:A" & LstRw)
    End With

    ComboBox1.Clear
    ComboBox1.List = Rng.Value

End Sub

我认为在某个地方你会有一个代码将新的项目添加到工作表中的List(“项目跟踪器”),

类似于:

Private Sub CommandButton1_Click()
'THIS IS IN THE OTHER USERFORM
'add item to first blank cell in column A sheets("Project Tracker")

    Dim sh As Worksheet
    Dim LstRws As Long

    Set sh = Sheets("Project Tracker")
    With sh
        LstRws = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        .Cells(LstRws, 1) = "SomeThingNew"    'whatever you are adding to the list
    End With


End Sub

代码会在工作表的列表中添加新内容。

再次显示表单时,新项目将位于组合框中。

enter image description here

您可以使用Button,Combobox事件,文本框事件,将项目添加到新列表中。