使用用户表单从当前打开的工作簿中的外部工作簿中检索信息

Retrieving information from external workbook from current open workbook using a user form

我为用户窗体编写代码,允许我使用工作簿的 Excel sheet 中的项目 ID 检索信息。

但是,现在我需要根据用户输入在名为 active master file 的外部工作簿 中找到的 projectID 进行搜索。

然后它将检索信息并将其放入用户窗体,并使用添加命令按钮将信息插入到当前工作簿(称为项目跟踪器文件)中找到的新行中。

问题是我不确定从外部工作簿检索信息到当前打开的工作簿中存在的用户窗体的代码。

我有两本练习册。此用户表单位于名为 project tracker 的工作簿中,而我想根据其项目 ID 检索信息的外部工作簿名为 active master project

这是我的命令搜索按钮代码,用于在名为 project tracker:

的工作簿的 sheet 中进行检索和搜索
Private Sub CommandSearchButton2_Click()

    Dim lastrow
    Dim ProjCode As String
    Dim LabelProjName As String
    Dim LabelObjective As String
    Dim LabelProjSponsor As String
    Dim LabelProjSponsorNew As String
    Dim LabelProjManager As String
    Dim LabelRegulatory As String
    Dim LabelRiskLvl As String
    Dim LabelDatePar As Date
    Dim LabelCostPar As Long
    Dim LabelAffectCust As String
    Dim LabelCustNonRetail As String
    Dim LabelCustRetail As String
    Dim LabOutsourcingImp As String
    Dim LabelKeyUpdate As String
    Dim LabelSector As String



    searchRow = 0
        lastrow = Sheets("Program Status Summary").Range("B" & Rows.Count).End(xlUp).Row
        ProjCode = TextBoxProjCode.Text

     For currentrow = 4 To 100

        If Cells(currentrow, 2).Text = ProjCode Then

            searchRow = currentrow

            TextBoxProjCode.Text = Cells(currentrow, 2).Text
            TextBoxProjName.Text = Cells(currentrow, 3)
            TextBoxSector.Text = Cells(currentrow, 4)
            TextBoxObjective.Text = Cells(currentrow, 5)
            TextBoxProjSponsor.Text = Cells(currentrow, 7)
            TextBoxProjSponsorNew.Text = Cells(currentrow, 8)
            TextBoxProjM.Text = Cells(currentrow, 6)
            TextBoxRegulatory.Text = Cells(currentrow, 20)
            TextBoxRiskLvl.Text = Cells(currentrow, 13)
            TextBoxDatePar.Text = Cells(currentrow, 12)
            TextBoxCostPar.Text = Cells(currentrow, 10)
            TextBoxAffectCust.Text = Cells(currentrow, 15)
            TextBoxCustNonRetail.Text = Cells(currentrow, 16)
            TextBoxCustRetail.Text = Cells(currentrow, 17)
            TextBoxOutsourcingImp.Text = Cells(currentrow, 19)
            TextBoxKeyUpdate.Text = Cells(currentrow, 18)

        End If

    Next currentrow

TextBoxProjCode.SetFocus

End Sub

处理多个工作簿的最佳方式是使用对象变量 :

Dim WbPT As Workbook, _
    WbAMP As Workbook, _
    WsPSS As Worksheet, _
    Ws As Worksheet

'----The SET keyword is only to attribute value to an object variable
Set WbPT = Workbooks("project tracker")
Set WbAMP = Workbooks("active master project")

Set WsPSS = WbPT.Sheets("Program Status Summary")

'----Use with to have the reference availaible starting with a simple dot "."
With WsPSS
    MsgBox .Cells(1, "B")
End With

所以如果我理解得很好,你的代码应该是这样的:

Private Sub CommandSearchButton2_Click()

    Dim lastrow
    Dim ProjCode As String
    Dim LabelProjName As String
    Dim LabelObjective As String
    Dim LabelProjSponsor As String
    Dim LabelProjSponsorNew As String
    Dim LabelProjManager As String
    Dim LabelRegulatory As String
    Dim LabelRiskLvl As String
    Dim LabelDatePar As Date
    Dim LabelCostPar As Long
    Dim LabelAffectCust As String
    Dim LabelCustNonRetail As String
    Dim LabelCustRetail As String
    Dim LabOutsourcingImp As String
    Dim LabelKeyUpdate As String
    Dim LabelSector As String

    Dim WbPT As Workbook, _
        WbAMP As Workbook, _
        WsPSS As Worksheet, _
        Ws As Worksheet

    Set WbPT = Workbooks("project tracker")
    Set WbAMP = Workbooks("active master project")

    Set WsPSS = WbPT.Sheets("Program Status Summary")

    With WsPSS
        searchRow = 0
        lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
        ProjCode = TextBoxProjCode.Text

         For currentrow = 4 To lastrow
            If .Cells(currentrow, 2).Text = ProjCode Then
                searchRow = currentrow

                TextBoxProjCode.Text = .Cells(currentrow, 2).Text
                TextBoxProjName.Text = .Cells(currentrow, 3)
                TextBoxSector.Text = .Cells(currentrow, 4)
                TextBoxObjective.Text = .Cells(currentrow, 5)
                TextBoxProjSponsor.Text = .Cells(currentrow, 7)
                TextBoxProjSponsorNew.Text = .Cells(currentrow, 8)
                TextBoxProjM.Text = .Cells(currentrow, 6)
                TextBoxRegulatory.Text = .Cells(currentrow, 20)
                TextBoxRiskLvl.Text = .Cells(currentrow, 13)
                TextBoxDatePar.Text = .Cells(currentrow, 12)
                TextBoxCostPar.Text = .Cells(currentrow, 10)
                TextBoxAffectCust.Text = .Cells(currentrow, 15)
                TextBoxCustNonRetail.Text = .Cells(currentrow, 16)
                TextBoxCustRetail.Text = .Cells(currentrow, 17)
                TextBoxOutsourcingImp.Text = .Cells(currentrow, 19)
                TextBoxKeyUpdate.Text = .Cells(currentrow, 18)
            End If
        Next currentrow
    End With

    TextBoxProjCode.SetFocus

End Sub