首页 > 解决方案 > VBA 一致地打开文件

问题描述

我有一个奇怪的问题,我真的不知道如何解决它。我有一个 Access 数据库,您可以在其中选择要运行的报告的条件。IE 您可以选择年份、公司和两位数的报价。

报告运行后,我设置了一种将其与您选择的报告进行比较的方法。第一次运行它时,它运行良好。

第二次运行它时,您会遇到什么都没有发生的问题,workbooks.open (FiletoLoad)或者您会弹出一个消息说 oldcfile.xlsm 已经存在,您仍然要保存吗?这意味着它卡在了自动恢复封面中,在这种情况下,我必须打开一个 excel 文件并关闭自动恢复窗格。

我试图删除 oldcfile.xlsm 以解决自动恢复问题,但最终我发现文件在选择后无法打开。

我试图以我能想到的每一种可能的方式来调整它打开文件的方式。我知道必须有一种方法来完成这项工作,但我已经做了好几个小时了,我被难住了。

更新:因此,只要我在最后的 End Sub 行之前手动停止代码,代码就会正确且连续地运行。但是,如果我允许 (IE F8) End Sub,那么它将不会打开我选择的文件。

有没有人看到/遇到过这个问题?如果是这样,您是如何解决的?

更新2:我发现了连续运行它的问题。我不得不添加appExcel到该行Set oldcfile = Workbooks.open,所以现在它是Set oldcfile = appExcel.Workbooks.Open(FiletoLoad)

现在的问题是我的 VLOOKUPS 不起作用。他们不会填写 Myworkbook 文件。

任何帮助将不胜感激!

我用于此比较过程的完整代码如下:

Dim nConfirmation As Integer
Dim MyFolder As String
Dim oldcfile As Workbook
Dim newcfile As Workbook
Dim FiletoLoad As Variant
Dim PathCreative As String
Dim Folderpath As String
Dim Directory As String
Dim ws As Worksheet

lrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

'Run Creative file Bumps?
nConfirmation = MsgBox("Would you like to bump your retail file?", vbSystemModal + vbInformation + vbYesNo, "Bump Retail File?")
If nConfirmation = vbYes Then

'Current Creative File
myWorkbook.Application.DisplayAlerts = False
   
'Open File to Bump against

With myWorkbook.Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    .InitialFileName = "\\page\data\NFInventory\groups\CID\Retail Setting\Lago Retail Uploads\" & strBrand & "\" & strSeason & "\" & strPrefix & "\"
    
    If .Show = True Then
        FiletoLoad = .SelectedItems(1)
        Set oldcfile = appExcel.Workbooks.Open(FiletoLoad)
    End If
 End With

 Set ws = oldcfile.ActiveSheet
 ws.Name = "Sheet1"

 myWorkbook.Application.DisplayAlerts = True
    
 'Formatting for bump
  With myWorkbook.Worksheets("qryCreativeFileBuild")
  myWorkbook.Activate
  lrow = .Cells(Rows.Count, 2).End(xlUp).Row
    .Columns("A:AC").AutoFilter
    .Columns("C:D").Insert Shift:=xlRight
    .Columns("F:G").Insert Shift:=xlRight
    .Columns("J:K").Insert Shift:=xlRight
    .Columns("M:N").Insert Shift:=xlRight
    .Columns("S:T").Insert Shift:=xlRight
    .Columns("W:X").Insert Shift:=xlRight
    .Columns("AC:AD").Insert Shift:=xlRight
    .Columns("AL:AM").Insert Shift:=xlRight
        
'Verification of Data and highlight differences
'Pack Check
   .Cells(2, 3).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],'[" & oldcfile.Name & "]" & ws.Name & "'!C2:C2,1,0),"""")"
   .Cells(2, 4).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2, 4).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=4, Criteria1:="FALSE"
   .Cells(2, 1).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 0, 0)
   .Cells(2, 2).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 255, 25)
   ActiveSheet.ShowAllData
   
'Per Months Check
   .Cells(2, 6).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-4],'[" & oldcfile.Name & "]Sheet1'!C2:C3,2,0),"""")"
   .Cells(2, 7).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2, 7).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=7, Criteria1:="FALSE"
   .Cells(2, 1).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 0, 0)
   .Cells(2, 5).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 255, 25)
   ActiveSheet.ShowAllData
   
'Original Retail Check
   .Cells(2, 10).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-8],'[" & oldcfile.Name & "]Sheet1'!C2:C5,4,0),"""")"
   .Cells(2, 11).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2, 11).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=11, Criteria1:="FALSE"
   .Cells(2, 1).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 0, 0)
   .Cells(2, 9).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 255, 25)
   ActiveSheet.ShowAllData
   
'EB Retail Check
   .Cells(2, 13).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-11],'[" & oldcfile.Name & "]Sheet1'!C2:C6,5,0),"""")"
   .Cells(2, 14).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2, 14).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=14, Criteria1:="FALSE"
   .Cells(2, 1).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 0, 0)
   .Cells(2, 12).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 255, 25)
   ActiveSheet.ShowAllData
   
'Savings % Check
   .Cells(2, 19).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-17],'[" & oldcfile.Name & "]Sheet1'!C2:C10,9,0),"""")"
   .Cells(2, 20).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2, 20).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=20, Criteria1:="FALSE"
   .Cells(2, 1).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 0, 0)
   .Cells(2, 18).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 255, 25)
   ActiveSheet.ShowAllData
   
'Retail Check
   .Cells(2, 23).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-21],'[" & oldcfile.Name & "]Sheet1'!C2:C12,11,0),"""")"
   .Cells(2, 24).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2, 24).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=24, Criteria1:="FALSE"
   .Cells(2, 1).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 0, 0)
   .Cells(2, 22).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 255, 25)
   ActiveSheet.ShowAllData
   
'Totals Check
   '.Cells(2, 29).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-27],'[oldcfile.xlsm]Sheet1'!C2:C16,15,0),"""")"
   '.Cells(2, 30).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   '.Cells(2, 30).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=30, Criteria1:="FALSE"
   '.Cells(2, 1).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 0, 0)
   '.Cells(2, 28).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 255, 25)
   'ActiveSheet.ShowAllData
   
'DRC Check
   .Cells(2, 38).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-36],'[" & oldcfile.Name & "]Sheet1'!C2:C23,22,0),"""")"
   .Cells(2, 39).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2, 39).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=39, Criteria1:="FALSE"
   .Cells(2, 1).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 0, 0)
   .Cells(2, 37).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 255, 25)
   ActiveSheet.ShowAllData

'Remove Rows
    .Columns("C:D").Delete Shift:=xlLeft
    .Columns("D:E").Delete Shift:=xlLeft
    .Columns("F:G").Delete Shift:=xlLeft
    .Columns("G:H").Delete Shift:=xlLeft
    .Columns("K:L").Delete Shift:=xlLeft
    .Columns("M:N").Delete Shift:=xlLeft
    .Columns("Q:R").Delete Shift:=xlLeft
    .Columns("X:Y").Delete Shift:=xlLeft
    
End With


'Save New file
'myWorkbook.SaveAs ("\\page\data\NFInventory\groups\CID\Retail Setting\Lago Retail Uploads\" & strBrand & "\" & strSeason & "\" & strPrefix & "\" & strPrefix & " " & Format(CStr(Now), "MM_DD_YYYY_hhmmss") & " Creative.xlsx")

'Close Bumped File
oldcfile.Close SaveChanges:=False

myWorkbook.Activate
Else
myWorkbook.SaveAs ("\\page\data\NFInventory\groups\CID\Retail Setting\Lago Retail Uploads\" & strBrand & "\" & strSeason & "\" & strPrefix & "\" & strPrefix & " " & Format(CStr(Now), "MM_DD_YYYY_hhmmss") & " Creative.xlsx")
End If


'Set appExcel = Nothing
'Set myWorkbook = Nothing
Set db = CurrentDb
db.Close

结束子

标签: excelvbams-access

解决方案


似乎您将工作簿引用存储到了后期。最佳时机是,当您打开 Workbook 时,as.Open方法提供了对打开的工作簿的引用!

Set wb = Workbooks.Open(FiletoLoad) 

当然,工作表应该在打开时保存:

Set ws = wb.ActiveSheet
wb.Same = "Sheet1"

始终使用显式引用,而不是像下面这样的隐式引用:

ActiveSheet.Name = "Sheet1"

什么是明确的:

ActiveWorkbook.ActiveWorksheet.Name = "Sheet1"

没有任何保证活动的书/表是您期望的!

通过代码名引用工作表也是一种有用的模式。之后的部分'Formatting for bump特别脏,因为您之前激活了工作簿,但随后您隐式引用.ActiveSheet了当时的任何内容。

最好使用With-Statement,就像稍后所做的那样,但使​​用显式对象。

With wb.Worksheets("Sheet1")

您应该知道,包含空格的路径需要特殊服务,例如从 shell 中使用!最好避免!

虽然我喜欢vbNullString(优于"") inIf Folderpath = vbNullString Then 我更喜欢通过检查其长度来检查空字符串(0 为空):

If Len(FolderPath) = 0 Then

或使用变体检查 Null 或 Empty ( NULL & vbNullStringis vbNullString):

If Len(VariantFolderPath & vbNullString) = 0 Then

推荐阅读