Modiy VBA code to import

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
780
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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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