excel - 从特定文件夹子文件夹重命名特定工作表
问题描述
我在文件夹中有几个 excel 文件,并且只想重命名文件夹中每个文件的特定工作表,其中包含
即。GTLB,薪水,GROC
每个文件都有一张上述字符,其他文件有不同的名称。因此,如果工作表名称包含上述字符,则将其更改为GROCERY。
提前致谢
解决方案
尝试使用它,它将遍历文件夹尝试查找文件(excel文件)并尝试在已指定的文件中查找字符串,如果找到匹配项,请更改名称。
Sub LoopThroughFiles()
'loops through all files in a folder
Dim MyObj As Object, MySource As Object, file As Variant
Dim wbk As Workbook
Dim path As String
Dim st As String
file = Dir("H:\TestCopy\testing\") 'file name
path = "H:\TestCopy\testing\" 'directory path
While (file <> "")
Set wbk = Workbooks.Open("H:\TestCopy\testing\" & file)
MsgBox "found " & file
' path = path & file 'path and filename
Call newloopTrhoughBooks
wbk.Save
wbk.Close
' Call loop_through_all_worksheets(path)
file = Dir
Wend
End Sub
Sub newloopTrhoughBooks()
Dim book As Workbook, sheet As Worksheet, text As String, text1 As String
Dim logic_string As String
Dim logic_string2 As String
Dim logic_string3 As String
logic_string = "GTLB"
logic_string2 = "SALARY"
logic_string3 = "GROC"
For Each book In Workbooks
text = text & "Workbook: " & book.Name & vbNewLine & "Worksheets: " & vbNewLine
For Each sheet In book.Worksheets
text = text & sheet.Name & vbNewLine
text1 = sheet.Name
If StrComp(logic_string, text1) = 1 Or StrComp(logic_string2, text1) = 1 Or StrComp(logic_string3, text1) = 1 Then 'compare file name
ActiveSheet.Name = text1
ActiveSheet.Name = "Change1"
End If
Next sheet
text = text & vbNewLine
Next book
MsgBox text
End Sub
推荐阅读
- python - 在 FastAPI 中连接数据库的最佳方法是什么?
- python - 无法在全新的 anaconda 环境中安装 apache-beam
- google-apps-script - appendParagraph - 在第一行添加空格
- c++ - c++ new 不存储对象
- flutter - ExpansionTile 不会滚动到打开的内容
- python - 替换单词,但替换了具有相同字母格式的另一个单词
- java - 如果绑定结果有错误,API 是否应该抛出异常?
- spring - 如何模拟 RestTemplate 交换?
- r - 如何改进沿 ablines 对齐文本的方式?
- html - HTML:iframe 和段落在同一行