使用相同的单元格值导入和保存

时间:2015-03-27 09:27:28

标签: excel-vba vba excel

我将不同的工作簿导入到一个工作簿中但成功但我无法使用相同的单元格值保存它。它似乎跳过了保存的Range("D2:E3").text部分。

Application.DisplayAlerts = False
Application.ScreenUpdating = False


Path = "\\ap01\data\Reports\Scrap WHS02\Scrap History\"
fileName = Dir(Path & Range("D2:E3").Text & "*_02*.xlsx")
Do While fileName <> ""
Workbooks.Open fileName:=Path & fileName, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(fileName).Close
fileName = Dir()
Loop

Dim sfileName As String
sfileName = "\\ap01\data\Reports\Scrap WHS02\Scrap History\" & Range("D2:E3").Text & "_02 Scrap.xlsx"
ActiveWorkbook.SaveAs fileName:=sfileName, FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

Application.ScreenUpdating = True
Application.DisplayAlerts = True

2 个答案:

答案 0 :(得分:0)

我在法国网站上看到使用Dir调用该文件时没有()。因此,在循环中更改fileName = Dir() fileName = Dir

答案 1 :(得分:0)

问题可能在于合并的单元格。 当我使用它时,它将起作用,它将不适用于合并的单元格范围(“D2:E3”)

Dim sfileName As String
sfileName = "C:\TestFolder\" & Range("D2") & "_02 Scrap.xlsx"
ActiveWorkbook.SaveAs Filename:=sfileName

尽量避免在代码中使用合并的单元格。