Import Data from CSV file to Existing Worksheet

Stvy

New Member
Joined
Mar 1, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello everyone.

I am trying to create a macro that would import data from a CSV file to a specific worksheet at a specific cell. I found a previous post with a code that successfully imports my CSV file; however, it defaults to importing the data to the first worksheet within the workbook.

For my specific workbook, I have a worksheet named "Market Trends" and would like the data to insert into cell Y15 in the "Market Trends" worksheet. Below is the code I found. I'm hoping someone can help me tweak the code specific to what I'm looking for.

VBA Code:
Sub CSV_Import()
   Dim dateien As Variant
   Dim i As Long
   
   dateien = Application.GetOpenFilename _
      ("csv-Dateien (*.csv), *.csv", MultiSelect:=True)
   If IsArray(dateien) Then
      For i = 1 To UBound(dateien)
         Workbooks.Open dateien(i), local:=True
         With ThisWorkbook.Sheets(1)
            ActiveSheet.UsedRange.Copy Destination:=.Cells(1, .Columns.Count).End(xlToLeft).Offset(, 1)
         End With
         ActiveWorkbook.Close False
      Next i
   End If
End Sub

Thank you in advance.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

GlennJ

New Member
Joined
Mar 26, 2018
Messages
42
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
Sub CSV_Import()
   Dim dateien As Variant
   Dim i As Long
  
   dateien = Application.GetOpenFilename _
      ("csv-Dateien (*.csv), *.csv", MultiSelect:=True)
   If IsArray(dateien) Then
      For i = 1 To UBound(dateien)
         Workbooks.Open dateien(i), local:=True
        
        
        
         ' Change the (3)below to how many sheets to right you need to go to get to your "Market Trends" sheet
         With ThisWorkbook.Sheets(3)
        
        
        
         ' the 15 below changes how many rows down you want to go and the 24 is how many columns to the right you want to go, to get
         ' to the the cell you want your data to start in
            ActiveSheet.UsedRange.Copy Destination:=.Cells(15, .Columns.Count).End(xlToLeft).Offset(, 24)
         End With
        
        
        
        
        
        
         ActiveWorkbook.Close False
      Next i
   End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,129,361
Messages
5,635,808
Members
416,884
Latest member
leeshjay

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
Top