Userform列表框名称单击以填充来自不同工作簿

时间:2015-11-28 04:03:33

标签: excel excel-vba vba

VBA和Stackoverflow的新手,但喜欢学习VBA以及它能做些什么。 我已经做了一些小代码工作,并且有了很好的开始,以实现这一目标。

我有一个名为Master Copy Ledger的Userform,它有一个带有列表框的Userform(从工作表中提取的人的名字),文本框(在文本框上面有12个月的标签,在左边有标签,有我想要的数据)基于从列表框中选择的名称或在文本框旁边的getdata命令按钮从不同的工作簿中检索)

我想要得到的数据是在同一个工作簿中(现在),A列中的人名和B,C,D到L列中的每个属性。 我现在有一个工作副本,当我选择一个名字时,我得到了Sheet1(Jan)的数据,但是现在需要弄清楚如何将Sheet2(Feb),Sheet3(Mar)等输入Userform并且它应该进入单独的文本框或我可以拉伸原始文本框。如果我必须使用单独的文本框,那么我假设我必须为每个'getdata'函数创建新代码,就像我在orignal中所做的那样,或者是否有一个循环函数来检查所有工作表并查找名称并获取数据和放入文本框(拉伸)或单独的文本框。最终我将学习从不同的工作簿学习,但需要从某个地方开始,同样的工作簿是代码现在工作的地方所以我说顺其自然把它打包然后我将查看不同的工作簿(20xxperformance.xlsx),它是1级,一旦找到该名称,它会从每列收集数据并将其放在正确的文本框中。 现在我在20xxperformance中有一个userform的工作副本

Private Sub cbo_Agent_Change()
Dim Rws As Long, ConRng As Range, AdhRng As Range, AHTRng As Range, ACWRng As Range, TcktsRng As Range, LMIRng As Range, UnderRng As Range, KnowRng As Range, OvrSatRng As Range, OvrScoRng As Range, NPSRng As Range, Agnt As Range

    Rws = Cells(Rows.Count, "A").End(xlUp).Row
    Set Rng = Range(Cells(2, 1), Cells(Rws, 1))
    Set Agnt = Rng.Find(what:=cbo_Agent, lookat:=xlWhole)
    Set ConRng = Agnt.Offset(0, 1)   'set ConRng
    Set AdhRng = Agnt.Offset(0, 2)  'set AdhRng
    Set AHTRng = Agnt.Offset(0, 3)  'set AHTRng
    Set ACWRng = Agnt.Offset(0, 4)  'set AHTRng
    Set TcktsRng = Agnt.Offset(0, 5)  'set TcktsRng
    Set LMIRng = Agnt.Offset(0, 6)  'set LMIRng
    Set UnderRng = Agnt.Offset(0, 7)  'set UnderRng
    Set KnowRng = Agnt.Offset(0, 8)  'set KnowRng
    Set OvrSatRng = Agnt.Offset(0, 9)  'set OvrSatRng
    Set OvrScoRng = Agnt.Offset(0, 10)  'set OvrScoRng
    Set NPSRng = Agnt.Offset(0, 11)  'set NPSRng
    txt_Con = ConRng
    txt_Adh = AdhRng
    txt_AHT = AHTRng
    txt_ACW = ACWRng
    txt_tckts = TcktsRng
    txt_LMI = LMIRng
    txt_Under = UnderRng
    txt_Know = KnowRng
    txt_Osat = OvrSatRng
    txt_OScor = OvrScoRng
    txt_NPS = NPSRng
End Sub



Private Sub UserForm_Initialize()
Dim Rws As Long, Rng As Range
    Rws = Cells(Rows.Count, "A").End(xlUp).Row
    Set Rng = Range(Cells(2, 1), Cells(Rws, 1))
    cbo_Agent.List = Rng.Value

End Sub

正如我所说,我是VBA的新手,感觉我在每个人的帮助下都取得了更多成就,并发现自己再次寻求专家建议并需要一些帮助。我不是在寻找整个代码(谦卑地接受,如果有人这样做),但至少还有另外一段很棒的代码,这样我才能理解并实践你的教导。

谢谢

ptpapa

enter image description here

3 个答案:

答案 0 :(得分:0)

希望这个示例对您有帮助,如果您使用命名范围而不是正常范围A1:A10也会减少时间和代码行:)

Public Sub comboval()
Dim spath As String
spath = "file path"

Workbooks.Open Filename:=spath
    With ActiveWorkbook

        Combobox1.List = .Sheets("Sheet1").Range("namedranmge1").Value
        Combobox2.List = .Sheets("Sheet2").Range("namedranmge2").Value

        .Close 0
    End With

End Sub

详细命名范围Click Here

答案 1 :(得分:0)

您可以在Month列中为每个文本框命名,例如Jan1,Jan2,Feb1,Feb2等。

当您循环浏览工作表时,可以按名称添加到文本框中 Controls(sh.Name & x).Value = .Cells(r, 1 + x)  完整的代码看起来像这样,我在编写代码时只使用了一个小例子,所以我每个月只使用了三个文本框

Private Sub ComboBox1_Change()
    Dim sh As Worksheet
    Dim x As Integer
    Dim rws As Long, rng As Range, c As Range, r


    For Each sh In Sheets
        With sh
            rws = .Cells(.Rows.Count, "A").End(xlUp).Row
            Set rng = .Range(.Cells(2, 1), .Cells(rws, 1))
            Set c = rng.Find(what:=ComboBox1, lookat:=xlWhole)
            r = c.Row
            For x = 1 To 3
                Controls(sh.Name & x).Value = .Cells(r, 1 + x)
            Next x
        End With
    Next sh

End Sub

Sheet Result

您可以在此处下载示例工作簿,查看文本框的命名方式。

UserForm Example

答案 2 :(得分:0)

这是我设法与Davesexcel的建议和示例相结合:

Private Sub cbo_Agent_Change(Target_Workbook As Workbook)
Dim Rws As Long, ConRng As Range, AdhRng As Range, AHTRng As Range, ACWRng As Range, TcktsRng As Range, LMIRng As Range, UnderRng As Range, KnowRng As Range, OvrSatRng As Range, OvrScoRng As Range, NPSRng As Range, Agnt As Range
Dim intCounter As Integer
Dim control_item
Dim Rng
Dim total_counter(11) As Single
Dim total_items As Integer

Dim Rng0
For intCounter = 1 To 12 'Goes through each individual month looking for stats

    Rws = Target_Workbook.Worksheets(intCounter).Cells(Rows.Count, "A").End(xlUp).Row

    If Rws > 1 Then 'This confirms there are stats for this month
        With Target_Workbook.Worksheets(intCounter)

            Set Rng = .Range(.Cells(2, 1), .Cells(Rws, 1))
            Set Agnt = Rng.Find(what:=lstNames, lookat:=xlWhole) 'Search for the employee in pull down menu

        End With

        Set ConRng = Agnt.Offset(0, 1)   'set ConRng
        Set AdhRng = Agnt.Offset(0, 2)  'set AdhRng
        Set AHTRng = Agnt.Offset(0, 3)  'set AHTRng
        Set ACWRng = Agnt.Offset(0, 4)  'set AHTRng
        Set TcktsRng = Agnt.Offset(0, 5)  'set TcktsRng
        Set LMIRng = Agnt.Offset(0, 6)  'set LMIRng
        Set UnderRng = Agnt.Offset(0, 7)  'set UnderRng
        Set KnowRng = Agnt.Offset(0, 8)  'set KnowRng
        Set OvrSatRng = Agnt.Offset(0, 9)  'set OvrSatRng
        Set OvrScoRng = Agnt.Offset(0, 10)  'set OvrScoRng
        Set NPSRng = Agnt.Offset(0, 11)  'set NPSRng

        'This fills the table with data
        With MonthlyStats 'These save all the information to the necessary text fields
            .Controls("txt_Con" & intCounter) = VBA.Format(ConRng, "0.0%")
            .Controls("txt_Adh" & intCounter) = VBA.Format(AdhRng, "0.0%")
            .Controls("txt_AHT" & intCounter) = AHTRng
            .Controls("txt_ACW" & intCounter) = ACWRng
            .Controls("txt_tckts" & intCounter) = VBA.Format(TcktsRng, "0.0%")
            .Controls("txt_LMI" & intCounter) = LMIRng
            .Controls("txt_Under" & intCounter) = VBA.Format(UnderRng, "0.0%")
            .Controls("txt_Know" & intCounter) = VBA.Format(KnowRng, "0.0%")
            .Controls("txt_Osat" & intCounter) = VBA.Format(OvrSatRng, "0.0%")
            .Controls("txt_OScor" & intCounter) = VBA.Format(OvrScoRng, "0.0%")
            .Controls("txt_NPS" & intCounter) = VBA.Format(NPSRng, "0.0%")
        End With

        'This keeps track of all the values for each row to later be used to figure out the average
        total_counter(0) = total_counter(0) + ConRng
        total_counter(1) = total_counter(1) + AdhRng
        total_counter(2) = total_counter(2) + AHTRng
        total_counter(3) = total_counter(3) + ACWRng
        total_counter(4) = total_counter(4) + TcktsRng
        total_counter(5) = total_counter(5) + LMIRng
        total_counter(6) = total_counter(6) + UnderRng
        total_counter(7) = total_counter(7) + KnowRng
        total_counter(8) = total_counter(8) + OvrSatRng
        total_counter(9) = total_counter(9) + OvrScoRng
        total_counter(10) = total_counter(10) + NPSRng

        total_items = total_items + 1
    End If

Next

'This will figure out the average of each line
txt_ConfYTD = VBA.Format(total_counter(0) / total_items, "0.0%")
txt_AdhYTD = VBA.Format(total_counter(1) / total_items, "0.0%")
txt_AHTYTD = VBA.Format(total_counter(2) / total_items, "0.00")
txt_ACWYTD = VBA.Format(total_counter(3) / total_items, "0.00")
txt_tcktsYTD = VBA.Format(total_counter(4) / total_items, "0.0%")
txt_LMIYTD = VBA.Format(total_counter(5) / total_items, "0.00")
txt_UnderYTD = VBA.Format(total_counter(6) / total_items, "0.0%")
txt_KnowYTD = VBA.Format(total_counter(7) / intCounter, "0.0%")
txt_OsatYTD = VBA.Format(total_counter(8) / intCounter, "0.0%")
txt_OScorYTD = VBA.Format(total_counter(9) / intCounter, "0.0%")
txt_NPSYTD = VBA.Format(total_counter(10) / intCounter, "0.0%")


End Sub