微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

VBA一致地打开文件

如何解决VBA一致地打开文件

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

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

第二次运行该命令时,您遇到的问题是workbooks.open (FiletoLoad)上什么都没有发生,或者弹出窗口说oldcfile.xlsm已经存在,您是否仍要保存?这意味着它卡在了autore封面中,在这种情况下,我必须打开一个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)
   .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,7).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2,7).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=7,5).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(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,11).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2,11).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=11,9).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(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,14).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2,14).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=14,12).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(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,20).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2,20).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=20,18).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(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,24).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2,24).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=24,22).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(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,"""")"
   '.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,0)
   '.Cells(2,28).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(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,39).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-2]=RC[-1]"
   .Cells(2,39).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).AutoFilter Field:=39,37).Resize(lrow - 1).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(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

结束子

解决方法

似乎您将工作簿引用存储到很晚。 最好的时间就是打开工作簿时,因为.Open方法提供了对打开的工作簿的引用!

Set wb = Workbooks.Open(FiletoLoad) 

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

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

始终使用explicit references,而不是隐式的,例如:

ActiveSheet.Name = "Sheet1"

明确的内容:

ActiveWorkbook.ActiveWorksheet.Name = "Sheet1"

没有任何保证,即期的书/页是您所期望的!

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

最好像稍后那样使用With语句,但要使用显式对象。

With wb.Worksheets("Sheet1")

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

尽管我喜欢vbNullString中的""(胜过If Folderpath = vbNullString Then) 我更喜欢通过检查其长度(0为空)来检查空字符串:

If Len(FolderPath) = 0 Then

或使用变体检查“空”或“空”(NULL & vbNullStringvbNullString):

If Len(VariantFolderPath & vbNullString) = 0 Then

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。