[英]VBA Opening files consistantly
我有一個奇怪的問題,我真的不知道如何解決它。 我有一個 Access 數據庫,您可以在其中使用 select 標准來運行報告。 IE 你可以 select 年份,公司和兩位數的報價。
報告運行后,我設置了一種將其與您選擇的報告進行比較的方法。 第一次運行它時,它運行良好。
第二次運行它時,您會遇到一個問題,即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
結束子
似乎您將工作簿引用存儲到了后期。 最好的時機是,當您打開 Workbook 時,因為.Open
方法提供了對打開的工作簿的引用!
Set wb = Workbooks.Open(FiletoLoad)
當然,工作表應該在打開時保存:
Set ws = wb.ActiveSheet
wb.Same = "Sheet1"
始終使用顯式引用,而不是像下面這樣的隱式引用:
ActiveSheet.Name = "Sheet1"
什么是明確的:
ActiveWorkbook.ActiveWorksheet.Name = "Sheet1"
沒有任何保證活動的書/表是您期望的!
通過CodeName引用工作表也是一種有用的模式'Formatting for bump
之后的部分特別臟,因為您之前激活了工作簿,但隨后您隱式引用.ActiveSheet
任何當時的內容。
最好使用With
-Statement,就像稍后所做的那樣,但使用顯式對象。
With wb.Worksheets("Sheet1")
您應該知道,包含空格的路徑需要特殊服務,例如從 shell 中使用! 最好避免!
雖然我喜歡vbNullString
(優於""
)在If Folderpath = vbNullString Then
我更喜歡通過檢查它的長度來檢查空字符串(0為空):
If Len(FolderPath) = 0 Then
或檢查 Null 或 Empty 的變體( NULL & vbNullString
是vbNullString
):
If Len(VariantFolderPath & vbNullString) = 0 Then
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.