Need help with VBA Loop

sparkytech

Board Regular
Joined
Mar 6, 2018
Messages
96
Office Version
  1. 365
  2. 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!

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
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Why do you have separate loops like this?
VBA Code:
    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

Since you are looping through the exact same rows every time, I believe you should be able to combine them into one loop, which should speed up your code a bit (as loops are notoriously slow):
VBA Code:
    For iCnt = 1 To iTotalRows
        Worksheets("Sheet1").Range("A" & iCnt).Formula = src.Worksheets("Sheet B").Range("B" & iCnt).Formula
        Worksheets("Sheet1").Range("B" & iCnt).Formula = src.Worksheets("Sheet B").Range("C" & iCnt).Formula
        Worksheets("Sheet1").Range("C" & iCnt).Formula = src.Worksheets("Sheet B").Range("I" & iCnt).Formula
        Worksheets("Sheet1").Range("D" & iCnt).Formula = src.Worksheets("Sheet B").Range("J" & iCnt).Formula
        Worksheets("Sheet1").Range("E" & iCnt).Formula = src.Worksheets("Sheet B").Range("K" & iCnt).Formula
        Worksheets("Sheet1").Range("F" & iCnt).Formula = src.Worksheets("Sheet B").Range("L" & iCnt).Formula
    Next iCnt

Regarding your question, what exactly is the formula in column L on your source sheet?
What does it show in column F on your destination sheet (if you go to one of the cells and look in the formula editor)?
I just want to make sure that it actually isn't a situation in which that there is a formula in that cell that is just return a blank.
 
Upvote 0
I suspect you are getting a runtime error. Modify your Error handler code as per below so you can see any error message

VBA Code:
ErrHandler:
    Dim Msg As String
   
    If Err.Number <> 0 Then
        Msg = "An error has occurred:" & vbCr & vbCr _
        & "     Runtime Error " & Err.Number & vbCr _
        & "     Desc: " & UCase(Err.Description) & vbCr & vbCr
        MsgBox Msg, vbCritical, "Error Report"
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top