【问题标题】:Compile Error: Duplicate declaration in current scope编译错误:当前范围内的重复声明
【发布时间】:2015-09-15 21:01:24
【问题描述】:

我正在尝试从选定工作簿的多个工作表中收集数据。我正在使用以下代码:

Sub Multiplesheet()

Dim filepath As Variant
Dim outputFilePath As String
Dim outputSheetName As String
Dim sql As String
Dim wbk As Workbook, wks As Worksheet
Dim rng As Excel.Range
Dim sheetname As Variant

'To which file and sheet within the file should the output go?
outputFilePath = "C:\Users\z003k50s\Desktop\Test\Output.xlsx"
outputSheetName = "Sheet1"

For Each filepath In Application.GetOpenFilename(FileFilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
    Set schema = conn.OpenSchema(adSchemaTables)
    For Each sheetname In schema.GetRows(, , "TABLE_NAME") 'returns a 2D array of one column
        sql = sql & _
            "UNION ALL SELECT F1 " & _
            "FROM [" & sheetname & "]" & _
                "IN """ & filepath & """ ""Excel 12.0;"""
    Next
Next
sql = Mid(sql, 5) 'Gets rid of the UNION ALL from the first SQL

Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset
 With conn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=""" & filepath & """;" & _
        "Extended Properties=""Excel 12.0;HDR=No"""
    .Open
    Set rs = .Execute(sql)
    Set wbk = Workbooks.Open(outputFilePath, , True)
    Set wks = wbk.Sheets(outputSheetName)
    wks.Cells(2, 1).CopyFromRecordset rs
    wks.Columns.AutoFill
    .Close
End With

End Sub

当我调试时它会突出显示:

 conn As New ADODB.Connection

我是 Excel VBA 的新手,我不知道它是什么意思。

【问题讨论】:

  • 正如它所说的 - 你复制了一个声明。在您的代码中,conn 在您 Dim 它之前已经使用过,所以要么已经有一个名为 conn 的公共变量,conn 在您 Dim 它之前被用作此代码中的变体会导致编译错误,或者您没有向我们展示完整的代码。
  • Option Explicit 放在声明部分的模块表顶部。还建议您进入工具 ► 选项并在 Require Variable Declaration 旁边打勾,以避免将来出现这些问题。
  • 我已经给你们发了完整的代码,但我看不到我在哪里复制了声明。
  • 如果你不需要在使用前声明你的变量,那么它们会被即时创建,因为 connSet schema = conn.OpenSchema(adSchemaTables)。然后,您尝试在几行之后将其声明为Dim conn As New ADODB.Connection,但它已经通过它的使用被“声明”了。

标签: excel excel-2007 vba


【解决方案1】:

您的代码片段:


For Each filepath In Application.GetOpenFilename(FileFilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
'----------
    Set schema = conn.OpenSchema(adSchemaTables) '<~~~ HERE YOU HAVE USED 'conn'
'----------

    For Each sheetname In schema.GetRows(, , "TABLE_NAME") 'returns a 2D array of one column
        sql = sql & _
            "UNION ALL SELECT F1 " & _
            "FROM [" & sheetname & "]" & _
                "IN """ & filepath & """ ""Excel 12.0;"""
    Next
Next
sql = Mid(sql, 5) 'Gets rid of the UNION ALL from the first SQL

'---------
Dim conn As New ADODB.Connection '<~~~ already exists, so duplicate declaration
'---------

Dim rs As ADODB.Recordset
 With conn

从我的 cmets 中可以看出,您使用了 conn,然后尝试对其进行维度 (Dim),这就是您收到编译错误的原因。

【讨论】:

  • 谢谢,现在我解决了这个问题,但现在我在运行宏时遇到了另一个错误。当我尝试运行它时,它带有编译错误:Argument not optional,当我调试它时,它在wks.Columns.AutoFill 行中突出显示AutoFill
  • AutoFill 方法需要一个目的地——否则它怎么知道填写到哪里?类似wks.Columns.AutoFill Range("A1:D10") 这是它的 MSDN 文章:msdn.microsoft.com/en-us/library/office/ff195345.aspx
  • 该死的,我一个接一个地出错,我已经为此工作了大约一个星期,而且我一直在出错。我想从选定工作簿中的多个工作表中复制数据,但我无法让它工作,Stackoverflow 上的一个人已经尝试帮助我,但我一直出错。
  • 我会提出一个单独的问题,因为它超出了这个问题的范围,但你基本上只需要搜索“循环浏览工作簿中的所有页面”,你应该找到你正在寻找的.
  • 我在一周或更长时间前提出了一个单独的问题,一个人试图帮助我,不幸的是没有运气。 stackoverflow.com/questions/32396339/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 2011-10-08
相关资源
最近更新 更多