Select Multiple VBA files and set individual range for each file.

SaurabhPandit

New Member
Joined
Dec 20, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,
I am trying to build a code in VBA for my project. I want to select five different CSV files and want to set the range in five different sheets. Can anyone help with this? I created code as below for just one CSV file. But I need multiple selections and ranges. In the attached images screenshot 2 are source CSV files and in screenshot 1 are five different sheets. Can anyone help with this?
Thank You.
Saurabh

Sub GetData()

Application.ScreenUpdating = False
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Dim rngSourceRange As Range
Dim rngDestination As Range
Set wkbCrntWorkBook = ActiveWorkbook

With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Lithology File"
.Filters.Add "CSV", "*.csv"
.FilterIndex = 1
.AllowMultiSelect = True
.InitialFileName = "*Lithology*.*"

result = .Show

If .SelectedItems.Count > 0 Then
Workbooks.Open .SelectedItems(1)
Set wkbSourceBook = ActiveWorkbook
Set rngSourceRange = Range("A1:BZ50000")
wkbCrntWorkBook.Activate
Set rngDestination = Sheets("Lithology").Range("A1")
rngSourceRange.Copy rngDestination
rngDestination.CurrentRegion.EntireColumn.AutoFit
wkbSourceBook.Close False
End If
End With
Application.ScreenUpdating = True
End Sub
 

Attachments

  • Screenshot 2.jpg
    Screenshot 2.jpg
    49.4 KB · Views: 9

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,223,937
Messages
6,175,513
Members
452,650
Latest member
Tinfish

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