【问题标题】:vba excel combo box in userform用户表单中的vba excel组合框
【发布时间】:2016-01-29 05:34:31
【问题描述】:

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

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

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

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

这是我的入职模块

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

这是用于入职表格的取消按钮

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

【问题讨论】:

  • 你为什么懒得回复这个帖子?

标签: vba excel


【解决方案1】:

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

在本例中,我将组合框称为“ComboBox1”

要放置在组合框中的 Range 如下所示...

填充组合框的代码将在用户表单模块中。

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',提交,然后当我回到我现在的 添加轨道的用户表单“OnboardingForm”应显示在 Track 组合框的下拉菜单

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

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

我假设在某个地方你会有一个代码,它将向工作表中的列表添加一个新项目(“项目跟踪器”),

类似:

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

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

当您再次显示表单时,新项目将出现在组合框中。

您可以使用按钮、组合框事件、文本框事件将项目添加到新列表中。

【讨论】:

    猜你喜欢
    • 2011-02-07
    • 1970-01-01
    • 2018-03-21
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多