PDA

View Full Version : Import Second Excel file



zmagic
04-07-2021, 11:24 PM
Hi,

Open *.xls and move data to REPORT spread sheet, now I want to import second *.xls file, once the first import is finished. Only 2 xls to Import. Some one help to modify this.



Public Sub OpenFile()
Dim FileToOpen As String
Dim CurDir As String
FileToOpen = Application.GetOpenFilename
Workbooks.Open Filename:=FileToOpen


Call MoveData
End Sub


Public Sub MoveData()
Application.ScreenUpdating = False
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim LastRow As Long, NextRow As Long
Set wb1 = ThisWorkbook
Set wb2 = ActiveWorkbook
Set ws1 = wb1.Sheets("REPORT")
Set ws2 = wb2.Worksheets(1)
LastRow = ws2.Cells(Rows.Count, 1).End(xlUp).row
NextRow = ws1.Cells(Rows.Count, 1).End(xlUp).row + 1


For i = 3 To LastRow
ws1.Cells(NextRow, 1) = CDate(ws2.Cells(i, 5))
ws1.Cells(NextRow, 1).NumberFormat = "dd/mm/yyyy"
ws1.Cells(NextRow, 2) = ws2.Cells(i, 8)
ws1.Cells(NextRow, 3) = ws2.Cells(i, 17)
ws1.Cells(NextRow, 3).NumberFormat = "0.00"

NextRow = NextRow + 1
Next i
wb2.Close SaveChanges = False
ws1.Activate
Set wb1 = Nothing
Set wb2 = Nothing
Set ws1 = Nothing
Set ws2 = Nothing

End Sub

snb
04-08-2021, 02:48 AM
If you are able to write such extensive code for such a simple task, you should be able to adapt it.
You should reduce it to 3 code lines.