2017-01-26 39 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

相关问题