Jeg forsøger at lave et script som kan importere en række csv-filer ind i eksisterende faner i et excelark og jeg har praktisk talt bare kopieret en youtube video, men kan ikke få den til at virke.
Den har virket før, men efter at jeg har arbejdet lidt videre med den kan jeg ikke få det til at fungere længere. Jeg har efterhånden fået nogle forskellige fejlmeddelelser, men lige pt er det:
Koden skal kort fortalt hente 8 forskellige csv-filer ind i 8 forskellige faner.
Filnavne på csv-filer samt årstal er defineret i arket "forudsætninger", sammen med navne på destinations-faner.
Koden er bygget op med to loops da csv-filerne er delt op i fire med samme navn + årstal og fire med et andet navn + årstal, og jeg ikke har kunnet finde ud af at lave en elegant måde at udskifte filnavnet
Sub Dataimport()
Application.ScreenUpdating = False
PathName = Application.ActiveWorkbook.Path & "\"
Filename =
Sheets("Forudsætninger").Range("G11").Value
Start_year =
Sheets("Forudsætninger").Range("G9").Value
End_year = Sheets("Forudsætninger").Range("G10").Value
Sheetname =
Sheets("Forudsætninger").Range("G13").Value
Dim i As Integer
For i = Start_year To End_year Step 5
Worksheets(Sheetname & "20" & i).Activate
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" &
PathName & Filename & i & ".csv",
Destination:=Range("$A$1"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh =
False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType =
xlDelimited
.TextFileTextQualifier =
xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter
= True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter =
True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter =
False
.TextFileColumnDataTypes =
Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers
= True
.Refresh
BackgroundQuery:=False
End With
Dim wb_connection As WorkbookConnection
For Each wb_connection In ActiveWorkbook.Connections
If InStr(Filename,
wb_connection.Name) > 0 Then
wb_connection.Delete
End If
Next wb_connection
Next i
Filename =
Sheets("Forudsætninger").Range("G12").Value
Sheetname =
Sheets("Forudsætninger").Range("G14").Value
Dim j As Integer
For j = Start_year To End_year Step 5
Worksheets(Sheetname & "20" & j).Activate
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" &
PathName & Filename & j & ".csv",
Destination:=Range("$A$1"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle =
xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh =
False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType =
xlDelimited
.TextFileTextQualifier =
xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter
= True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter =
False
.TextFileSpaceDelimiter =
False
.TextFileColumnDataTypes =
Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers
= True
.Refresh
BackgroundQuery:=False
End With
For Each wb_connection In ActiveWorkbook.Connections
If InStr(Filename,
wb_connection.Name) > 0 Then
wb_connection.Delete
End If
Next wb_connection
Next j
MsgBox ("Done")
End Sub