首先,我想说我是VBA的新手但是我已经学习了很多,并且在我的上一个问题上得到了Davesexcel的大力帮助。感谢他,我已经能够创建一个Userform,根据在combobox中选择的名称将数据拉入我的Userform上的文本框中。我现在遇到的问题是每个名称需要每月从同一工作簿中拉出(多个工作表 - 每个工作表按月命名;因此Jan是sheet1; Feb是工作表2,依此类推)。我可以通过选择的名称将工作表1数据拉入Userform,并且工作正常。接下来的几个月我将如何进行,如果它们进入Userform上的单独文本框,或者我应该扩展已经存在的文本框的宽度,并为每个主题使用一个文本框。我提供了Userform外观的屏幕视图,希望代码也能正确显示Userform with code working but should textboxes be extended or additional textboxes used
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
与原始问题一样;一旦我学到足够的目标,我的目标是每月填写一次Userform,但是来自不同的工作簿,但是现在我想学习并能够在每个月的同一工作簿中提取数据,然后解决如何从数据中提取数据的问题。关闭的工作簿。 Orignally希望将数据从20xxperformance.xlsx提取到Master Ledger中,但为了学习,我在20xx性能中构建了Userform以了解功能方面。我希望我的调查是可以理解的,如果不是我的道歉,因为如果需要,我会尽量更明确。感谢您在我继续学习和成长的过程中提供的帮助和指导。
问题我不能沿着这条线使用某些东西吗?
Dim Ws As Worksheet, rCell As Range
For Each Ws In ActiveWorkbook.Worksheets
Select Case UCase(Ws.Name)
Case "SHEET1", "SHEET3", "SHEET4"
With Ws
For Each rCell In .Range("A1", .Cells(Rows.Count, 1).End(xlUp))
ComboBox1.AddItem rCell
Next rCell
End With
Case "SHEET2"
With Ws
For Each rCell In .Range("A1", .Cells(Rows.Count, 4).End(xlUp))
ComboBox1.AddItem rCell
Next rCell
End With
End Select
Next Ws
For Each Ws In ActiveWorkbook.Worksheets
Select Case UCase(Ws.Name)
Case "SHEET1", "SHEET3", "SHEET4"
With Ws
For Each rCell In .Range("A1", .Cells(Rows.Count, 1).End(xlUp))
ComboBox1.AddItem rCell
Next rCell
End With
Case "SHEET2"
With Ws
For Each rCell In .Range("A1", .Cells(Rows.Count, 4).End(xlUp))
ComboBox1.AddItem rCell
Next rCell
End With
End Select
Next Ws
或者我应该使用列表框吗?我宁愿不做月度代码和许多文本框,虽然它有助于学习哪些我仍然会做,但需要尽快得到这个,我喜欢Mathew的循环的想法,但仍然需要帮助超过我收到的东西,因为它仍然有些困惑我。 感谢
答案 0 :(得分:3)
我会使用附加文本框。你有一天(如果你愿意的话)可以动态地创建它们并将它们用作数组,但它更先进一些。
使用工作表对象从不是活动工作表的工作表中获取数据。
Dim ws as Excel.Worksheet
Set ws = Application.Sheets("February")
然后使用工作表对象作为范围和单元格对象。
Rws = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set Rng = ws.Range(ws.Cells(2, 1), ws.Cells(Rws, 1))