Results 1 to 4 of 4

VBA Improve Loop

Threaded View

lacrimosus1028 VBA Improve Loop 04-06-2021, 03:16 AM
AliGW Re: Help Please..VBA Improve... 04-06-2021, 03:21 AM
lacrimosus1028 Re: Help Please..VBA Improve... 04-06-2021, 03:36 AM
AliGW Re: Help Please..VBA Improve... 04-06-2021, 03:42 AM
  1. #1
    Registered User
    Join Date
    11-25-2019
    Location
    Korea
    MS-Off Ver
    2010
    Posts
    12

    VBA Improve Loop

    Hi,
    I'm new to VBA and currently working with the loop. However, my VBA code seems very slow...
    The purpose of my VBA is to loop the Source Workbook by merging all the outcomes.
    I would be appreciated with your help..!

    Here is my code,

    Sub Merge()
    
    Application.ScreenUpdating = False
    
    Dim lastRow As Integer
    Dim SourceWB As Workbook: Set SourceWB = Workbooks.Open("")
    Dim TargetWB As Workbook: Set TargetWB = Workbooks.Open("")
    Dim lRowCount As Integer, i As Integer
    Dim WS1 As Worksheet, WS2 As Worksheet, WS3 As Worksheet
        With ThisWorkbook
            Set WS1 = .Sheets("OUTPUT_1")
            Set WS2 = .Sheets("OUTPUT_2")
            Set WS3 = .Sheets("PROCESS")
        End With
    
    TargetWB.Sheets("PROCESS").Range("A4:Q100000").Clear
    SourceWB.Activate
    Application.ActiveSheet.UsedRange
    lRowCount = Worksheets("SOURCE").UsedRange.Rows.Count
    Application.Calculation = xlAutomatic
    TargetWB.Sheets("INFO").Activate
    
        For i = 1 To lRowCount
            Cells(2, 5) = i
                Worksheets("INFO").Range("C4").Copy
                Worksheets("PROCESS").Range("A" & i + 3).PasteSpecial Paste:=xlPasteValues
                
                Worksheets("INFO").Range("C8").Copy
                Worksheets("PROCESS").Range("B" & i + 3).PasteSpecial Paste:=xlPasteValues
                
                Worksheets("BETA").Range("AC1:AJ1").Copy
                Worksheets("PROCESS").Range("K" & i + 3).PasteSpecial Paste:=xlPasteValues
                
                Worksheets("BETA").Range("AQ1:AR1").Copy
                Worksheets("PROCESS").Range("S" & i + 3).PasteSpecial Paste:=xlPasteValues
                
                Worksheets("INFO").Range("C5").Copy
                Worksheets("PROCESS").Range("C" & i + 3).PasteSpecial Paste:=xlPasteValues
                
                Worksheets("INFO").Range("C6").Copy
                Worksheets("PROCESS").Range("E" & i + 3).PasteSpecial Paste:=xlPasteValues
                
                Worksheets("INFO").Range("C11").Copy
                Worksheets("PROCESS").Range("F" & i + 3).PasteSpecial Paste:=xlPasteValues
                
                Worksheets("BETA").Range("CC1").Copy
                Worksheets("PROCESS").Range("H" & i + 3).PasteSpecial Paste:=xlPasteValues
                
                Worksheets("INFO").Range("C17").Copy
                Worksheets("PROCESS").Range("I" & i + 3).PasteSpecial Paste:=xlPasteValues
        Next i
    
    With WS3
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    .Range("C4:C" & lastRow).Formula = _
    "=VLOOKUP($D4,Mapping!$A:$B,2,FALSE)"
    .Range("C4:C" & lastRow).Value = .Range("C4:C" & lastRow).Value
    .Range("I4:I" & lastRow).Formula = _
    "=CONCAT(B4," & Chr(34) & "|" & Chr(34) & ",C4," & Chr(34) & "|" & Chr(34) & ",E4," & Chr(34) & "|" & Chr(34) & ",F4," & Chr(34) & "|" & Chr(34) & ",G4)"
    .Range("I4:I" & lastRow).Value = .Range("I4:I" & lastRow).Value
    .Range("F4:F" & lastRow).Formula = _
    "=YEAR($E4)"
    .Range("F4:F" & lastRow).Value = .Range("F4:F" & lastRow).Value
    End With
    
    With WS3
    .AutoFilterMode = False
    LRow = .Range("A" & .Rows.Count).End(xlUp).Row
    LCol = .Range("A2").End(xlToRight).Column
    Set RngBeforeFilter = .Range(.Cells(2, 1), .Cells(LRow, LCol))
    RngBeforeFilter.Rows(1).AutoFilter Field:=9, Criteria1:="AAA"
    WS3.Range("J4:T" & LRow).Copy Destination:=WS2.Range("A4")
    End With
    
    With WS3
    .AutoFilterMode = False
    LRow = .Range("A" & .Rows.Count).End(xlUp).Row
    LCol = .Range("A2").End(xlToRight).Column
    Set RngBeforeFilter = .Range(.Cells(2, 1), .Cells(LRow, LCol))
    RngBeforeFilter.Rows(1).AutoFilter Field:=9, Criteria1:="<>AAA"
    WS3.Range("J4:T" & LRow).Copy Destination:=WS1.Range("A4")
    End With
    
    WS3.AutoFilterMode = False
    
    Application.ScreenUpdating = True
    Application.Calculation = xlManual
    
    SourceWB.Close
    
    End Sub
    Last edited by AliGW; 04-06-2021 at 03:42 AM. Reason: Irrelevant section of title removed - this is a help forum!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. improve loop
    By hgchas in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-17-2020, 08:21 PM
  2. Help me improve my formula
    By kakarot456 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-19-2020, 01:07 PM
  3. [SOLVED] Improve Speed: VBA Code very slow due to changing cell values in for loop
    By Nick_G in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-29-2020, 03:25 PM
  4. Improve code so it doesn't loop through worksheets seperately?
    By carissa7 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-18-2018, 04:52 AM
  5. [SOLVED] Improve my Macro!
    By Coeus in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-15-2015, 06:48 PM
  6. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  7. Need your help to improve
    By baba4005 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 04-19-2012, 03:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1