Reduce the time of running the macro

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello Experts

I have this file which runs through a long process to get the final result. I have recorded several macros along with the help of macros I got through this board. The problem is it takes at least 2 minutes to get the result. I am not able to figure out why the macro is running so slow. With your expert advice, I am sure I will be able to edit and run the macro in just a few seconds. I get an error when I try to edit the multiple select options in the code

The sheet “Original” is the raw data pasted. “SheetF” is the final result of the data. To get the result the macro runs through different sheets to get the expected result. “GetData” is the macro to get the result and “ClearData” is to clear the old data so that a new data can be pasted. “ClearData” also inserts once again, different formulas in the “SheetB” which were deleted while getting the data. In short, I am arranging the data of Original sheet in the format as shown in SheetF with this code.
I am sharing the link of a sample file.
 
In thaqt case I would recommend:
1. Putting thi line at the end of the GetData code rather than in the ClearData code
VBA Code:
Sheets("Formulas").Range("B2:AU2").Copy Sheets("B").Range("K3:K" & Cells(Rows.Count, "A").End(xlUp).Row)
2. adding extra lines to turn of calculations until all data was imported
VBA Code:
With Application
.screenupating = false
.Calculation = xlCalculationManual
End With
then at the end of the code
VBA Code:
With Application
.screenupating = true
.Calculation = xlCalculationautomatic
End With
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Further, if you use this code as your clear data macro it reduces the run time by over 60%
VBA Code:
Sub ClearData2()
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
Sheets("Bank").UsedRange.Clear
Sheets("A").UsedRange.Clear
Sheets("B").Range("A3:BD" & Cells(Rows.Count, "A").End(xlUp).Row).ClearContents
Sheets("E").UsedRange.Clear
Sheets("Z").UsedRange.Clear
Sheets("F").Range("B2:BE" & Cells(Rows.Count, "A").End(xlUp).Row).ClearContents
Sheets("Formulas").Range("I3").Copy Sheets("B").Range("I3:I" & Cells(Rows.Count, "A").End(xlUp).Row)
Sheets("Formulas").Range("B2:AU2").Copy Sheets("B").Range("K3:K5000")
Sheets("B").Range("K3:K" & Cells(Rows.Count, "A").End(xlUp).Row).Borders.LineStyle = xlLineStyleNone
Sheets("B").Range("I3:I" & Cells(Rows.Count, "A").End(xlUp).Row).Borders.LineStyle = xlLineStyleNone
Sheets("Original").Activate
Range("A1").Select
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
End Sub
 
Upvote 0
Guys, It took 6 months to reach here step by step to reach here. I am getting a feeling that I will not be able to increase the speed of the macro with this code. I better be satisfied with the slow macro for the time being till somebody comes out with a solution. As for me I have reached a dead end. ETC..(end of thinking capacity).

Fear not @RAJESH1960, we will try to help you.

How about the following for starters:

VBA Code:
Sub ClearDataV3()
'
    Application.ScreenUpdating = False                                                                      ' Turn ScreenUpdating off
    Application.Calculation = xlManual                                                                      ' Turn Calculation's off
'
    Dim ColumnNumber            As Long
    Dim LastColumnNumberSheetB  As Long
    Dim LastRowInSheetB         As Long
    Dim LastRowInSheetF         As Long
    Dim LastRowOriginalColumnA  As Long
    Dim LastColumnLetterSheetB  As String
    Dim LastColumnLetterSheetF  As String
    Dim WS                      As Worksheet
'
    LastRowOriginalColumnA = Sheets("Original").Range("A" & Rows.Count).End(xlUp).Row               ' Get last column row # used in Sheets("Original") column A
    LastColumnLetterSheetB = Split(Sheets("B").Range("K2").End(xlToRight).Address, "$")(1)                  ' Get last column letter used in row 2 of Sheets("B")
    LastRowInSheetB = Sheets("B").Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row                 ' Find last row # used in Sheets("B")
    LastRowInSheetF = Sheets("F").Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row                 ' Find last row # used in Sheets("F")
    LastColumnLetterSheetF = Split(Cells(1, (Sheets("F").Cells.Find("*", , xlFormulas, , xlByColumns, _
            xlPrevious).Column)).Address, "$")(1)                                                           ' Get last column letter used in Sheets("F")
    LastColumnNumberSheetB = Sheets("B").Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column    ' Find last column # used in Sheets("B")
'
    For Each WS In Sheets(Array("Bank", "A", "E", "Z"))                                                     ' Prep sheets to clear ranges
        With WS                                                                                             '   With each worksheet to be cleared ...
            .UsedRange.Clear                                                                                '       Clear the data from all used cells
        End With
    Next                                                                                                    ' Loop back for next sheet to be cleared
'
    Sheets("B").Range("A3:" & LastColumnLetterSheetB & LastRowInSheetB).ClearContents                       ' Clear contents of cells in Sheets("B")
    Sheets("F").Range("B2:" & LastColumnLetterSheetF & LastRowInSheetF).ClearContents                       ' Clear contents of cells in Sheets("F")
'
'----
'
    With Sheets("Formulas")
        .Range("B2", .Range("B2").End(xlToRight)).Copy Sheets("B").Range("K3")          '   Copy Formula range from Sheets("Formulas") to Sheets("B") range
        .Range("I3").Copy Sheets("B").Range("I3")                                       '   Copy Formula from Sheets("Formulas") I3 to Sheets("B") I3
    End With
'
    With Sheets("B")
        .Range("I3:I" & LastRowOriginalColumnA).FillDown                                '   Copy formula in Sheets("B") I3 down the column
'
        For ColumnNumber = 11 To LastColumnNumberSheetB                                 '   Loop from column 11(K) of Sheets("B") to LastColumnNumberSheetB
            .Cells(3, ColumnNumber).AutoFill .Range(.Cells(3, ColumnNumber), .Cells(LastRowOriginalColumnA, ColumnNumber))  '       Fill Range with formulas
        Next                                                                            ' Loop back
'
        With .Range("I3:I" & LastRowOriginalColumnA)                                    '   Remove borders from Sheets("B") column I cells
            .Borders(xlEdgeLeft).LineStyle = xlNone                                     '
            .Borders(xlEdgeTop).LineStyle = xlNone                                      '
            .Borders(xlEdgeRight).LineStyle = xlNone                                    '
            .Borders(xlInsideHorizontal).LineStyle = xlNone                             '
        End With
    End With
'
    Sheets("Original").Range("A1:C1").Select                                            ' Select Sheets("Original").Range("A1:C1")
'
    Application.Calculation = xlAutomatic                                               ' Turn Calculations back on
    Application.ScreenUpdating = True                                                   ' Turn ScreenUpdating back on
End Sub

And:

VBA Code:
Sub GetDataV3()
'
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
'
    Dim i                   As Long, ini        As Long, j      As Long, k  As Long
    Dim VchNo               As Long
    Dim cell                As Range
    Dim Fnd                 As Range
    Dim rngReferenceRange   As Range, rngToCopy As Range
    Dim a                   As Variant, b       As Variant, c   As Variant
    Dim NewName             As String
'
'
    With Sheets("Bank")
        Sheets("Original").Columns("A:I").Copy .Range("A1")
        .UsedRange.UnMerge
        Set Fnd = .Range("A:A").Find("Date", , , xlPart, xlByRows, xlNext, False, , False)
        If Not Fnd Is Nothing And Fnd.Row > 1 Then ini = Fnd.Row + 2 Else ini = 1
        a = .Range("A" & ini, .Range("I" & .Rows.Count).End(3)).Value
    End With
'
    ReDim b(1 To UBound(a), 1 To 7)
    ReDim c(1 To UBound(a), 1 To 7)
'
    For i = 1 To UBound(a) - 3
        If LCase(a(i, 3)) <> LCase("(as per details)") And a(i, 6) <> "" Then
            j = j + 1
            b(j, 1) = i 'Line
            b(j, 2) = a(i, 1) 'Date
            b(j, 3) = a(i, 6) 'Vch Type
            b(j, 4) = a(i, 7) 'Vch No.
            b(j, 5) = a(i, 3) 'Particulars
            b(j, 6) = a(i, 8) 'Debit
            b(j, 7) = a(i, 9) 'Credit
        Else
            k = k + 1
            c(k, 1) = i 'Line
            c(k, 2) = a(i, 1) 'Date
            c(k, 3) = a(i, 6) 'Vch Type
            c(k, 4) = a(i, 7) 'Vch No.
            c(k, 5) = a(i, 3) 'Particulars
            c(k, 6) = a(i, 8) 'Debit
            c(k, 7) = a(i, 9) 'Credit
        End If
    Next
'
    With Sheets("Bank")
        .UsedRange.Clear
        .Range("A1:G1").Value = Array("Line", "Date", "Vch Type", "Vch No.", "Particulars", "Debit", "Credit")
        .Range("A2").Resize(j, 7).Value = b
        .Range("A" & j + 3).Resize(k, 7).Value = c
        .Columns("F:G").NumberFormat = "0.00"
'
        .Range("A" & j + 3).Resize(k, 7).Interior.Pattern = xlSolid
'
        .UsedRange.EntireColumn.AutoFit
        .UsedRange.HorizontalAlignment = xlLeft
'
        .Range("B:B").NumberFormat = "dd-mm-yyyy"
'
        .UsedRange.Interior.Pattern = xlNone
    End With
'
    NewName = Sheets("Original").Range("K1")
    VchNo = 1000
'
    For Each cell In Sheets("Bank").Range("D2:D" & Sheets("Bank").Range("E" & Sheets("Bank").Rows.Count).End(xlUp).Row)
        If Not cell.Offset(, -1) = vbNullString Then
            VchNo = VchNo + 1
            cell.Value = VchNo
        End If
'
        If cell.Offset(0, 1) = "(as per details)" Then cell.Offset(0, 1).Value = NewName
    Next
'
    Set rngReferenceRange = Sheets("Bank").Range("A1").CurrentRegion
    Set rngToCopy = Sheets("Bank").Cells(rngReferenceRange.Rows.Count + 2, 1).CurrentRegion
'
    rngToCopy.Copy
'
'---------------------------------------------------------------------------------------------------------------------
'
    With Sheets("A")
        .Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
'
        .Columns("B:B").NumberFormat = "dd-mm-yyyy"
        .Columns("E:E").Insert Shift:=xlToRight
        .Columns("G:H").Insert Shift:=xlToRight
        .Range("G1").FormulaR1C1 = "=IF(RC[2]="""","""",-RC[2])"
        .Range("H1").FormulaR1C1 = "=IF(RC[2]="""","""",RC[2])"
        .Range("G1:H1").AutoFill Destination:=.Range("G1:H" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
'
    With Sheets("A").Range("B2:D" & Sheets("A").Range("A" & Rows.Count).End(xlUp).Row)
        .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
'
    With Sheets("A")
        .Columns("D").NumberFormat = "0"
        .Columns("G:H").NumberFormat = "0.00"
'
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).Copy
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).Copy Sheets("B").Range("A3")
    End With
'
    Dim Mx As Long
'
'   this range needs to be changed
    With Sheets("B")
        Mx = Application.Max(.Range("K3:K" & .Range("K" & .Rows.Count).End(xlUp).Row))
'
        Sheets("E").Range("A3:AT3").Resize(Mx).Value = .Range("K3:BD3").Resize(Mx).Value            ' Probably should make variable for 'BD' ;)
'
        .UsedRange.Value = .UsedRange.Value
    End With
'
    Sheets("F").Range("B2:AT2").Resize(Mx).Value = Sheets("E").Range("A3:AT3").Resize(Mx).Value
'
'
    Sheets("Bank").Select
    Range("A1").Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
'
    Sheets("Z").Select
    Range("A2").Select
    ActiveSheet.Paste
'
'
    With Sheets("Z")
        .UsedRange.EntireColumn.AutoFit
        .Columns("F:G").Insert Shift:=xlToRight
        .Range("F3").FormulaR1C1 = "=IF(RC[2]="""",RC[3],-RC[2])"
        .Range("G3").FormulaR1C1 = "=-RC[-1]"
'
'       this range needs to be changed
        .Range("F3:G3").AutoFill Destination:=.Range("F3:G2000")
'''        .Range("F2:G2").AutoFill Destination:=.Range("F2:G" & .Range("G" & .Rows.Count)).End(xlUp).Row
'
'       this range needs to be changed
''        .Range("F2:G2000").Select
        .Range("F3:G3", .Range("F3:G3").End(xlDown)).Copy
        .Range("F3:G3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End With
'
    Application.CutCopyMode = False
'
    Dim rr      As Long
    Dim p       As Variant
    Dim pp      As Variant
    Dim s()     As Variant
    Dim ss()    As Variant
'
    s = Array(2, 3, 4, 5, 6, 7)
    ss = Array(2, 3, 4, 8, 7, 9)
    p = Sheets("Z").Cells(Sheets("Z").Rows.Count, 1).End(3).Row
    pp = Sheets("F").Cells(Sheets("F").Rows.Count, 3).End(3).Row + 1
'
    For rr = 0 To UBound(s)
        Sheets("Z").Range(Sheets("Z").Cells(3, s(rr)), Sheets("Z").Cells(p, s(rr))).Copy Sheets("F").Cells(pp, ss(rr))
    Next
'
    Sheets("F").Cells(pp, "f").Resize(Sheets("F").Range("b" & Sheets("Z").Rows.Count).End(3).Row - pp + 1) = NewName
'
    Application.CutCopyMode = True
'
    With Sheets("Z").UsedRange
        .Value = .Value
    End With
'
    Dim da As Long
    Dim ku As Long
'
    With Sheets("F").Range("A1").CurrentRegion
        For da = 2 To .Rows.Count
            If .Cells(da, 7) < 0 Then
                ku = .Cells(da, .Columns.Count).End(xlToLeft).Column
                .Cells(da, ku + 1).Resize(, 2).Value = .Cells(da, 6).Resize(, 2).Value
'
                With .Cells(da, 6).Resize(, .Columns.Count)
                    .Value = .Offset(, 2).Value
                End With
            End If
        Next
    End With
'
    With Sheets("F")
        .UsedRange.HorizontalAlignment = xlGeneral
        .Range("D2", .Range("D2").End(xlDown)).ClearContents
    End With
'
    Sheets("Original").Activate
    Range("A2").Select
'
    Application.Calculation = xlAutomatic
    Application.ScreenUpdating = True
'
    MsgBox ("File sorted successfully.")
End Sub

Test those. They should be way faster than the original versions
 
Upvote 0
Fear not @RAJESH1960, we will try to help you.
That is very kind of you JohnnyL. I am so sorry I am not able to make you understand the code. The formulas in sheet B are not working. It has to display when there is data in columns A:J, the columns K3:BD3 are auto calculated. If required you can convert the formulas into a code. I am getting a run time error 1004 in the last code shared by you in #43.

Rich (BB code):
Sheets("E").Range("A3:AT3").Resize(Mx).Value = .Range("K3:BD3").Resize(Mx).Value            ' Probably should make variable for 'BD' ;)
Only the sheets Bank is right, sheet A column G the amounts are wrong . The rest of the sheets E,Z and F are left blank.
 
Last edited:
Upvote 0
& Cells(Rows.Count, "A").End(xlUp).Row)
Due to this line the code is selecting a limited number of cells to copy the formula whereas it has to copy the formula till the end.
 
Upvote 0
Dang it. That was my mistake. Calculation mode should not have been added at the end of my trial.

Edited version of GetData:

VBA Code:
Sub GetDataV3_2()
'
    Application.ScreenUpdating = False
'
    Dim i                   As Long, ini        As Long, j      As Long, k  As Long
    Dim VchNo               As Long
    Dim cell                As Range
    Dim Fnd                 As Range
    Dim rngReferenceRange   As Range, rngToCopy As Range
    Dim a                   As Variant, b       As Variant, c   As Variant
    Dim NewName             As String
'
'
    With Sheets("Bank")
        Sheets("Original").Columns("A:I").Copy .Range("A1")
        .UsedRange.UnMerge
        Set Fnd = .Range("A:A").Find("Date", , , xlPart, xlByRows, xlNext, False, , False)
        If Not Fnd Is Nothing And Fnd.Row > 1 Then ini = Fnd.Row + 2 Else ini = 1
        a = .Range("A" & ini, .Range("I" & .Rows.Count).End(3)).Value
    End With
'
    ReDim b(1 To UBound(a), 1 To 7)
    ReDim c(1 To UBound(a), 1 To 7)
'
    For i = 1 To UBound(a) - 3
        If LCase(a(i, 3)) <> LCase("(as per details)") And a(i, 6) <> "" Then
            j = j + 1
            b(j, 1) = i 'Line
            b(j, 2) = a(i, 1) 'Date
            b(j, 3) = a(i, 6) 'Vch Type
            b(j, 4) = a(i, 7) 'Vch No.
            b(j, 5) = a(i, 3) 'Particulars
            b(j, 6) = a(i, 8) 'Debit
            b(j, 7) = a(i, 9) 'Credit
        Else
            k = k + 1
            c(k, 1) = i 'Line
            c(k, 2) = a(i, 1) 'Date
            c(k, 3) = a(i, 6) 'Vch Type
            c(k, 4) = a(i, 7) 'Vch No.
            c(k, 5) = a(i, 3) 'Particulars
            c(k, 6) = a(i, 8) 'Debit
            c(k, 7) = a(i, 9) 'Credit
        End If
    Next
'
    With Sheets("Bank")
        .UsedRange.Clear
        .Range("A1:G1").Value = Array("Line", "Date", "Vch Type", "Vch No.", "Particulars", "Debit", "Credit")
        .Range("A2").Resize(j, 7).Value = b
        .Range("A" & j + 3).Resize(k, 7).Value = c
        .Columns("F:G").NumberFormat = "0.00"
'
        .Range("A" & j + 3).Resize(k, 7).Interior.Pattern = xlSolid
'
        .UsedRange.EntireColumn.AutoFit
        .UsedRange.HorizontalAlignment = xlLeft
'
        .Range("B:B").NumberFormat = "dd-mm-yyyy"
'
        .UsedRange.Interior.Pattern = xlNone
    End With
'
    NewName = Sheets("Original").Range("K1")
    VchNo = 1000
'
    For Each cell In Sheets("Bank").Range("D2:D" & Sheets("Bank").Range("E" & Sheets("Bank").Rows.Count).End(xlUp).Row)
        If Not cell.Offset(, -1) = vbNullString Then
            VchNo = VchNo + 1
            cell.Value = VchNo
        End If
'
        If cell.Offset(0, 1) = "(as per details)" Then cell.Offset(0, 1).Value = NewName
    Next
'
    Set rngReferenceRange = Sheets("Bank").Range("A1").CurrentRegion
    Set rngToCopy = Sheets("Bank").Cells(rngReferenceRange.Rows.Count + 2, 1).CurrentRegion
'
    rngToCopy.Copy
'
'---------------------------------------------------------------------------------------------------------------------
'
    With Sheets("A")
        .Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
'
        .Columns("B:B").NumberFormat = "dd-mm-yyyy"
        .Columns("E:E").Insert Shift:=xlToRight
        .Columns("G:H").Insert Shift:=xlToRight
        .Range("G1").FormulaR1C1 = "=IF(RC[2]="""","""",-RC[2])"
        .Range("H1").FormulaR1C1 = "=IF(RC[2]="""","""",RC[2])"
        .Range("G1:H1").AutoFill Destination:=.Range("G1:H" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
'
    With Sheets("A").Range("B2:D" & Sheets("A").Range("A" & Rows.Count).End(xlUp).Row)
        .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
'
    With Sheets("A")
        .Columns("D").NumberFormat = "0"
        .Columns("G:H").NumberFormat = "0.00"
'
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).Copy
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).Copy Sheets("B").Range("A3")
    End With
'
    Dim Mx As Long
'
'   this range needs to be changed
    With Sheets("B")
        Mx = Application.Max(.Range("K3:K" & .Range("K" & .Rows.Count).End(xlUp).Row))
'
        Sheets("E").Range("A3:AT3").Resize(Mx).Value = .Range("K3:BD3").Resize(Mx).Value            ' Probably should make variable for 'BD' ;)
'
        .UsedRange.Value = .UsedRange.Value
    End With
'
    Sheets("F").Range("B2:AT2").Resize(Mx).Value = Sheets("E").Range("A3:AT3").Resize(Mx).Value
'
'
    Sheets("Bank").Select
    Range("A1").Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
'
    Sheets("Z").Select
    Range("A2").Select
    ActiveSheet.Paste
'
'
    With Sheets("Z")
        .UsedRange.EntireColumn.AutoFit
        .Columns("F:G").Insert Shift:=xlToRight
        .Range("F3").FormulaR1C1 = "=IF(RC[2]="""",RC[3],-RC[2])"
        .Range("G3").FormulaR1C1 = "=-RC[-1]"
'
'       this range needs to be changed
        .Range("F3:G3").AutoFill Destination:=.Range("F3:G2000")
'''        .Range("F2:G2").AutoFill Destination:=.Range("F2:G" & .Range("G" & .Rows.Count)).End(xlUp).Row
'
'       this range needs to be changed
''        .Range("F2:G2000").Select
        .Range("F3:G3", .Range("F3:G3").End(xlDown)).Copy
        .Range("F3:G3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End With
'
    Application.CutCopyMode = False
'
    Dim rr      As Long
    Dim p       As Variant
    Dim pp      As Variant
    Dim s()     As Variant
    Dim ss()    As Variant
'
    s = Array(2, 3, 4, 5, 6, 7)
    ss = Array(2, 3, 4, 8, 7, 9)
    p = Sheets("Z").Cells(Sheets("Z").Rows.Count, 1).End(3).Row
    pp = Sheets("F").Cells(Sheets("F").Rows.Count, 3).End(3).Row + 1
'
    For rr = 0 To UBound(s)
        Sheets("Z").Range(Sheets("Z").Cells(3, s(rr)), Sheets("Z").Cells(p, s(rr))).Copy Sheets("F").Cells(pp, ss(rr))
    Next
'
    Sheets("F").Cells(pp, "f").Resize(Sheets("F").Range("b" & Sheets("Z").Rows.Count).End(3).Row - pp + 1) = NewName
'
    Application.CutCopyMode = True
'
    With Sheets("Z").UsedRange
        .Value = .Value
    End With
'
    Dim da As Long
    Dim ku As Long
'
    With Sheets("F").Range("A1").CurrentRegion
        For da = 2 To .Rows.Count
            If .Cells(da, 7) < 0 Then
                ku = .Cells(da, .Columns.Count).End(xlToLeft).Column
                .Cells(da, ku + 1).Resize(, 2).Value = .Cells(da, 6).Resize(, 2).Value
'
                With .Cells(da, 6).Resize(, .Columns.Count)
                    .Value = .Offset(, 2).Value
                End With
            End If
        Next
    End With
'
    With Sheets("F")
        .UsedRange.HorizontalAlignment = xlGeneral
        .Range("D2", .Range("D2").End(xlDown)).ClearContents
    End With
'
    Sheets("Original").Activate
    Range("A2").Select
'
    Application.ScreenUpdating = True
'
    MsgBox ("File sorted successfully.")
End Sub
 
Upvote 0
Solution
Dang it. That was my mistake. Calculation mode should not have been added at the end of my trial.

Edited version of GetData:

VBA Code:
Sub GetDataV3_2()
'
    Application.ScreenUpdating = False
'
    Dim i                   As Long, ini        As Long, j      As Long, k  As Long
    Dim VchNo               As Long
    Dim cell                As Range
    Dim Fnd                 As Range
    Dim rngReferenceRange   As Range, rngToCopy As Range
    Dim a                   As Variant, b       As Variant, c   As Variant
    Dim NewName             As String
'
'
    With Sheets("Bank")
        Sheets("Original").Columns("A:I").Copy .Range("A1")
        .UsedRange.UnMerge
        Set Fnd = .Range("A:A").Find("Date", , , xlPart, xlByRows, xlNext, False, , False)
        If Not Fnd Is Nothing And Fnd.Row > 1 Then ini = Fnd.Row + 2 Else ini = 1
        a = .Range("A" & ini, .Range("I" & .Rows.Count).End(3)).Value
    End With
'
    ReDim b(1 To UBound(a), 1 To 7)
    ReDim c(1 To UBound(a), 1 To 7)
'
    For i = 1 To UBound(a) - 3
        If LCase(a(i, 3)) <> LCase("(as per details)") And a(i, 6) <> "" Then
            j = j + 1
            b(j, 1) = i 'Line
            b(j, 2) = a(i, 1) 'Date
            b(j, 3) = a(i, 6) 'Vch Type
            b(j, 4) = a(i, 7) 'Vch No.
            b(j, 5) = a(i, 3) 'Particulars
            b(j, 6) = a(i, 8) 'Debit
            b(j, 7) = a(i, 9) 'Credit
        Else
            k = k + 1
            c(k, 1) = i 'Line
            c(k, 2) = a(i, 1) 'Date
            c(k, 3) = a(i, 6) 'Vch Type
            c(k, 4) = a(i, 7) 'Vch No.
            c(k, 5) = a(i, 3) 'Particulars
            c(k, 6) = a(i, 8) 'Debit
            c(k, 7) = a(i, 9) 'Credit
        End If
    Next
'
    With Sheets("Bank")
        .UsedRange.Clear
        .Range("A1:G1").Value = Array("Line", "Date", "Vch Type", "Vch No.", "Particulars", "Debit", "Credit")
        .Range("A2").Resize(j, 7).Value = b
        .Range("A" & j + 3).Resize(k, 7).Value = c
        .Columns("F:G").NumberFormat = "0.00"
'
        .Range("A" & j + 3).Resize(k, 7).Interior.Pattern = xlSolid
'
        .UsedRange.EntireColumn.AutoFit
        .UsedRange.HorizontalAlignment = xlLeft
'
        .Range("B:B").NumberFormat = "dd-mm-yyyy"
'
        .UsedRange.Interior.Pattern = xlNone
    End With
'
    NewName = Sheets("Original").Range("K1")
    VchNo = 1000
'
    For Each cell In Sheets("Bank").Range("D2:D" & Sheets("Bank").Range("E" & Sheets("Bank").Rows.Count).End(xlUp).Row)
        If Not cell.Offset(, -1) = vbNullString Then
            VchNo = VchNo + 1
            cell.Value = VchNo
        End If
'
        If cell.Offset(0, 1) = "(as per details)" Then cell.Offset(0, 1).Value = NewName
    Next
'
    Set rngReferenceRange = Sheets("Bank").Range("A1").CurrentRegion
    Set rngToCopy = Sheets("Bank").Cells(rngReferenceRange.Rows.Count + 2, 1).CurrentRegion
'
    rngToCopy.Copy
'
'---------------------------------------------------------------------------------------------------------------------
'
    With Sheets("A")
        .Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
'
        .Columns("B:B").NumberFormat = "dd-mm-yyyy"
        .Columns("E:E").Insert Shift:=xlToRight
        .Columns("G:H").Insert Shift:=xlToRight
        .Range("G1").FormulaR1C1 = "=IF(RC[2]="""","""",-RC[2])"
        .Range("H1").FormulaR1C1 = "=IF(RC[2]="""","""",RC[2])"
        .Range("G1:H1").AutoFill Destination:=.Range("G1:H" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
'
    With Sheets("A").Range("B2:D" & Sheets("A").Range("A" & Rows.Count).End(xlUp).Row)
        .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
'
    With Sheets("A")
        .Columns("D").NumberFormat = "0"
        .Columns("G:H").NumberFormat = "0.00"
'
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).Copy
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).Copy Sheets("B").Range("A3")
    End With
'
    Dim Mx As Long
'
'   this range needs to be changed
    With Sheets("B")
        Mx = Application.Max(.Range("K3:K" & .Range("K" & .Rows.Count).End(xlUp).Row))
'
        Sheets("E").Range("A3:AT3").Resize(Mx).Value = .Range("K3:BD3").Resize(Mx).Value            ' Probably should make variable for 'BD' ;)
'
        .UsedRange.Value = .UsedRange.Value
    End With
'
    Sheets("F").Range("B2:AT2").Resize(Mx).Value = Sheets("E").Range("A3:AT3").Resize(Mx).Value
'
'
    Sheets("Bank").Select
    Range("A1").Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
'
    Sheets("Z").Select
    Range("A2").Select
    ActiveSheet.Paste
'
'
    With Sheets("Z")
        .UsedRange.EntireColumn.AutoFit
        .Columns("F:G").Insert Shift:=xlToRight
        .Range("F3").FormulaR1C1 = "=IF(RC[2]="""",RC[3],-RC[2])"
        .Range("G3").FormulaR1C1 = "=-RC[-1]"
'
'       this range needs to be changed
        .Range("F3:G3").AutoFill Destination:=.Range("F3:G2000")
'''        .Range("F2:G2").AutoFill Destination:=.Range("F2:G" & .Range("G" & .Rows.Count)).End(xlUp).Row
'
'       this range needs to be changed
''        .Range("F2:G2000").Select
        .Range("F3:G3", .Range("F3:G3").End(xlDown)).Copy
        .Range("F3:G3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End With
'
    Application.CutCopyMode = False
'
    Dim rr      As Long
    Dim p       As Variant
    Dim pp      As Variant
    Dim s()     As Variant
    Dim ss()    As Variant
'
    s = Array(2, 3, 4, 5, 6, 7)
    ss = Array(2, 3, 4, 8, 7, 9)
    p = Sheets("Z").Cells(Sheets("Z").Rows.Count, 1).End(3).Row
    pp = Sheets("F").Cells(Sheets("F").Rows.Count, 3).End(3).Row + 1
'
    For rr = 0 To UBound(s)
        Sheets("Z").Range(Sheets("Z").Cells(3, s(rr)), Sheets("Z").Cells(p, s(rr))).Copy Sheets("F").Cells(pp, ss(rr))
    Next
'
    Sheets("F").Cells(pp, "f").Resize(Sheets("F").Range("b" & Sheets("Z").Rows.Count).End(3).Row - pp + 1) = NewName
'
    Application.CutCopyMode = True
'
    With Sheets("Z").UsedRange
        .Value = .Value
    End With
'
    Dim da As Long
    Dim ku As Long
'
    With Sheets("F").Range("A1").CurrentRegion
        For da = 2 To .Rows.Count
            If .Cells(da, 7) < 0 Then
                ku = .Cells(da, .Columns.Count).End(xlToLeft).Column
                .Cells(da, ku + 1).Resize(, 2).Value = .Cells(da, 6).Resize(, 2).Value
'
                With .Cells(da, 6).Resize(, .Columns.Count)
                    .Value = .Offset(, 2).Value
                End With
            End If
        Next
    End With
'
    With Sheets("F")
        .UsedRange.HorizontalAlignment = xlGeneral
        .Range("D2", .Range("D2").End(xlDown)).ClearContents
    End With
'
    Sheets("Original").Activate
    Range("A2").Select
'
    Application.ScreenUpdating = True
'
    MsgBox ("File sorted successfully.")
End Sub
Give me some time. I am trying it on the sample data.
 
Upvote 0
Dang it. That was my mistake. Calculation mode should not have been added at the end of my trial.

Edited version of GetData:

VBA Code:
Sub GetDataV3_2()
'
    Application.ScreenUpdating = False
'
    Dim i                   As Long, ini        As Long, j      As Long, k  As Long
    Dim VchNo               As Long
    Dim cell                As Range
    Dim Fnd                 As Range
    Dim rngReferenceRange   As Range, rngToCopy As Range
    Dim a                   As Variant, b       As Variant, c   As Variant
    Dim NewName             As String
'
'
    With Sheets("Bank")
        Sheets("Original").Columns("A:I").Copy .Range("A1")
        .UsedRange.UnMerge
        Set Fnd = .Range("A:A").Find("Date", , , xlPart, xlByRows, xlNext, False, , False)
        If Not Fnd Is Nothing And Fnd.Row > 1 Then ini = Fnd.Row + 2 Else ini = 1
        a = .Range("A" & ini, .Range("I" & .Rows.Count).End(3)).Value
    End With
'
    ReDim b(1 To UBound(a), 1 To 7)
    ReDim c(1 To UBound(a), 1 To 7)
'
    For i = 1 To UBound(a) - 3
        If LCase(a(i, 3)) <> LCase("(as per details)") And a(i, 6) <> "" Then
            j = j + 1
            b(j, 1) = i 'Line
            b(j, 2) = a(i, 1) 'Date
            b(j, 3) = a(i, 6) 'Vch Type
            b(j, 4) = a(i, 7) 'Vch No.
            b(j, 5) = a(i, 3) 'Particulars
            b(j, 6) = a(i, 8) 'Debit
            b(j, 7) = a(i, 9) 'Credit
        Else
            k = k + 1
            c(k, 1) = i 'Line
            c(k, 2) = a(i, 1) 'Date
            c(k, 3) = a(i, 6) 'Vch Type
            c(k, 4) = a(i, 7) 'Vch No.
            c(k, 5) = a(i, 3) 'Particulars
            c(k, 6) = a(i, 8) 'Debit
            c(k, 7) = a(i, 9) 'Credit
        End If
    Next
'
    With Sheets("Bank")
        .UsedRange.Clear
        .Range("A1:G1").Value = Array("Line", "Date", "Vch Type", "Vch No.", "Particulars", "Debit", "Credit")
        .Range("A2").Resize(j, 7).Value = b
        .Range("A" & j + 3).Resize(k, 7).Value = c
        .Columns("F:G").NumberFormat = "0.00"
'
        .Range("A" & j + 3).Resize(k, 7).Interior.Pattern = xlSolid
'
        .UsedRange.EntireColumn.AutoFit
        .UsedRange.HorizontalAlignment = xlLeft
'
        .Range("B:B").NumberFormat = "dd-mm-yyyy"
'
        .UsedRange.Interior.Pattern = xlNone
    End With
'
    NewName = Sheets("Original").Range("K1")
    VchNo = 1000
'
    For Each cell In Sheets("Bank").Range("D2:D" & Sheets("Bank").Range("E" & Sheets("Bank").Rows.Count).End(xlUp).Row)
        If Not cell.Offset(, -1) = vbNullString Then
            VchNo = VchNo + 1
            cell.Value = VchNo
        End If
'
        If cell.Offset(0, 1) = "(as per details)" Then cell.Offset(0, 1).Value = NewName
    Next
'
    Set rngReferenceRange = Sheets("Bank").Range("A1").CurrentRegion
    Set rngToCopy = Sheets("Bank").Cells(rngReferenceRange.Rows.Count + 2, 1).CurrentRegion
'
    rngToCopy.Copy
'
'---------------------------------------------------------------------------------------------------------------------
'
    With Sheets("A")
        .Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
'
        .Columns("B:B").NumberFormat = "dd-mm-yyyy"
        .Columns("E:E").Insert Shift:=xlToRight
        .Columns("G:H").Insert Shift:=xlToRight
        .Range("G1").FormulaR1C1 = "=IF(RC[2]="""","""",-RC[2])"
        .Range("H1").FormulaR1C1 = "=IF(RC[2]="""","""",RC[2])"
        .Range("G1:H1").AutoFill Destination:=.Range("G1:H" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
'
    With Sheets("A").Range("B2:D" & Sheets("A").Range("A" & Rows.Count).End(xlUp).Row)
        .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
'
    With Sheets("A")
        .Columns("D").NumberFormat = "0"
        .Columns("G:H").NumberFormat = "0.00"
'
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).Copy
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'
        .Range("B1:H1", .Range("B1:H1").End(xlDown)).Copy Sheets("B").Range("A3")
    End With
'
    Dim Mx As Long
'
'   this range needs to be changed
    With Sheets("B")
        Mx = Application.Max(.Range("K3:K" & .Range("K" & .Rows.Count).End(xlUp).Row))
'
        Sheets("E").Range("A3:AT3").Resize(Mx).Value = .Range("K3:BD3").Resize(Mx).Value            ' Probably should make variable for 'BD' ;)
'
        .UsedRange.Value = .UsedRange.Value
    End With
'
    Sheets("F").Range("B2:AT2").Resize(Mx).Value = Sheets("E").Range("A3:AT3").Resize(Mx).Value
'
'
    Sheets("Bank").Select
    Range("A1").Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
'
    Sheets("Z").Select
    Range("A2").Select
    ActiveSheet.Paste
'
'
    With Sheets("Z")
        .UsedRange.EntireColumn.AutoFit
        .Columns("F:G").Insert Shift:=xlToRight
        .Range("F3").FormulaR1C1 = "=IF(RC[2]="""",RC[3],-RC[2])"
        .Range("G3").FormulaR1C1 = "=-RC[-1]"
'
'       this range needs to be changed
        .Range("F3:G3").AutoFill Destination:=.Range("F3:G2000")
'''        .Range("F2:G2").AutoFill Destination:=.Range("F2:G" & .Range("G" & .Rows.Count)).End(xlUp).Row
'
'       this range needs to be changed
''        .Range("F2:G2000").Select
        .Range("F3:G3", .Range("F3:G3").End(xlDown)).Copy
        .Range("F3:G3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End With
'
    Application.CutCopyMode = False
'
    Dim rr      As Long
    Dim p       As Variant
    Dim pp      As Variant
    Dim s()     As Variant
    Dim ss()    As Variant
'
    s = Array(2, 3, 4, 5, 6, 7)
    ss = Array(2, 3, 4, 8, 7, 9)
    p = Sheets("Z").Cells(Sheets("Z").Rows.Count, 1).End(3).Row
    pp = Sheets("F").Cells(Sheets("F").Rows.Count, 3).End(3).Row + 1
'
    For rr = 0 To UBound(s)
        Sheets("Z").Range(Sheets("Z").Cells(3, s(rr)), Sheets("Z").Cells(p, s(rr))).Copy Sheets("F").Cells(pp, ss(rr))
    Next
'
    Sheets("F").Cells(pp, "f").Resize(Sheets("F").Range("b" & Sheets("Z").Rows.Count).End(3).Row - pp + 1) = NewName
'
    Application.CutCopyMode = True
'
    With Sheets("Z").UsedRange
        .Value = .Value
    End With
'
    Dim da As Long
    Dim ku As Long
'
    With Sheets("F").Range("A1").CurrentRegion
        For da = 2 To .Rows.Count
            If .Cells(da, 7) < 0 Then
                ku = .Cells(da, .Columns.Count).End(xlToLeft).Column
                .Cells(da, ku + 1).Resize(, 2).Value = .Cells(da, 6).Resize(, 2).Value
'
                With .Cells(da, 6).Resize(, .Columns.Count)
                    .Value = .Offset(, 2).Value
                End With
            End If
        Next
    End With
'
    With Sheets("F")
        .UsedRange.HorizontalAlignment = xlGeneral
        .Range("D2", .Range("D2").End(xlDown)).ClearContents
    End With
'
    Sheets("Original").Activate
    Range("A2").Select
'
    Application.ScreenUpdating = True
'
    MsgBox ("File sorted successfully.")
End Sub
Your code has reduced the time by 50%. It can still be reduced if you have noticed why, it is taking so long. Maximum time is taken in sheet B in calculating threads. As Micheal M suggested to write a code instead of the formulas getting the result in sheet B. That is a different question which I will post in my next query. By the way THANK YOU SO MUCH FOR YOUR TIME.
 
Upvote 0
The array formula, 3rd formula if I recall correctly, is the bottleneck. ;)
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

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