2017-01-26 42 views
0

我一直在這一天工作,似乎無法弄清楚。這就是我要做的(應該是很簡單):VBA - 應用程序定義或對象定義錯誤?

兩個工作簿:一個已經(的ThisWorkbook)打開,一個通過Application.FileDialog(msoFileDialogOpen)開(我們將調用這個練習冊2)

  1. 如果列K在ThisWorkbook中爲空,「Sheet1」然後分別在工作簿2「503雜項」,列B和G中的列M和列P中搜索值。
  2. 如果Workbook2中的值匹配「503 Sundry」,則將H列和I列中的值複製並粘貼到ThisWorkbook中。「Sheet1」分別爲列I和K。

這是我到目前爲止,但我不斷收到錯誤消息「應用程序定義或對象定義的錯誤」下面的粗體行代碼。

Sub JPlan() 

    Dim wb1 As Workbook 
    Dim wb2 As Workbook 
    Dim cell1 As Range, rng1 As Range, cell2 As Range, rng2 As Range 
    Dim Cel As Range 
    Dim Sht1 As Worksheet 
    Dim SundrySht As Worksheet 

    Set wb1 = ThisWorkbook 

    With Application.FileDialog(msoFileDialogOpen) 
     .AllowMultiSelect = False 
     If .Show Then 
      Filename = .SelectedItems(1) 
      Set wb2 = Workbooks.Open(Filename) 
     Else 
      Exit Sub 
     End If 
    End With 

    Set Sht1 = wb1.Sheets("Sheet1") 
    Set SundrySht = wb2.Sheets("503 Sundry") 

    Set Cel = Sht1.Range("P2") 
    Set rng1 = Range(Cel, Cel.Offset(Sht1.Cells.Rows.Count - Cel.Row, 0).End(xlUp)) 
    Set Cel = SundrySht.Range("G2") 
    Set rng2 = Range(Cel, Cel.Offset(SundrySht.Cells.Rows.Count - Cel.Row, 0).End(xlUp)) 

    If Sht1.Cells(i, 11) = "" Then 'if current cell in column 11 is empty then... 
    For Each cell2 In rng2  'for each cell in range 2 defined above (column G in "503 Sundry")... 
     For Each cell1 In rng1  'for each cell in range 1 defined above (column P in Sheet 1)... 
     If cell2.Value = cell1.Value And cell2.Offset(0, -5) = cell1.Offset(0, -3).Value Then 'if the value of cell2 equals the value of cell1 AND the value of cell2 (offset by 5 columns) equals the value of cell1 (offset by 3 columns) then... 
      cell1.Offset(0, -7).Value = cell2.Offset(0, 1).Value 'from to Sundry column H to Sheet1 column I 
      cell1.Offset(0, -5).Value = cell2.Offset(0, 2).Value 'from to Sundry column I to Sheet1 column K 
      Exit For 
     End If 
     Next 
    Next 
    End If 

End Sub 
+0

代碼視圖做語法高亮,但是你可能已經應用的任何粗體都會丟失。你能找到一些其他方式來指出錯誤嗎? – teylyn

+1

這裏有[938其他現有的帖子](http://stackoverflow.com/search?q=%5Bvba%5D+application+defined+or+object+defined+error),包含精確的錯誤信息。你有多少人在發佈另一篇文章之前,看過他們是否回答了你的問題? –

+0

看起來像只在if子句的一部分中設置了wb2。如果你從未設置它,會發生什麼?最有可能的是1004錯誤lol –

回答

0

至於有沒有在你的問題,表示該線產生的1004錯誤,我會認爲這是在該行發生的話說If Sht1.Cells(i, 11) = "" Then。此行將失敗,因爲您尚未聲明變量i,更重要的是,您從未爲其分配值。因此,它將假定iVariant(沒有什麼特別不好,但讓Excel猜測使用哪種變量類型不是一個好習慣),其值爲零。 Excel不理解「行0」的概念,因此引發了1004錯誤。

通過將Option Explicit作爲每個代碼模塊的第一行,可以避免(或至少減少)這些問題。這告訴編譯器拋出一個錯誤,突出顯示你的代碼中沒有明確聲明的變量。 (這也將強調,你有沒有聲明的變量Filename的事實。)

下重構的代碼避免了要麼你的兩位目前未聲明的變量的需要:

'In order to prevent mistakes due to undefined variables, 
'include this as the first line in each code module 
Option Explicit 

Sub JPlan() 
    Dim wb1 As Workbook 
    Dim wb2 As Workbook 
    Dim cell1 As Range, rng1 As Range, cell2 As Range, rng2 As Range 

    Set wb1 = ThisWorkbook 

    With Application.FileDialog(msoFileDialogOpen) 
     .AllowMultiSelect = False 
     If .Show Then 
      '"Filename" wasn't declared, so I just merged the two lines 
      ' rather than adding a "Dim Filename As String" line 
      Set wb2 = Workbooks.Open(.SelectedItems(1)) 
     Else 
      Exit Sub 
     End If 
    End With 

    With wb1.Sheets("Sheet1") 
     Set rng1 = .Range(.Cells(2, "P"), .Cells(.Rows.Count, "P").End(xlUp)) 
    End With 
    With wb2.Sheets("503 Sundry") 
     Set rng2 = .Range(.Cells(2, "G"), .Cells(.Rows.Count, "G").End(xlUp)) 
    End With 

    'Changing the order of the loops avoids the need for the variable "i" 
    For Each cell1 In rng1  'for each cell in range 1 defined above (column P in Sheet 1)... 
     If cell1.Offset(0, -5).Value = "" Then 'if current cell in column 11 is empty then... 
      For Each cell2 In rng2  'for each cell in range 2 defined above (column G in "503 Sundry")... 
       If cell2.Value = cell1.Value And _ 
        cell2.Offset(0, -5).Value = cell1.Offset(0, -3).Value Then 'if the value of cell2 equals the value of cell1 AND the value of cell2 (offset by 5 columns) equals the value of cell1 (offset by 3 columns) then... 
        cell1.Offset(0, -7).Value = cell2.Offset(0, 1).Value 'from to Sundry column H to Sheet1 column I 
        cell1.Offset(0, -5).Value = cell2.Offset(0, 2).Value 'from to Sundry column I to Sheet1 column K 
        Exit For 
       End If 
      Next 
     End If 
    Next 

End Sub 
+0

所以...很好新聞是你的代碼工程...但我有一些更大的問題,我相信它是格式化。如果我將Workbook2中的隨機NHA和PN複製並粘貼到列M和P中,然後運行代碼將數據拉入。但是,如果我從Woorkbook1中將相同的NHA和PN拉出並粘貼到列M和P中並運行它沒有提取任何數據的代碼。這甚至有意義嗎?是格式問題嗎? @ YowE3K – CC268

+0

@ CC268我不知道「NHA」和「PN」是什麼,但是你對格式化的評論讓我覺得你可能在一張紙上有文字,在另一張上有數字。如果是,請嘗試更改'If cell2.Value = cell1.Value and cell2.Offset(0,-5).Value = cell1.Offset(0,-3).Value Then'' if if cell2.Text = cell1。文本和cell2。Offset(0,-5).Text = cell1.Offset(0,-3).Text Then'。這將比較單元格中**顯示的**內容,而不是單元格的**實際**內容。 – YowE3K

+0

非常感謝您的幫助@ YowE3K。對此,我真的非常感激。我終於開始工作,你的代碼是完美的。原來工作簿2中的列M和P中的值被格式化爲非常奇怪。我不得不使用TRIM函數,因爲單元格中有各種奇怪的空格。這就是爲什麼我在運行代碼時沒有選擇它。反正現在一切都很好! – CC268

相關問題