SaurabhPandit
New Member
- Joined
- Dec 20, 2021
- Messages
- 6
- Office Version
- 365
- Platform
- 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
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