Modiy VBA code to import

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
786
Office Version
  1. 365
Hi,

found the this code:

How to modify the vba code below to import the data into an existing spreadsheet to specific columns in that spreadsheet instead creating a new file:


VBA Code:
Sub CSV_to_XLSX_Add_Columns()

' ### 23 Apr. 2019 oAnast ###

Dim mywb As Workbook, wb As Workbook

Dim sh As Worksheet

Set mywb = ThisWorkbook

Dim vFile

Dim fn

Dim x As Integer, t As Integer

Dim v As Variant, vName As Variant

v = Array(117, 113, 121) ' ## EXPORT COLUMNS ##

vFile = Application.GetOpenFilename("CSV Files(*.csv),*.csv", , "please select a file", MultiSelect:=False)

If vFile = False Then Exit Sub

vName = Split(vFile, "\")

vName = Replace(vName(UBound(v)), ".csv", "")

Application.ScreenUpdating = False

Set sh = mywb.Sheets.Add(before:=mywb.Sheets(1))

sh.Name = vName

Workbooks.OpenText Filename:=vFile, Local:=True

Set wb = ActiveWorkbook

t = 1

For x = 0 To UBound(v)

wb.Sheets(1).Columns(v(x)).Copy sh.Cells(1, t)

t = t + 1

Next

sh.UsedRange.EntireColumn.AutoFit

wb.Close False

fn = Environ("USERPROFILE") & "\Desktop\" & vName & ".xlsx"

sh.Copy

Application.DisplayAlerts = False

ActiveWorkbook.SaveAs fn

ActiveWorkbook.Close False

sh.Delete

Application.DisplayAlerts = True

Application.ScreenUpdating = True

End Sub

thank you
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,224,602
Messages
6,179,847
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top