用户窗体列表框名称单击以填充不同工作簿中的文本框

新的VBA和Stackoverflow,但享受学习VBA和它可以做什么。 我已经做了一些小的代码工作,并得到了很好的启动,以达到这一点。

我有一个名为Master Copy Ledger的UserForm,它有一个带有列表框的用户窗体(从工作表中拉出的用户名),文本框(在文本框上方有12个月的标签,左侧有标签,基于从列表框中select的名称的不同工作簿或位于文本框旁边的getdata命令button)

我想得到的数据是在同一个工作簿(现在)在A列中的人的名字,在列B,C,D到列L中的每个属性。我现在有一个工作副本,当我select一个名字我获取Sheet1(Jan)的数据,但现在需要弄清楚如何将Sheet2(Feb),Sheet3(Mar)等放到Userform中,并且它应该放在单独的文本框中,还是可以拉伸原始文本框。 如果我必须使用单独的文本框,那么我假设我必须为每个“getdata”函数创build新的代码,就像我在orignal中所做的那样,或者是有循环函数来检查所有表单并find名称并获取数据放置到文本框(拉伸)或单独的文本框。最后,我将学习从一个不同的工作簿获得,但需要从某处开始,同一个工作簿是代码正在工作的地方,所以我说,随着stream程,直到我把它打包下来,然后我将查找不同级别的工作簿(20xxperformance.xlsx),一旦find该名称,就会从每列收集数据并将其放在正确的文本框中。 现在我有20xx性能的用户表单的工作副本

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的新手,感觉我已经完成了更多与大家的帮助,发现自己寻找专家再次build议,需要一些帮助。 我并不是在寻找整个代码(如果有人这样做,那么就虚心接受),但至less还有另一个伟大的代码,所以我可以理解并付诸实践你的教导。

谢谢

ptpapa

在这里输入图像说明

希望这个示例能够帮助你,如果你使用命名范围而不是正常范围A1:A10会减less时间和代码行:)

 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 

命名范围详细点击这里

您可以在“月份”列中命名每个文本框,例如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 

片 结果

您可以在这里下载示例工作簿,查看如何命名文本框。

用户窗体示例

这是我设法与Davesexcel的build议和例子一起:

 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