Hello,
The code below is executed from a workbook (WB1), the user selects a workbook (WB2), it loops through all sheets and copies values from specific cells in WB2 to WB1.
The values from each sheet of WB2 will be entered in a new row on WB1, starting on row 3.
I would like to alter the code to start on the first blank row, so existing data it is not overwritten.
I have attempted unsuccessful to use the statement, LastRow = ActiveSheet.UsedRange.Rows.Count. I don’t know if the problem is my code or my placement of it within the procedure.
Note: WB1 and WB2 are only used in this description to make it easier to understand, I hope.
Thanks for the assistance.
Ron
The code below is executed from a workbook (WB1), the user selects a workbook (WB2), it loops through all sheets and copies values from specific cells in WB2 to WB1.
The values from each sheet of WB2 will be entered in a new row on WB1, starting on row 3.
I would like to alter the code to start on the first blank row, so existing data it is not overwritten.
I have attempted unsuccessful to use the statement, LastRow = ActiveSheet.UsedRange.Rows.Count. I don’t know if the problem is my code or my placement of it within the procedure.
Note: WB1 and WB2 are only used in this description to make it easier to understand, I hope.
Thanks for the assistance.
Ron
Code:
Sub ImportBOE()
Dim wb As Workbook, fullName As String, fileName As String
Dim i As Long, fileToOpen As Variant
Dim rCount As Integer
On Error Resume Next
fileToOpen = Application.GetOpenFilename("Excel files (*.xls), *.xls")
fullName = fileToOpen
fileName = Dir(fileToOpen)
If IsWbOpen(fileName) Then
Set wb = Workbooks(fileName)
Else
Set wb = Workbooks.Open(fullName)
End If
rCount = 3 'starting row
For i = 1 To wb.Worksheets.Count
With ThisWorkbook.Sheets("Resources (Spread or Load)")
.Cells(rCount, 1).Value = wb.Sheets(i).Cells(2, 4).Value 'Task ID
.Cells(rCount, 2).Value = wb.Sheets(i).Cells(5, 2).Value 'Resource ID
.Cells(rCount, 12).Value = wb.Sheets(i).Cells(7, 6).Value 'Spread
.Cells(rCount, 5).Value = wb.Sheets(i).Cells(3, 2).Value 'WBS
.Cells(rCount, 13).Value = wb.Sheets(i).Cells(6, 8).Value 'Hours
.Cells(rCount, 14).Value = wb.Sheets(i).Cells(7, 2).Value 'Start date
.Cells(rCount, 15).Value = wb.Sheets(i).Cells(7, 4).Value 'End date
End With
rCount = rCount + 1
Next i
Workbooks(fileName).Close
End Sub
Public Function IsWbOpen(wbName As String) As Boolean
On Error Resume Next
IsWbOpen = Len(Workbooks(wbName).Name)
End Function