Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA始终如一地打开文件_Excel_Vba_Ms Access - Fatal编程技术网

Excel VBA始终如一地打开文件

Excel VBA始终如一地打开文件,excel,vba,ms-access,Excel,Vba,Ms Access,我有一个奇怪的问题,我真的不知道如何解决它。 我有一个Access数据库,您可以在其中选择要运行的报告的条件。 即您可以选择年份、公司和两位数的报价 报告运行后,我设置了一种方法,将其与您选择的报告进行比较。 第一次运行它时,它工作得非常好 第二次运行时,您可能会遇到一个问题,workbooks.open(FiletoLoad)上什么也没有发生,或者会出现一个弹出窗口,说oldcfile.xlsm已经存在,是否仍要保存?这意味着它被卡在自动恢复封面中,在这种情况下,我必须打开一个excel文件并

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

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

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

我曾试图删除oldcfile.xlsm以避开自动恢复问题,但最终选择后该文件无法打开

我试着用我能想到的所有可能的方法来调整它如何打开文件。 我知道一定有办法让这项工作成功,但我已经做了好几个小时了,我被难倒了

更新: 因此,只要我在末尾的End子行之前手动停止代码,代码就会正确地连续运行。如果我允许(即F8)结束子然而,那么它不会打开我选择的文件

有人见过/有过这个问题吗?如果是的话,你是如何修复的

更新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

End Sub

看起来您很晚才存储工作簿引用。 打开工作簿的最佳时间是
。open
方法提供对已打开工作簿的引用

Set wb=workbook.Open(FiletoLoad)
当然,图纸应在打开时保存:

Set ws=wb.ActiveSheet
wb.Same=“Sheet1”
始终使用,而不是隐式使用,如:

ActiveSheet.Name=“Sheet1”
什么是明确的:

ActiveWorkbook.ActiveWorksheet.Name=“Sheet1”
没有任何保证活动图书/工作表是您期望的

按图纸引用图纸也是一种有用的模式 bump格式设置后的部分特别脏,因为您之前激活了工作簿,但随后您隐式引用了
.ActiveSheet
当时的任何内容

最好将
-语句一起使用,就像后面所做的那样,但使用显式对象

与wb.工作表(“表1”)
您应该知道,包含空格的路径需要特殊服务,例如,如果从shell使用!最好避免

虽然我喜欢
中的
vbNullString
(优于
),但如果Folderpath=vbNullString,则

我更喜欢通过检查其长度来检查空字符串(0为空):

如果Len(FolderPath)=0,则
或者使用变体检查Null或空(
Null&vbNullString
is
vbNullString
):

如果Len(VariantFolderPath&vbNullString)=0,则

您似乎很晚才存储工作簿引用。 打开工作簿的最佳时间是
。open
方法提供对已打开工作簿的引用

Set wb=workbook.Open(FiletoLoad)
当然,图纸应在打开时保存:

Set ws=wb.ActiveSheet
wb.Same=“Sheet1”
始终使用,而不是隐式使用,如:

ActiveSheet.Name=“Sheet1”
什么是明确的:

ActiveWorkbook.ActiveWorksheet.Name=“Sheet1”
没有任何保证活动图书/工作表是您期望的

按图纸引用图纸也是一种有用的模式 bump
格式设置后的部分特别脏,因为您之前激活了工作簿,但随后您隐式引用了
.ActiveSheet
当时的任何内容

最好将
-语句一起使用,就像后面所做的那样,但使用显式对象

与wb.工作表(“表1”)
您应该知道,包含空格的路径需要特殊服务,例如,如果从shell使用!最好避免

虽然我喜欢
中的
vbNullString
(优于
),但如果Folderpath=vbNullString,则

我更喜欢通过检查其长度来检查空字符串(0为空):

如果Len(FolderPath)=0,则
或者使用变体检查Null或空(
Null&vbNullString
is
vbNullString
):

如果Len(VariantFolderPath&vbNullString)=0,则

您在哪里为
PathSeason
变量赋值?您是否从access运行代码?我的工作簿是什么?打开Excel会话?否则,Application.DisplayAlerts`就足够了。尝试调试时,
Application.DisplayAlerts=False
根本没有帮助。您可以再次将其设置为
False
,除非所有操作(除了一些不需要的消息)都运行顺利…我看不到涉及任何Ms访问?通常会生成报告there@FaneDuru
PathSeason
是这段代码的另一部分,整个代码大约有500行,我不想把所有的东西都扔掉,压倒所有人,因为这部分起作用了,所以我把它删掉了。我在原始帖子中的内容在Access查询运行后添加。您在哪里为
PathSeason
变量赋值?您是否从access运行代码?我的工作簿是什么?打开Excel会话?否则,Application.DisplayAlerts`就足够了。尝试调试时,
Application.DisplayAlerts=False
根本没有帮助。您可以再次将其设置为
False
,除非所有操作(除了一些不需要的消息)都运行顺利…我看不到任何Ms Access发票