简体   繁体   English

VBA 一致地打开文件

[英]VBA Opening files consistantly

I've got a strange issue and I'm really not sure how to fix it.我有一个奇怪的问题,我真的不知道如何解决它。 I have an Access database where you can select criteria for a report to run.我有一个 Access 数据库,您可以在其中使用 select 标准来运行报告。 IE You can select year, company and a two digit offer. IE 你可以 select 年份,公司和两位数的报价。

After the report runs I've setup a way to compare it to a report you choose.报告运行后,我设置了一种将其与您选择的报告进行比较的方法。 The first time you run it, it works perfectly.第一次运行它时,它运行良好。

The second time you run it you either get an issue where nothing happens on workbooks.open (FiletoLoad) or you get a pop up saying that oldcfile.xlsm already exists do you want to save anyway?第二次运行它时,您会遇到一个问题,即workbooks.open (FiletoLoad)上没有任何反应,或者您会弹出一个消息说 oldcfile.xlsm 已经存在,您仍然要保存吗? Which means that it got stuck in the autore cover in which case I have to open an excel file and close the auto recover pane.这意味着它卡在了自动恢复封面中,在这种情况下,我必须打开 excel 文件并关闭自动恢复窗格。

I've tried to delete the oldcfile.xlsm to get around the auto recovery issue, but then I end up with the file just not opening after selection.我试图删除 oldcfile.xlsm 以解决自动恢复问题,但最终我发现文件在选择后无法打开。

I've tried to tweak how it opens the file every possible way I can think of.我试图以我能想到的每一种可能的方式来调整它打开文件的方式。 I know there has to be a way to make this work but I've been at it for hours and I'm stumped.我知道必须有一种方法来完成这项工作,但我已经做了好几个小时了,我被难住了。

UPDATE: So the code will run correctly and consecutively as long as I manually stop it before the End Sub line at the end.更新:因此,只要我在最后的 End Sub 行之前手动停止代码,代码就会正确且连续地运行。 If I allow (IE F8) End Sub however, then it won't open the file I selected.但是,如果我允许 (IE F8) End Sub,那么它将不会打开我选择的文件。

Has anyone seen/had this issue?有没有人看到/遇到过这个问题? if so how did you fix it?如果是这样,您是如何解决的?

UPDATE 2: I figured out the issue with running it consecutively.更新2:我发现了连续运行它的问题。 I had to add appExcel to the line Set oldcfile = Workbooks.open so now it's Set oldcfile = appExcel.Workbooks.Open(FiletoLoad)我必须将appExcel添加到Set oldcfile = Workbooks.open行,所以现在它是Set oldcfile = appExcel.Workbooks.Open(FiletoLoad)

The problem now is that my VLOOKUPS don't work.现在的问题是我的 VLOOKUPS 不起作用。 They won't fill in on the Myworkbook file.他们不会填写 Myworkbook 文件。

Any help would be greatly appreciated!任何帮助将不胜感激!

The full code I'm using for this compare process is as follows:我用于此比较过程的完整代码如下:

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结束子

Seems like you store Workbook references to late.似乎您将工作簿引用存储到了后期。 Best time is, when you open the Workbook, as .Open method provides a reference to the opened workbook!最好的时机是,当您打开 Workbook 时,因为.Open方法提供了对打开的工作簿的引用!

Set wb = Workbooks.Open(FiletoLoad) 

Of course the Sheet should be saved at opening:当然,工作表应该在打开时保存:

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

Always use explicit references , not implicit like in:始终使用显式引用,而不是像下面这样的隐式引用:

ActiveSheet.Name = "Sheet1"

What is explicit:什么是明确的:

ActiveWorkbook.ActiveWorksheet.Name = "Sheet1"

without any guaranty that active book/sheet is the one you expect!没有任何保证活动的书/表是您期望的!

Referencing a sheet by its CodeName is a usefull pattern too The Part after 'Formatting for bump is especially dirty, as you activate a workbook before, but then you implicit reference .ActiveSheet whatever that is at that time.通过CodeName引用工作表也是一种有用的模式'Formatting for bump之后的部分特别脏,因为您之前激活了工作簿,但随后您隐式引用.ActiveSheet任何当时的内容。

Better use a With -Statement, like done later, but with explicit objects.最好使用With -Statement,就像稍后所做的那样,但使用显式对象。

With wb.Worksheets("Sheet1")

You should be aware, that paths containing blanks, need special service, if eg used from a shell!您应该知道,包含空格的路径需要特殊服务,例如从 shell 中使用! Best to avoid!最好避免!

Although I like vbNullString (better than "" ) in If Folderpath = vbNullString Then I prefer to check for empty strings by checking its length (0 is empty):虽然我喜欢vbNullString (优于"" )在If Folderpath = vbNullString Then我更喜欢通过检查它的长度来检查空字符串(0为空):

If Len(FolderPath) = 0 Then

or with variants checking for Null or Empty ( NULL & vbNullString is vbNullString ):或检查 Null 或 Empty 的变体( NULL & vbNullStringvbNullString ):

If Len(VariantFolderPath & vbNullString) = 0 Then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM