Hi,
I have a VBA code that imports values from other .csv files. I make use of "Offset" to skip the first row in each target file (column headers) and have the titles hard coded in my destination sheet. Problem is: when the entire code runs it automatially deletes all column headers. I narrowed it down to the delimit values piece but have no idea how to define my ranges properly and this is really bugging me.
What I want is for excel to import .csv values from other sheets, skip their first rows but leave my row1 in the destination file untouched and delimit values.
Here is the code I use
I have a VBA code that imports values from other .csv files. I make use of "Offset" to skip the first row in each target file (column headers) and have the titles hard coded in my destination sheet. Problem is: when the entire code runs it automatially deletes all column headers. I narrowed it down to the delimit values piece but have no idea how to define my ranges properly and this is really bugging me.
What I want is for excel to import .csv values from other sheets, skip their first rows but leave my row1 in the destination file untouched and delimit values.
Here is the code I use
Code:
Private Sub CommandButton2_Click()
'Import Position Data and Refresh Pivot Tables
Dim pos As Worksheet
Dim import1 As Variant ' import pop-up window
Dim import2 As Variant ' loop through each file to be imported
Dim pt As PivotTable ' each pivottable
Dim ws As Worksheet ' each worksheet in workbook
Dim y As Integer ' delete additional headers range index
Set pos = ActiveWorkbook.Worksheets("Position Data")
'Disable Excel Messages & Automated Calculations:
Application.DisplayAlerts = False
Application.CutCopyMode = False
Application.Calculation = xlCalculationManual
'remove filters if any
On Error Resume Next
If pos.AutoFilterMode Then pos.ShowAllData
Resume
'Delete old data in range A to CL:
With pos
.Range("A2:CL" & Rows.Count).Clear
End With
'Import Position files (.ok extensions - change to .xls or other if needed):
import1 = Application.GetOpenFilename(Filefilter:="OK Files (*.ok*),*.ok*", _
Title:="Select .OK files List to Import", MultiSelect:=True)
If VarType(import1) = vbBoolean Then Exit Sub
For Each import2 In import1
With Workbooks.Open(import2)
.Worksheets(1).UsedRange.Offset(1).Copy
pos.Cells(pos.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValues, operation:=xlPasteSpecialOperationNone
.Close savechanges:=False
End With
Next import2
'Delimit imported data
With pos
.Columns("A").TextToColumns Destination:=.Range("A1"), _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, _
TrailingMinusNumbers:=True
End With
'Turn alerts & automated calculations back on
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
pos.Range("A:CZ").HorizontalAlignment = xlcentrer
'Refresh PivotTables in entire Workbook
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
Next ws
End Sub
Last edited: