excel - 链接用户窗体答案以更改宏中的文件路径
问题描述
我有一个摘要工作簿,它从保存在文件夹中的多个 Excel 工作簿中提取数据。每个文件夹都包含特定年份和月份的数据工作簿。例如,C:\Desktop\Summaries\MONTH YEAR
我还有一个用户表单,可以让您选择年份和月份。我想从我的用户窗体中得到答案以告知文件路径。 见图片
我想根据在我的用户窗体中选择的内容更改此文件路径的月份和年份
例如,如果我想提取 2021 年 2 月的数据,我会从我的 UserForm 中选择 2 月和 2021 年。当我按下提交时,我希望我的文件路径更改为 C:\Desktop\Summaries\February 2021
然后我的代码将运行以汇总该文件夹中的数据。如何将我的用户窗体答案链接到我的文件路径的更改?
请参阅下面我用来总结的代码:
Dim desWS As Worksheet, srcWB As Workbook
Set desWS = ThisWorkbook.Sheets("Data")
Dim LastRow As Long
Const strPath As String = "C:\Desktop\Summaries\MONTH YEAR"
ChDir strPath
strExtension = Dir(strPath & "*.xlsx")
Do While strExtension <> ""
Set srcWB = Workbooks.Open(strPath & strExtension)
With srcWB.Sheets("Macro")
desWS.Cells(desWS.Rows.Count, "B").End(xlUp).Offset(1, 0).Resize(, 22).Value = Application.WorksheetFunction.Transpose(.Range("C3:C24").Value)
End With
srcWB.Close False
strExtension = Dir
Loop
Application.ScreenUpdating = True
解决方案
由于没有关于用户表单的更多信息,我建议查看以下代码。我假设您的组合框是相应命名的。这是您在用户表单的类模块中的示例代码
Option Explicit
Private mCancelled As Boolean
Public Property Get Cancelled() As Boolean
Cancelled = mCancelled
End Property
Property Get Year() As String
Year = cmbYear.Value
End Property
Property Get Month() As String
Month = cmbMonth.Value
End Property
Private Sub UserForm_QueryClose(Cancel As Integer _
, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
Hide
mCancelled = True
End Sub
这是测试它的代码
Option Explicit
Sub GetValues()
Dim frm As New UserForm1
frm.Show
If frm.Cancelled = True Then
MsgBox "The UserForm was cancelled."
Else
MsgBox "You entered: " & frm.Year & " - " & frm.Month
End If
Unload frm
Set frm = Nothing
End Sub
推荐阅读
- linux - 氧化:交换机备份输出配置文件格式-DeviceName/IP-YYYY:DD:MM:HH:SS
- ruby - 在 ruby 中接收来自 IP 255.255.255.255 的广播数据包
- python-3.x - python mongodb over cosmosdb 通过字段获取具有最大日期组的行
- sql - BigQuery - 查询每个元素
- c# - 将 json 字符串中的 24 字符十六进制值解析为 BsonObjectId
- python - 在界面中导入 CSV 文件
- php - 外部库上的 Visual Studio Code PHP 自动完成
- java - 如何将自定义对象列表重新组合成 Java 中的地图?
- python - Django/Docker/Logging:日志文件为空
- css - 为什么 flexbox 让我的广告内容消失(有时)?