当工作簿已打开时,VBA集工作簿无法正确分配

时间:2018-01-25 05:10:03

标签: excel vba excel-vba loops set

这可能会成为一个初学者的问题,我想我在这里缺少一些基本的东西。

用户选择不同的Excel文件,这些文件名存储在字符串数组fileNames中。然后,宏将用于遍历所选文件并将特定工作表转换为XML。

Application.ScreenUpdating = False

'Select Leveling pattern files from the file system
Dim fileNames() As String
fileNames = ChooseLevelingPatternFiles()

'Create new .xml-file
Dim pathDelivList As String
pathDelivList = "X:\..\..\DeliveryList.xml"

Open pathDelivList For Output As #1
'Print XML header into new file
Print #1, "<?xml version=""1.0"" encoding=""ISO-8859-1"" standalone=""yes""?>"
Print #1, "<StramaDeliveryPlans>"

'Create new .xml-file 
Dim pathShiftInfo As String
pathShiftInfo = "X:\..\..\ShiftInformationFromLP.xml"

Open pathShiftInfo For Output As #2
'Print XML header into new file
Print #2, "<?xml version=""1.0"" encoding=""ISO-8859-1"" standalone=""yes""?>"
Print #2, "<ShiftInformationFromLevelingPlans>"

'Define Object for Target Workbook and Target Worksheet
Dim Target_Workbook As Workbook
Dim Target_Worksheet As Worksheet
Dim Target_Path as String, currentLPType() As String


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Enter loop through different files
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim i As Integer
For i = 0 To UBound(fileNames) - 1

   'Change the path of the target file and assign the Leveling Pattern worksheet to the variable
   Target_Path = fileNames(i)

   Application.DisplayAlerts = False
   Set Target_Workbook = Nothing
   Set Target_Workbook = Workbooks.Open(Filename:=Target_Path, UpdateLinks:=xlUpdateLinksNever, ReadOnly:=True, Notify:=True)
   Set Target_Worksheet = Nothing
   Set Target_Worksheet = Target_Workbook.Sheets("Leveling Pattern")


   'Print title of current file
   currentLPType = Split(Target_Workbook.Name, " ")
   Print #1, "<" + currentLPType(0) + "_" + currentLPType(1) + ">"
   Print #2, "<" + currentLPType(0) + "_" + currentLPType(1) + ">"


   '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
   ' A lot of code only affecting the current sheet
   '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


   'Before closing the file close the containing xml element
   Print #1, "</" + currentLPType(0) + "_" + currentLPType(1) + ">"
   Print #2, "</" + currentLPType(0) + "_" + currentLPType(1) + ">"

   'Target_Workbook.Close False

   Application.DisplayAlerts = False
   DoEvents

Next i

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Exit loop through different files
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Close the .xml files
Print #1, "</StramaDeliveryPlans>"
Close #1
Print #2, "</ShiftInformationFromLevelingPlans>"
Close #2

Application.ScreenUpdating = True

End Sub

问题: Set Target_Workbook = Workbooks.Open(Target_Path)无法正常运作。 Workbook对象始终保持第一个分配,并在每次处理同一文件时进行处理。

我在作业之前尝试了Target_Workbook = Nothing但没有效果。 Target_Path = fileNames(i)工作正常。

我做错了什么?任何帮助表示赞赏。

EDIT2: 当目标工作簿已经打开时,问题似乎只是在发生,因此底层问题似乎是宏无法选择已经打开的文件。

0 个答案:

没有答案