Problem executing code VBA

sofas

Active Member
Joined
Sep 11, 2022
Messages
486
Office Version
  1. 2019
Platform
  1. Windows
Hello, how can I fix this code or shorten it? It works fine, but it's slow

VBA Code:
Sub TEST()

Dim MT As Worksheet
Set MT = Feuil1
  With MT.Range("F2:F" & MT.Range("a" & Rows.Count).End(3).Row)
      .Formula = "=VLOOKUP(""*""&RC[-1]&""*"",C1:C4,1,0)"
    .Value = .Value
    With MT.Range("G2:G" & MT.Range("a" & Rows.Count).End(3).Row)
     .Formula = "=VLOOKUP(""*""&RC[-2]&""*"",C1:C4,2,0)"
    .Value = .Value
      With MT.Range("H2:H" & MT.Range("a" & Rows.Count).End(3).Row)
      .Formula = "=VLOOKUP(""*""&RC[-3]&""*"",C1:C4,3,0)"
    .Value = .Value
    With MT.Range("I2:I" & MT.Range("a" & Rows.Count).End(3).Row)
      .Formula = "=VLOOKUP(""*""&RC[-4]&""*"",C1:C4,4,0)"
   .Value = .Value
  End With
  End With
  End With
  End With
End Sub
 
There is next update to do
VBA Code:
    ReDim Preserve b(LBound(b, 1) To UBound(b, 1), 1 To 4)

change to
VBA Code:
    ReDim Preserve b(LBound(b, 1) To UBound(b, 1), 1 To 5)
 
Upvote 0
Solution

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
There is next update to do
VBA Code:
    ReDim Preserve b(LBound(b, 1) To UBound(b, 1), 1 To 4)

change to
VBA Code:
    ReDim Preserve b(LBound(b, 1) To UBound(b, 1), 1 To 5)
Thank you very much, the code has been implemented with a huge difference.
you are genius
Only note when there are two values in column b ABC_DEF Example DEF is copied colmun 4
 
  • Like
Reactions: PCL
Upvote 0
Can you manage to turn off calculation:
Formula > Calculation Option > Manual
Run the macro
Turn back calculation to Automatic
and see if it is faster
 
Upvote 0
Good, so you don't need to do the last test.
Enjoy Excel
 
Upvote 0
Good, so you don't need to do the last test.
Enjoy Excel
Screenshot 2023-01-07 203750.png
 
Upvote 0
Try the following code:

VBA Code:
Sub TEST_MH2()
'
    Dim StartTime               As Double
    StartTime = Timer
'
    Dim lr                      As Long
    Dim TableRange              As Range
    Dim FormulasArray(1 To 4)   As Variant, HeaderArray     As Variant
    Dim MT                      As Worksheet
'
    Set MT = Worksheets("sheet4")
'
    Application.ScreenUpdating = False                                                      ' Turn ScreenUpdating off
'
    lr = MT.Range("A" & Rows.Count).End(xlUp).Row                                           ' Get last used row of column A
'
    HeaderArray = MT.Range("F1:I1")                                                         ' Save the header row for columns F:I
'
    Columns("F:I").Delete                                                                   ' Delete the columns of data
'
    Columns("F").Resize(, 4).EntireColumn.Insert                                            ' Insert the blank columns
'
    MT.Range("F1:I1") = HeaderArray                                                         ' Write the header back to the columns
'
'
    MT.ListObjects.Add(xlSrcRange, MT.Range("$F$1:$I$" & lr), , xlYes).Name = "Table1"      ' Convert the F:I range to a table
'
    FormulasArray(1) = "=VLOOKUP(""*""&E2&""*"",A:D,1,0)"                                   ' Column F formula
    FormulasArray(2) = "=VLOOKUP(""*""&E2&""*"",A:D,2,0)"                                   ' Column G formula
    FormulasArray(3) = "=VLOOKUP(""*""&E2&""*"",A:D,3,0)"                                   ' Column H formula
    FormulasArray(4) = "=VLOOKUP(""*""&E2&""*"",A:D,4,0)"                                   ' Column I formula
'
    MT.Range("F2:I2").Formula = FormulasArray                                               ' Write FormulasArray to sheet
'
    With MT.ListObjects("Table1")
        Set TableRange = .Range                                                             '   Save the range of 'Table1'
        .Unlist                                                                             '   Convert the table back to a range
    End With
'
    With TableRange
        .Interior.ColorIndex = xlColorIndexNone                                             '   Remove the .Interior.ColorIndex that the adding table did
        .Font.ColorIndex = xlColorIndexAutomatic                                            '   Remove the .Font.ColorIndex that the adding table did
        .Borders.LineStyle = xlLineStyleNone                                                '   Remove the .Borders.LineStyle that the adding table did
    End With
'
'
    With MT.Range("F2:I" & lr)
        .Value = .Value                                                                     '   Remove the formulas leaving just the values
    End With
'
    Application.ScreenUpdating = True                                                       ' Turn ScreenUpdating back on
'
    MsgBox "Script completed in " & Timer - StartTime & " seconds."                         ' Let user know that script has completed
End Sub
 
Upvote 0
Try the following code:

VBA Code:
Sub TEST_MH2()
'
    Dim StartTime               As Double
    StartTime = Timer
'
    Dim lr                      As Long
    Dim TableRange              As Range
    Dim FormulasArray(1 To 4)   As Variant, HeaderArray     As Variant
    Dim MT                      As Worksheet
'
    Set MT = Worksheets("sheet4")
'
    Application.ScreenUpdating = False                                                      ' Turn ScreenUpdating off
'
    lr = MT.Range("A" & Rows.Count).End(xlUp).Row                                           ' Get last used row of column A
'
    HeaderArray = MT.Range("F1:I1")                                                         ' Save the header row for columns F:I
'
    Columns("F:I").Delete                                                                   ' Delete the columns of data
'
    Columns("F").Resize(, 4).EntireColumn.Insert                                            ' Insert the blank columns
'
    MT.Range("F1:I1") = HeaderArray                                                         ' Write the header back to the columns
'
'
    MT.ListObjects.Add(xlSrcRange, MT.Range("$F$1:$I$" & lr), , xlYes).Name = "Table1"      ' Convert the F:I range to a table
'
    FormulasArray(1) = "=VLOOKUP(""*""&E2&""*"",A:D,1,0)"                                   ' Column F formula
    FormulasArray(2) = "=VLOOKUP(""*""&E2&""*"",A:D,2,0)"                                   ' Column G formula
    FormulasArray(3) = "=VLOOKUP(""*""&E2&""*"",A:D,3,0)"                                   ' Column H formula
    FormulasArray(4) = "=VLOOKUP(""*""&E2&""*"",A:D,4,0)"                                   ' Column I formula
'
    MT.Range("F2:I2").Formula = FormulasArray                                               ' Write FormulasArray to sheet
'
    With MT.ListObjects("Table1")
        Set TableRange = .Range                                                             '   Save the range of 'Table1'
        .Unlist                                                                             '   Convert the table back to a range
    End With
'
    With TableRange
        .Interior.ColorIndex = xlColorIndexNone                                             '   Remove the .Interior.ColorIndex that the adding table did
        .Font.ColorIndex = xlColorIndexAutomatic                                            '   Remove the .Font.ColorIndex that the adding table did
        .Borders.LineStyle = xlLineStyleNone                                                '   Remove the .Borders.LineStyle that the adding table did
    End With
'
'
    With MT.Range("F2:I" & lr)
        .Value = .Value                                                                     '   Remove the formulas leaving just the values
    End With
'
    Application.ScreenUpdating = True                                                       ' Turn ScreenUpdating back on
'
    MsgBox "Script completed in " & Timer - StartTime & " seconds."                         ' Let user know that script has completed
End Sub
Screenshot 2023-01-07 221533.png
Thank you nice and fast
 
Upvote 0

Forum statistics

Threads
1,215,723
Messages
6,126,470
Members
449,315
Latest member
misterzim

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