Vba 为什么我会得到;编译错误:未定义变量";为了一个常数?

Vba 为什么我会得到;编译错误:未定义变量";为了一个常数?,vba,excel,csv,constants,Vba,Excel,Csv,Constants,以下代码使用固定文件夹中的csv文件导入/更新工作簿中的摘要表。我将代码分为多个子部分:一个用于导入csv文件,一个用于在再次导入时删除汇总表,另一个用于导入csv文件。我已将folderpath声明为常量。为什么我会出错 Option Explicit Private U As New U1 Const PB_UGE_CSV_FOLDER$ = "C:\pathexample" Sub PB_uge_import_click() If Dir(PB_UGE_CSV_FOLDE

以下代码使用固定文件夹中的csv文件导入/更新工作簿中的摘要表。我将代码分为多个子部分:一个用于导入csv文件,一个用于在再次导入时删除汇总表,另一个用于导入csv文件。我已将folderpath声明为常量。为什么我会出错

Option Explicit

Private U As New U1

Const PB_UGE_CSV_FOLDER$ = "C:\pathexample"


Sub PB_uge_import_click()

    If Dir(PB_UGE_CSV_FOLDER, vbDirectory) = "" Then
        MsgBox "CSV Folder " & PB_UGE_CSV_FOLDER & "does not exist", vbExclamation
        Exit Sub
    End If

    Dim csvFile$
    Dim wsCSV As Worksheet, wsImport As Worksheet
    Dim cnt%, csvName$

    U.Start

    Call PB_uge_deleteCsvSheets

    csvFile = Dir(PB_UGE_CSV_FOLDER & "\*.csv")

    Do While csvFile <> ""

        Call ImportToTempSheet(PB_UGE_CSV_FOLDER & "\" & csvFile)
        Set wsCSV = TempSheet2

        csvName = Mid(csvFile, InStrRev(csvFile, "\") + 1)
        csvName = Replace(csvName, ".csv", "", , , vbTextCompare)

        Call PB_uge_import(wsCSV, csvName)

        cnt = cnt + 1
        csvFile = Dir()
    Loop

    Call PB_uge_mergeIntoCSV

    MacroSheet.Activate
    U.Finish

    MsgBox cnt & " files imported/updated", vbInformation

End Sub

Private Sub PB_uge_update(wsCSV As Worksheet, wsImport As Worksheet)

    Dim iRow&

    With wsImport
        .Cells.Clear
        wsCSV.UsedRange.Copy

        .Range("A1").PasteSpecial xlPasteValues

        Application.CutCopyMode = False
    End With
End Sub

Private Sub PB_uge_import(wsCSV As Worksheet, csvName$)

    Dim wsImport As Worksheet

    ThisWorkbook.Sheets.Add after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set wsImport = ActiveSheet

    With wsImport
        .Name = csvName

        .Cells.Clear
        wsCSV.Cells.Copy

        .Range("A1").PasteSpecial xlPasteValues

        Application.CutCopyMode = False
    End With

    Application.Goto wsImport.Range("A1"), True
End Sub

Sub PB_uge_deleteCsvSheets()
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Sheets
        If ws.Name = PB_uge_SummarySheet.Name Then GoTo ContLoop
        If ws.Name = MacroSheet.Name Then GoTo ContLoop
        If ws.Name = TempSheet2.Name Then GoTo ContLoop

        ws.Delete

ContLoop:
    Next
End Sub

Sub PB_uge_mergeIntoCSV()
    Dim ws As Worksheet
    Dim bFirst As Boolean
    Dim sRow&, lrow&

    bFirst = True
    With PB_uge_SummarySheet
        If .FilterMode Then .ShowAllData
        .Cells.Clear

        For Each ws In ThisWorkbook.Sheets
            If ws.Name = PB_uge_SummarySheet.Name Then GoTo ContLoop
            If ws.Name = MacroSheet.Name Then GoTo ContLoop
            If ws.Name = TempSheet2.Name Then GoTo ContLoop

            If ws.FilterMode Then ws.ShowAllData

            If bFirst Then
                ws.Range("1:1").Copy .Range("A1")
                bFirst = False
            End If

            lrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
            sRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1

            ws.Range("A2:SA" & lrow).Copy
            .Range("A" & sRow).PasteSpecial
            Cells.EntireColumn.AutoFit

            ws.Visible = xlSheetHidden
ContLoop:
        Next

        Application.CutCopyMode = False
    End With
End Sub

Sub PB_uge_ImportToTempSheet(iFile$)
    TempSheet2.Cells.Clear

    With TempSheet2.QueryTables.Add(Connection:="TEXT;" & iFile, Destination:=TempSheet2.Range("$A$1"))
        .Name = "02093861"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

    Dim qt As QueryTable
    For Each qt In TempSheet2.QueryTables
        qt.Delete
    Next

End Sub
选项显式
作为新U1的私人U
Const PB_UGE_CSV_文件夹$=“C:\pathexample”
子PB_uge_导入_单击()
如果Dir(PB_UGE_CSV_文件夹,vbDirectory)=“”,则
MsgBox“CSV文件夹”和PB_UGE_CSV_文件夹&“不存在”,vb感叹号
出口接头
如果结束
暗csvFile$
将wsCSV设置为工作表,将wsCSV设置为工作表导入
Dim cnt%,CSV名称$
美国起步
调用PB_uge_deleteCsvSheets
csvFile=Dir(PB\u UGE\u CSV\u文件夹和“\*.CSV”)
在csvFile“”时执行此操作
调用ImportTempSheet(PB\U UGE\U CSV\U文件夹和“\”CSV文件)
设置wsCSV=TempSheet2
csvName=Mid(csvFile,InStrRev(csvFile,“\”)+1)
csvName=Replace(csvName、.csv、“”、、vbTextCompare)
调用PB_uge_导入(wsCSV、csvName)
cnt=cnt+1
csvFile=Dir()
环
调用PB_uge_mergeintocv
宏表。激活
美国完成
MsgBox cnt&“导入/更新的文件”,vbInformation
端接头
私有子PB_uge_更新(wsCSV作为工作表,wsImport作为工作表)
黯淡&
使用wsImport
.细胞,清除
wsCSV.UsedRange.Copy
.Range(“A1”).Paste特殊XLPaste值
Application.CutCopyMode=False
以
端接头
私有子PB_uge_导入(wsCSV作为工作表,csvName$)
导入为工作表
ThisWorkbook.Sheets.Add after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
设置wsImport=ActiveSheet
使用wsImport
.Name=csvName
.细胞,清除
wsCSV.Cells.Copy
.Range(“A1”).Paste特殊XLPaste值
Application.CutCopyMode=False
以
Application.Goto wsImport.Range(“A1”),True
端接头
子PB_uge_deleteCsvSheets()
将ws设置为工作表
对于此工作簿中的每个ws.Sheets
如果ws.Name=PB_uge_SummarySheet.Name,则转到ContLoop
如果ws.Name=MacroSheet.Name,则转到ContLoop
如果ws.Name=TempSheet2.Name,则转到ContLoop
ws.Delete
ContLoop:
下一个
端接头
子PB_uge_mergentocsv()
将ws设置为工作表
Dim bFirst作为布尔值
变暗sRow和lrow&
bFirst=True
带PB_uge_汇总表
If.FilterMode Then.ShowAllData
.细胞,清除
对于此工作簿中的每个ws.Sheets
如果ws.Name=PB_uge_SummarySheet.Name,则转到ContLoop
如果ws.Name=MacroSheet.Name,则转到ContLoop
如果ws.Name=TempSheet2.Name,则转到ContLoop
如果为ws.FilterMode,则为ws.ShowAllData
如果是第一次的话
ws.Range(“1:1”)。Copy.Range(“A1”)
bFirst=False
如果结束
lrow=ws.Cells(Rows.Count,“A”).End(xlUp).Row
sRow=.Cells(Rows.Count,“A”).End(xlUp).行+1
ws.Range(“A2:SA”和lrow)。复制
.Range(“A”和sRow).PasteSpecial
Cells.EntireColumn.AutoFit
ws.Visible=xlSheetHidden
ContLoop:
下一个
Application.CutCopyMode=False
以
端接头
次级PB_uge_进口临时表(iFile$)
TempSheet2.Cells.Clear
使用TempSheet2.QueryTables.Add(连接:=“TEXT;”&iFile,目标:=TempSheet2.Range($A$1”))
.Name=“02093861”
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xl
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=437
.TextFileStartRow=1
.TextFileParseType=xlDelimited
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=False
.TextFileSemicolonDelimiter=True
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(1)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
以
将qt设置为查询表
对于TempSheet2.QueryTables中的每个qt
qt.删除
下一个
端接头

当您定义变量PB\u UGE\u CSV\u FOLDER$时,它会认为您有一个输入错误。尝试不使用美元符号。不是:)运行宏时仍指向“If Dir(PB_UGE_CSV_文件夹,vbDirectory)=”Then“。使用“$”将路径声明为字符串。很好,我不知道您可以这样做。你知道哪个变量没有定义吗?另外,什么是
U1
?哪一行给出了错误?我注意到,
callimporttempsheet(PB\u UGE\u CSV\u文件夹和“\”&csvFile)
行中的子项不包括在代码示例中,但
PB\u UGE\u importtempsheet(iFile$)
是。这是故意的吗?哦,看到你上面的评论了。如果Dir(PB_UGE_CSV_FOLDER,vbDirectory)=“那么当我尝试您的代码时,
行编译没有问题,但是正如我所说,
importTempSheet
丢失(并且给出了一个错误)。