以下代码使用固定文件夹中的csv文件导入/更新工作簿中的summarysheet。我将代码划分为多个子代码:一个用于导入csv文件,一个用于删除summarysheet,再次导入时,一个用于mergin导入的csv文件。我已将文件夹路径声明为常量。为什么我会收到错误?
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