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
Bookmarks