sparkytech
Board Regular
- Joined
- Mar 6, 2018
- Messages
- 96
- Office Version
- 365
- 2019
I have the code below which opens Workbook 1 and copies columns into Workbook 2. The function is this: open Workbook 1, copy columns B to A, C to B, I to C, J to D, K to E, and L to F to Workbook 2. Everything sort of works, but the odd thing is it doesn't copy/paste the last one in the code (L to F). I've tried everything I can think of but can't get it to function correctly. What am I missing here, and is there a better way to do this?
Thanks!
Thanks!
VBA Code:
Option Explicit
Private Sub Workbook_Open()
Call ReadDataFromCloseFile
End Sub
Sub ReadDataFromCloseFile()
On Error GoTo ErrHandler
Application.ScreenUpdating = False
Dim src As Workbook
Dim iCnt As Integer ' COUNTER
' OPEN THE SOURCE EXCEL WORKBOOK IN "READ ONLY MODE".
Set src = Workbooks.Open("<redacted>.xlsx")
' GET THE TOTAL ROWS FROM THE SOURCE WORKBOOK.
Dim iTotalRows As Integer
iTotalRows = src.Worksheets("Sheet B").Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row).Rows.Count
' COPY DATA FROM SOURCE (CLOSE WORKGROUP) TO THE DESTINATION WORKBOOK
'Dim iCnt As Integer ' COUNTER.
For iCnt = 1 To iTotalRows
Worksheets("Sheet1").Range("A" & iCnt).Formula = src.Worksheets("Sheet B").Range("B" & iCnt).Formula
Next iCnt
'Dim iCnt As Integer ' COUNTER.
For iCnt = 1 To iTotalRows
Worksheets("Sheet1").Range("B" & iCnt).Formula = src.Worksheets("Sheet B").Range("C" & iCnt).Formula
Next iCnt
'Dim iCnt As Integer ' COUNTER.
For iCnt = 1 To iTotalRows
Worksheets("Sheet1").Range("C" & iCnt).Formula = src.Worksheets("Sheet B").Range("I" & iCnt).Formula
Next iCnt
'Dim iCnt As Integer ' COUNTER.
For iCnt = 1 To iTotalRows
Worksheets("Sheet1").Range("D" & iCnt).Formula = src.Worksheets("Sheet B").Range("J" & iCnt).Formula
Next iCnt
'Dim iCnt As Integer ' COUNTER.
For iCnt = 1 To iTotalRows
Worksheets("Sheet1").Range("E" & iCnt).Formula = src.Worksheets("Sheet B").Range("K" & iCnt).Formula
Next iCnt
'Dim iCnt As Integer ' COUNTER.
For iCnt = 1 To iTotalRows
Worksheets("Sheet1").Range("F" & iCnt).Formula = src.Worksheets("Sheet B").Range("L" & iCnt).Formula
Next iCnt
' CLOSE THE SOURCE FILE.
'src.Close savechanges:=False
src.Close False ' FALSE - DON'T SAVE THE SOURCE FILE.
Set src = Nothing
ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub