poopopoopo
New Member
- Joined
- Oct 26, 2017
- Messages
- 1
Hi,
I have copied the following code to import data from multiple workbooks. There is a text in every source workbooks sheet1 cell b6 which I want to get to SummarySheet's Column A instead of source workbooks filename. Code below:
Im beginner in vba and stuck in this. Please help.
I have copied the following code to import data from multiple workbooks. There is a text in every source workbooks sheet1 cell b6 which I want to get to SummarySheet's Column A instead of source workbooks filename. Code below:
Code:
Sub Hae()
Dim SummarySheet As Worksheet
Dim SelectedFiles() As Variant
Dim NRow As Long
Dim FileName As String
Dim NFile As Long
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim s As Worksheet, t As String
Dim i As Long, K As Long
Set SummarySheet = Worksheets("Data")
' Set the current directory to the the folder path.
ChDrive ThisWorkbook.Path
ChDir ThisWorkbook.Path
' Open the file dialog box and filter on Excel files, allowing multiple files
' to be selected.
SelectedFiles = Application.GetOpenFilename( _
filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
Application.ScreenUpdating = False
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1
' Loop through the list of returned file names
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
[B] ' Set FileName to be the current workbook file name to open.
FileName = SelectedFiles(NFile)
' Open the current workbook.
Set WorkBk = Workbooks.Open(FileName)
' Set the cell in column A to be the file name.
SummarySheet.Range("A" & NRow).Value = FileName[/B]
' Set the source range
Set SourceRange = WorkBk.Worksheets(3).Range("b1:am1464")
' Set the destination range to start at column B and be the same size as the source range.
Set DestRange = SummarySheet.Range("B" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
' Increase NRow so that we know where to copy data next.
NRow = NRow + DestRange.Rows.Count
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
Next NFile
' Call AutoFit on the destination sheet so that all data is readable.
SummarySheet.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
Im beginner in vba and stuck in this. Please help.