excel - 如何更改引用输入框的变体?
问题描述
我正在尝试将 file_split 脚本简化为我部门中的自助服务点。没有人真正了解该语言,因此我正在检查是否可以进一步简化其中的任何内容,以便同事不必从编辑器窗格更新代码。
例如,我有一些事情想Basepath
指定文件将被保存在哪里。我该如何改变
Dim Basepath As String
Basepath = "C:\Users\File Cuts\"
directory as string
到这样的用户可以选择文件夹路径的东西?
Dim Basepath as filedialog
with basepath
.title = "Select save location"
.directory = .selecteditems(1)
end with
然后是我有特定列要引用的实例(每个新文件的目标值列、命名约定列等......)
如:
Dim Manager_Name, Login_ID, Leader
Manager_Name = SourceData(i,4)
Login_ID = SourceData(i,5)
Leader = SourceData(i,9)
由列字母的输入框输入,例如:
Dim column_selection as variant
column_selection = InputBox("Enter Column Letter")
Manager_Name = SourceData(i,column_selection)
有很多参考资料我想看看是否可以更改,以便在不实际接触代码的情况下进行编辑(名称和登录 ID 等变体的列范围将发生很大变化)
其余代码:
Option Explicit
Sub File_Splits()
Dim Wb As Workbook
Dim SourceData, Mgr_Name, Login_Id
Dim i As Long, j As Long, k As Long, a As Long
Dim Destination_Cell As Range
Dim Basepath As String, strNewpath As String, strLeader As String
Basepath = "C:\File Cuts\" '1. paste in file save pathway, keep last \
Set Wb = Workbooks.Open("C:\File_Split_Mgr_Template.xlsx") '2. paste template ws address here
Set Destination_Cell = Wb.Worksheets("Manager Data").Range("A2") '3. Update worksheet name and target cell
With ThisWorkbook.Worksheets("Roster")
SourceData = .Range("I10", .Range("A" & Rows.Count).End(xlUp)) '4. change I10 to your last column letter, dont change the number(keep the 10)
End With
Wb.Activate
Call Speed_Up_Code(True)
For i = 1 To UBound(SourceData)
If SourceData(i, 5) <> Login_Id Then '5. change the 1 to login column #
If i > 9 Then
Destination_Cell.Select
strNewpath = Basepath & strLeader & "\" 'comment this out if folders aren't needed
If Len(Dir(strNewpathD, vbDirectory)) = 0 Then 'comment this out if folders aren't needed
MkDir strNewpath 'comment this out if folders aren't needed
End If 'comment this out if folders aren't needed
Wb.SaveCopyAs strNewpath & _
ValidFileName(Login_Id & "_" & Mgr_Name & "_File Name.xlsx") '6. update file name
End If
With Wb.Worksheets("Manager Data") '7. change to template sheet
.Rows(2 & ":" & .Rows.Count).ClearContents '8. change 2 to row after header(s)--if header isn't in row 1
End With
Mgr_Name = SourceData(i, 4) '9. change 1 to mgr name column
Login_Id = SourceData(i, 5) '10. change 2 to login ID column
strLeader = SourceData(i, 9) '11. change 5 to lvl 3 mgr column
j = 0
End If
a = 0
For k = 1 To UBound(SourceData, 2)
Destination_Cell.Offset(j, a) = SourceData(i, k)
a = a + 1
Next
j = j + 1
Next
If Len(Dir(strNewpath, vbDirectory)) = 0 Then
MkDir strNewpath
End If
SaveCopy Wb, strNewpath, Login_Id, Mgr_Name
Call Speed_Up_Code(False)
End Sub
Public Sub SaveCopy(Wb As Workbook, strNewpath As String, Login_Id, Mgr_Name)
Wb.SaveCopyAs strNewpath & _
ValidFileName(Login_Id & "_" & Mgr_Name & "_File Name.xlsx") '12. update file name
End Sub
解决方案
您是否考虑过有一张名为“配置”之类的工作表,用户可以在其中写入,而您的脚本可以从中读取。必要时隐藏或保护
例如,在 col A 中列出您所有的配置描述,并且用户在 col B 中填写旁边的值,因此如果 A1 包含文本“Manager Name Column [AZ] =”,则用户输入值“D”或 4在单元格 B1 中。脚本变为 Mgr_Name = SourceData(i, wsConfig.range("B1"))。我想您可以在他们的条目中添加验证。
将工作表布局为逻辑组中的表单,并突出显示条目单元格的位置。在像输入列名这样的情况下,我会将它们与上面的描述和下面的条目单元格水平放置,这似乎更自然。保护除突出显示的单元格之外的所有单元格。
推荐阅读
- javascript - 在 Vue 中访问组件属性
- java - Flink 作业执行失败
- react-native - 我可以在本机反应中关闭应用程序实例上的 AsyncStorage 中的项目吗?
- arrays - jq:错误不能用字符串索引字符串。如何使用 jq 解析具有可变对象值的 json 文件
- postman - 使用不同的数据在 Postman 中多次运行请求只运行一次
- java - 如何使用 Springboot 将 JSP 文件保存在两个或多个不同的文件夹中?
- linux - 如何对文件的完整文件夹进行排序并返回加起来为 x 的文件的名称
- sql - 来自数据的 Oracle SQL Todays 日期
- java - Spring - 对同一服务器上远程 Git 的更改做出反应
- excel - 将 Excel 行数据传输到其相关列