
Sub importafolder()
Dim DestinationSheet As Worksheet
Dim CSVfolder As String
Dim CSVfile As String
Dim stRw As Long
Dim wbNew As Workbook
stRw = 1
CSVfolder = "c:historicaldata"
Set DestinationSheet = Worksheets(1)
Application.ScreenUpdating = False
If Right(CSVfolder, 1) <> "" Then CSVfolder = CSVfolder & ""
CSVfile = Dir(CSVfolder & "*.csv")
While CSVfile <> ""
Set wbNew = Workbooks.Open(Filename:=CSVfolder & CSVfile)
If Not wbNew Is Nothing Then
With DestinationSheet
.Range("A" & stRw).Value = CSVfile 'CSVfolder & CSVfile
Range("A1").CurrentRegion.Offset(1, 0).Copy .Range("A" & stRw + 1)
stRw = .Cells(Rows.Count, "A").End(xlUp).Row + 2
End With
wbNew.Close False
End If
CSVfile = Dir()
Wend
Set wbNew = Nothing
Set DestinationSheet = Nothing
Application.ScreenUpdating = True
End Sub |
Sub importafolder2()
Dim DestinationSheet As Worksheet
Dim CSVfolder As String
Dim CSVfile As String
CSVfolder = "c:historicaldata"
Set DestinationSheet = Worksheets(1)
' Application.ScreenUpdating = False
If Right(CSVfolder, 1) <> "" Then CSVfolder = CSVfolder & ""
CSVfile = Dir(CSVfolder & "*.csv")
linenumber = 0
While CSVfile <> ""
elementnumber = 0
Open CSVfolder & CSVfile For Input As #1 ' Open file for input
Do While Not EOF(1) ' Loop until end of file
linenumber = linenumber + 1
Line Input #1, line
arrayOfElements = Split(line, ",")
elementnumber = 0
For Each element In arrayOfElements
elementnumber = elementnumber + 1
DestinationSheet.Cells(linenumber, elementnumber).Value = element
Next
Loop
Close #1 ' Close file.
Stop
CSVfile = Dir()
Wend
Set DestinationSheet = Nothing
Application.ScreenUpdating = True
End Sub
|
