简体   繁体   中英

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. IE You can select year, company and a two digit offer.

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? 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.

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.

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. If I allow (IE F8) End Sub however, then it won't open the file I selected.

Has anyone seen/had this issue? if so how did you fix it?

UPDATE 2: I figured out the issue with running it consecutively. I had to add appExcel to the line Set oldcfile = Workbooks.open so now it's Set oldcfile = appExcel.Workbooks.Open(FiletoLoad)

The problem now is that my VLOOKUPS don't work. They won't fill in on the Myworkbook file.

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!

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.

Better use a With -Statement, like done later, but with explicit objects.

With wb.Worksheets("Sheet1")

You should be aware, that paths containing blanks, need special service, if eg used from a 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):

If Len(FolderPath) = 0 Then

or with variants checking for Null or Empty ( NULL & vbNullString is vbNullString ):

If Len(VariantFolderPath & vbNullString) = 0 Then

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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