VBA 错误不支持此类接口(我正在使用 MS Access 应用程序来操作 Excel 文件/数据
我正在使用MS Access应用程序来操纵文件。 GYST是我想通过所有文件夹和子文件夹循环,打开每个工作簿,如果存在Mastersheet选项卡,请删除它。如果不是,请创建它。对于第一个文件夹来说正常工作,但文件上的错误。同样,当我尝试打开将选项卡添加到Explorer中的价格文件时,价格文件和主文件一次打开。任何帮助都非常感谢!!!提前致谢!
Private Sub ProcessFilesBtn_Click()
'Windows object variables
Dim fso As Object
Dim folder As Object
Dim subfolders As Object
Dim CurrFile As Object
Dim MyFile As String
'Folder and subfolder location variables
Dim DivisionFolder As String
Dim sFileName As String
'Excel workbook variables
Dim PriceGrid As Excel.Workbook
Dim xlapp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlPath As String
Dim SheetName As String
Dim TotalSheets As Integer
Dim MasterWorkbook As Workbook
Dim msheet As Worksheet
''Set Object Variables
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\Users\name\Desktop\Pricing Tool\Price Grids\")
Set subfolders = folder.subfolders
SheetName = "MasterSheet"
For Each subfolders In subfolders
Set CurrFile = subfolders.Files
For Each CurrFile In CurrFile
Debug.Print subfolders.Path & "\" & CurrFile.Name
Debug.Print subfolders.Name
Debug.Print CurrFile.Name
Set PriceGrid = Workbooks.Open(subfolders.Path & "\" & CurrFile.Name)
Set xlapp = CreateObject("Excel.Application")
Set MasterWorkbook = xlapp.Workbooks.Open("C:\Users\name\Desktop\Pricing Tool\MasterSheet")
'Step 1 - If the MasterSheet tab exists on the PriceGrid tab, delete and recreate it. This is for reloads when data is updated -add in Loop
For Each xlSheet In PriceGrid.Worksheets
Debug.Print xlSheet.Name
If SheetName = xlSheet.Name Then
xlSheet.Delete
End If
Next xlSheet
'Copies the master worksheet to the PriceGrid workbook -Add to Loop
For Each msheet In MasterWorkbook.Sheets
msheet.Copy Before:=PriceGrid.Sheets(1)
Next
'Save changes and close workbook
PriceGrid.Save
PriceGrid.Close
MasterWorkbook.Close
Next
Next
Set fso = Nothing
Set folder = Nothing
Set subfolders = Nothing
我尝试了同一文件夹中的文件,在不同文件夹中的文件,创建新文件。尝试了其他文章中的一些答案。没有成功。
I am using an MS Access App to manipulate files. The gyst is I want to loop thru all folders and subfolders, open each workbook and if the mastersheet tab exists, delete it. If not, create it. Works fine for the first folder but errors on the file. Also when I try to open the price file that had the tab added to it in explorer, both the price file and master file open at once. Any help is greatly appreciated!!! Thanks in advance!
Private Sub ProcessFilesBtn_Click()
'Windows object variables
Dim fso As Object
Dim folder As Object
Dim subfolders As Object
Dim CurrFile As Object
Dim MyFile As String
'Folder and subfolder location variables
Dim DivisionFolder As String
Dim sFileName As String
'Excel workbook variables
Dim PriceGrid As Excel.Workbook
Dim xlapp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlPath As String
Dim SheetName As String
Dim TotalSheets As Integer
Dim MasterWorkbook As Workbook
Dim msheet As Worksheet
''Set Object Variables
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\Users\name\Desktop\Pricing Tool\Price Grids\")
Set subfolders = folder.subfolders
SheetName = "MasterSheet"
For Each subfolders In subfolders
Set CurrFile = subfolders.Files
For Each CurrFile In CurrFile
Debug.Print subfolders.Path & "\" & CurrFile.Name
Debug.Print subfolders.Name
Debug.Print CurrFile.Name
Set PriceGrid = Workbooks.Open(subfolders.Path & "\" & CurrFile.Name)
Set xlapp = CreateObject("Excel.Application")
Set MasterWorkbook = xlapp.Workbooks.Open("C:\Users\name\Desktop\Pricing Tool\MasterSheet")
'Step 1 - If the MasterSheet tab exists on the PriceGrid tab, delete and recreate it. This is for reloads when data is updated -add in Loop
For Each xlSheet In PriceGrid.Worksheets
Debug.Print xlSheet.Name
If SheetName = xlSheet.Name Then
xlSheet.Delete
End If
Next xlSheet
'Copies the master worksheet to the PriceGrid workbook -Add to Loop
For Each msheet In MasterWorkbook.Sheets
msheet.Copy Before:=PriceGrid.Sheets(1)
Next
'Save changes and close workbook
PriceGrid.Save
PriceGrid.Close
MasterWorkbook.Close
Next
Next
Set fso = Nothing
Set folder = Nothing
Set subfolders = Nothing
I tried files in the same folder, files in different folders, creating new files. Tried some of the answers from the other articles. No success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论