Speed up VBA Macro - Getting rid of Activate/Select

Mitchx

New Member
Joined
Oct 20, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi there!

Beforehand, if you need any extra information, as I may have missed something you need then feel free to ask me any questions. Many thanks!

I am in dire need of your help to speed up two macro's. The names in the macro are Dutch, though that shouldn't matter. The macro was written by someone else in the past, but is way too slow now.
And I am not that expertised in VBA code.

Basically I have a sheet called 'Input Kostprijsanalyse' this is where a lot of data validation cells are that extract the data from the "Input SQL" Sheet. So if I pick "Calcnr" in the data validation cell everything else get's
filled in at the "Input Kostprijsanalyse" sheet.

Basically how the Macro works now is that it converts the input from "Input Kostprijsanalyse" and converts it to the logfile with corresponding values(See below macros).
It then deletes the second row of "Input SQL" and then clears the content in O14 of "Input Kostprijsanalyse"(Calcnr). Then loops to again the second row of "Input SQL" etc etc, until there are no more lines left.

As you may see in the bulk macro is that there is use of Activate and Select statements which to my opinion drastically slow down the process.


Below is the first macro

VBA Code:
Sub Log()
   
    'LastRow = Sheets("logfile").Cells(Rows.Count, "C").End(xlUp).Row
    Lastrow = Sheets("logfile").Cells.Find(What:="*", _
                         LookAt:=xlPart, _
                         LookIn:=xlFormulas, _
                         SearchOrder:=xlByRows, _
                         SearchDirection:=xlPrevious, _
                         MatchCase:=False).Row
    Lastrow = Application.WorksheetFunction.Max(Lastrow + 1, 10)
    Debug.Print Lastrow
    
    'read
    Datum = Range("datum").value
    klant_naam = Range("klant_naam").value
    Klantnummer = Range("Klantnummer").value
    Bedrijf = Range("Bedrijf").value
    Klantrating = Range("Klantrating").value
    calcnr = Range("calcnr").value
    aanmaakdatum = Range("aanmaakdatum").value
    contract_nummer = Range("Contract_nummer").value
    kenteken = Range("kenteken").value
    Datum_ingang = Range("Datum_ingang").value
    Calculatie_mantel = Range("Calculatie_mantel").value
    Type_lease = Range("Type_lease").value
    Sale_LB = Range("Sale_LB").value
    Speciale_afspraak = Range("Speciale_afspraak").value
    merk_input = Range("merk_input").value
    model_input = Range("model_input").value
    geelgrijs_input = Range("geelgrijs_input").value
    brandstof_input = Range("brandstof_input").value
    investering = Range("investering").value
    restwaarde = Range("restwaarde").value
    restwaarde_perc = Range("restwaarde_perc").value
    basis_restwaarde = Range("basis_restwaarde").value
    basis_restwaardepercentage = Range("basis_restwaardepercentage").value
    geschatte_verkoopwaarde = Range("geschatte_verkoopwaarde").value
    looptijd = Range("looptijd_input").value
    kilometers = Range("totaal_kilometers").value
    rente = Range("rente_input").value
    kostprijs_rente = Range("kostprijs_rente").value
    commercieel = Range("commercieel").value
    Aanpassing_commercieel = Range("Aanpassing_commercieel").value
    overhead = Range("overhead").value
    speciale_afspraak_bedrag = Range("speciale_afspraak_bedrag").value
    opbrengst_totaal = Range("opbrengsten_totaal").value
    opbrengst_maand = Range("opbrengsten_maand").value
    kosten_totaal = Range("kosten_totaal").value
    kosten_maand = Range("kosten_maand").value
    marge_totaal = Range("marge_totaal").value
    marge_maand = Range("marge_maand").value
    margeperc = Range("margeperc").value
    verkoopresultaat = Range("verkoopresultaat").value
    verkoopresultaatnto = Range("verkoopresultaatnto").value
    verkoop_perc = Range("verkoop_perc").value
    margebruto = Range("margebruto").value
    marge_bto_perc = Range("marge_bto_perc").value
    overhead_kosten = Range("overhead_kosten").value
    marge_netto = Range("marge_netto").value
    marge_nto_perc = Range("marge_nto_perc").value
    marge_basis = Range("marge_basis").value
    marge_basis_perc = Range("marge_basis_perc").value
    afschropbr = Range("afschropbr").value
    verkoopresultaatnto = Range("verkoopresultaatnto").value
    verzopbr = Range("verzopbr").value
    verzmarge = Range("verzmarge").value
    hsbopbr = Range("hsbopbr").value
    hsbmarge = Range("hsbmarge").value
    roopbr = Range("roopbr").value
    romarge = Range("romarge").value
    bopbr = Range("bopbr").value
    bmarge = Range("bmarge").value
    vvopbr = Range("vvopbr").value
    vvmarge = Range("vvmarge").value
    overheadopbr = Range("overheadopbr").value
    overheadmarge = Range("overheadmarge").value
    brandstofopbr = Range("brandstofopbr").value
    brandstofmarge = Range("brandstofmarge").value
    renteopbr = Range("renteopbr").value
    rentemarge = Range("rentemarge").value
    commopbr = Range("commopbr").value
    commmarge = Range("commmarge").value
    contract_status = Range("contract_status").value
    invuller = Range("invuller").value
      
    'log
    Sheets("logfile").Select
    Cells(Lastrow, 3).value = Datum
    Cells(Lastrow, 4).value = klant_naam
    Cells(Lastrow, 5).value = Klantnummer
    Cells(Lastrow, 6).value = Bedrijf
    Cells(Lastrow, 7).value = Klantrating
    Cells(Lastrow, 8).value = calcnr
    Cells(Lastrow, 9).value = aanmaakdatum
    Cells(Lastrow, 10).value = contract_nummer
    Cells(Lastrow, 11).value = kenteken
    Cells(Lastrow, 12).value = Datum_ingang
    Cells(Lastrow, 13).value = Calculatie_mantel
    Cells(Lastrow, 14).value = Type_lease
    Cells(Lastrow, 15).value = Sale_LB
    Cells(Lastrow, 16).value = Speciale_afspraak
    Cells(Lastrow, 17).value = merk_input
    Cells(Lastrow, 18).value = model_input
    Cells(Lastrow, 19).value = geelgrijs_input
    Cells(Lastrow, 20).value = brandstof_input
    Cells(Lastrow, 21).value = investering
    Cells(Lastrow, 22).value = restwaarde
    Cells(Lastrow, 23).value = restwaarde_perc
    Cells(Lastrow, 24).value = basis_restwaarde
    Cells(Lastrow, 25).value = basis_restwaardepercentage
    Cells(Lastrow, 26).value = geschatte_verkoopwaarde
    Cells(Lastrow, 27).value = looptijd
    Cells(Lastrow, 28).value = kilometers
    Cells(Lastrow, 29).value = rente
    Cells(Lastrow, 30).value = kostprijs_rente
    Cells(Lastrow, 31).value = commercieel
    Cells(Lastrow, 32).value = Aanpassing_commercieel
    Cells(Lastrow, 33).value = overhead
    Cells(Lastrow, 34).value = speciale_afspraak_bedrag
    Cells(Lastrow, 35).value = opbrengst_totaal
    Cells(Lastrow, 36).value = opbrengst_maand
    Cells(Lastrow, 37).value = kosten_totaal
    Cells(Lastrow, 38).value = kosten_maand
    Cells(Lastrow, 39).value = marge_totaal
    Cells(Lastrow, 40).value = marge_maand
    Cells(Lastrow, 41).value = margeperc
    Cells(Lastrow, 42).value = verkoopresultaat
    Cells(Lastrow, 43).value = verkoopresultaatnto
    Cells(Lastrow, 44).value = verkoop_perc
    Cells(Lastrow, 45).value = margebruto
    Cells(Lastrow, 46).value = marge_bto_perc
    Cells(Lastrow, 47).value = overhead_kosten
    Cells(Lastrow, 48).value = marge_netto
    Cells(Lastrow, 49).value = marge_nto_perc
    Cells(Lastrow, 50).value = marge_basis
    Cells(Lastrow, 51).value = marge_basis_perc
    Cells(Lastrow, 52).value = afschropbr
    Cells(Lastrow, 53).value = verkoopresultaatnto
    Cells(Lastrow, 54).value = verzopbr
    Cells(Lastrow, 55).value = verzmarge
    Cells(Lastrow, 56).value = hsbopbr
    Cells(Lastrow, 57).value = hsbmarge
    Cells(Lastrow, 58).value = roopbr
    Cells(Lastrow, 59).value = romarge
    Cells(Lastrow, 60).value = bopbr
    Cells(Lastrow, 61).value = bmarge
    Cells(Lastrow, 62).value = vvopbr
    Cells(Lastrow, 63).value = vvmarge
    Cells(Lastrow, 64).value = overheadopbr
    Cells(Lastrow, 65).value = overheadmarge
    Cells(Lastrow, 66).value = brandstofopbr
    Cells(Lastrow, 67).value = brandstofmarge
    Cells(Lastrow, 68).value = renteopbr
    Cells(Lastrow, 69).value = rentemarge
    Cells(Lastrow, 70).value = commopbr
    Cells(Lastrow, 71).value = commmarge
    Cells(Lastrow, 72).value = contract_status
    Cells(Lastrow, 73).value = invuller
End Sub

Second Macro that collaborates with the first macro ("Blad19") is the "Input SQL" Sheet. "Blad 1 is the "Input Kostprijsanalyse" Sheet. "Blad 7" is the Logfile Sheet.
Cell O14 is the calcnr cell


VBA Code:
Sub Bulk()

'screen updating
Application.ScreenUpdating = False

'declare
Dim calcnr As String

Dim i As Integer
Dim iCount As Integer

Dim DoubleCount As Integer
Dim TotalCount As Integer

'determine
i = 1
iCount = Application.WorksheetFunction.CountA(Blad19.Range("A:A")) - 1

'loop
For i = 1 To iCount

'read
calcnr = Range("calcnr").value

If calcnr = "" Then

    calcnr = Blad19.Range("A2")
    
    Range("calcnr") = calcnr
    
End If

'check
If Not Blad7.Range("H:H").Find(calcnr) Is Nothing Then

    DoubleCount = DoubleCount + 1

Else
    
    'execute
    Module2.Log
        
    TotalCount = TotalCount + 1
        
End If

'erase
Blad19.Activate
Rows(2).Select
Selection.Delete

Blad1.Activate
Range("O14").Select
Selection.ClearContents

Next i

'screen updating
Application.ScreenUpdating = True

'erase
frmWait.lblWait.Caption = ""

'close
frmWait.Hide

'message
MsgBox TotalCount & " calculatie(s) gelogd" & vbCrLf & DoubleCount & " dubbele calculatie(s) niet toegevoegd"

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi there!

Beforehand, if you need any extra information, as I may have missed something you need then feel free to ask me any questions. Many thanks!

I am in dire need of your help to speed up two macro's. The names in the macro are Dutch, though that shouldn't matter. The macro was written by someone else in the past, but is way too slow now.
And I am not that expertised in VBA code.

Basically I have a sheet called 'Input Kostprijsanalyse' this is where a lot of data validation cells are that extract the data from the "Input SQL" Sheet. So if I pick "Calcnr" in the data validation cell everything else get's
filled in at the "Input Kostprijsanalyse" sheet.

Basically how the Macro works now is that it converts the input from "Input Kostprijsanalyse" and converts it to the logfile with corresponding values(See below macros).
It then deletes the second row of "Input SQL" and then clears the content in O14 of "Input Kostprijsanalyse"(Calcnr). Then loops to again the second row of "Input SQL" etc etc, until there are no more lines left.

As you may see in the bulk macro is that there is use of Activate and Select statements which to my opinion drastically slow down the process.


Below is the first macro

VBA Code:
Sub Log()
  
    'LastRow = Sheets("logfile").Cells(Rows.Count, "C").End(xlUp).Row
    Lastrow = Sheets("logfile").Cells.Find(What:="*", _
                         LookAt:=xlPart, _
                         LookIn:=xlFormulas, _
                         SearchOrder:=xlByRows, _
                         SearchDirection:=xlPrevious, _
                         MatchCase:=False).Row
    Lastrow = Application.WorksheetFunction.Max(Lastrow + 1, 10)
    Debug.Print Lastrow
   
    'read
    Datum = Range("datum").value
    klant_naam = Range("klant_naam").value
    Klantnummer = Range("Klantnummer").value
    Bedrijf = Range("Bedrijf").value
    Klantrating = Range("Klantrating").value
    calcnr = Range("calcnr").value
    aanmaakdatum = Range("aanmaakdatum").value
    contract_nummer = Range("Contract_nummer").value
    kenteken = Range("kenteken").value
    Datum_ingang = Range("Datum_ingang").value
    Calculatie_mantel = Range("Calculatie_mantel").value
    Type_lease = Range("Type_lease").value
    Sale_LB = Range("Sale_LB").value
    Speciale_afspraak = Range("Speciale_afspraak").value
    merk_input = Range("merk_input").value
    model_input = Range("model_input").value
    geelgrijs_input = Range("geelgrijs_input").value
    brandstof_input = Range("brandstof_input").value
    investering = Range("investering").value
    restwaarde = Range("restwaarde").value
    restwaarde_perc = Range("restwaarde_perc").value
    basis_restwaarde = Range("basis_restwaarde").value
    basis_restwaardepercentage = Range("basis_restwaardepercentage").value
    geschatte_verkoopwaarde = Range("geschatte_verkoopwaarde").value
    looptijd = Range("looptijd_input").value
    kilometers = Range("totaal_kilometers").value
    rente = Range("rente_input").value
    kostprijs_rente = Range("kostprijs_rente").value
    commercieel = Range("commercieel").value
    Aanpassing_commercieel = Range("Aanpassing_commercieel").value
    overhead = Range("overhead").value
    speciale_afspraak_bedrag = Range("speciale_afspraak_bedrag").value
    opbrengst_totaal = Range("opbrengsten_totaal").value
    opbrengst_maand = Range("opbrengsten_maand").value
    kosten_totaal = Range("kosten_totaal").value
    kosten_maand = Range("kosten_maand").value
    marge_totaal = Range("marge_totaal").value
    marge_maand = Range("marge_maand").value
    margeperc = Range("margeperc").value
    verkoopresultaat = Range("verkoopresultaat").value
    verkoopresultaatnto = Range("verkoopresultaatnto").value
    verkoop_perc = Range("verkoop_perc").value
    margebruto = Range("margebruto").value
    marge_bto_perc = Range("marge_bto_perc").value
    overhead_kosten = Range("overhead_kosten").value
    marge_netto = Range("marge_netto").value
    marge_nto_perc = Range("marge_nto_perc").value
    marge_basis = Range("marge_basis").value
    marge_basis_perc = Range("marge_basis_perc").value
    afschropbr = Range("afschropbr").value
    verkoopresultaatnto = Range("verkoopresultaatnto").value
    verzopbr = Range("verzopbr").value
    verzmarge = Range("verzmarge").value
    hsbopbr = Range("hsbopbr").value
    hsbmarge = Range("hsbmarge").value
    roopbr = Range("roopbr").value
    romarge = Range("romarge").value
    bopbr = Range("bopbr").value
    bmarge = Range("bmarge").value
    vvopbr = Range("vvopbr").value
    vvmarge = Range("vvmarge").value
    overheadopbr = Range("overheadopbr").value
    overheadmarge = Range("overheadmarge").value
    brandstofopbr = Range("brandstofopbr").value
    brandstofmarge = Range("brandstofmarge").value
    renteopbr = Range("renteopbr").value
    rentemarge = Range("rentemarge").value
    commopbr = Range("commopbr").value
    commmarge = Range("commmarge").value
    contract_status = Range("contract_status").value
    invuller = Range("invuller").value
     
    'log
    Sheets("logfile").Select
    Cells(Lastrow, 3).value = Datum
    Cells(Lastrow, 4).value = klant_naam
    Cells(Lastrow, 5).value = Klantnummer
    Cells(Lastrow, 6).value = Bedrijf
    Cells(Lastrow, 7).value = Klantrating
    Cells(Lastrow, 8).value = calcnr
    Cells(Lastrow, 9).value = aanmaakdatum
    Cells(Lastrow, 10).value = contract_nummer
    Cells(Lastrow, 11).value = kenteken
    Cells(Lastrow, 12).value = Datum_ingang
    Cells(Lastrow, 13).value = Calculatie_mantel
    Cells(Lastrow, 14).value = Type_lease
    Cells(Lastrow, 15).value = Sale_LB
    Cells(Lastrow, 16).value = Speciale_afspraak
    Cells(Lastrow, 17).value = merk_input
    Cells(Lastrow, 18).value = model_input
    Cells(Lastrow, 19).value = geelgrijs_input
    Cells(Lastrow, 20).value = brandstof_input
    Cells(Lastrow, 21).value = investering
    Cells(Lastrow, 22).value = restwaarde
    Cells(Lastrow, 23).value = restwaarde_perc
    Cells(Lastrow, 24).value = basis_restwaarde
    Cells(Lastrow, 25).value = basis_restwaardepercentage
    Cells(Lastrow, 26).value = geschatte_verkoopwaarde
    Cells(Lastrow, 27).value = looptijd
    Cells(Lastrow, 28).value = kilometers
    Cells(Lastrow, 29).value = rente
    Cells(Lastrow, 30).value = kostprijs_rente
    Cells(Lastrow, 31).value = commercieel
    Cells(Lastrow, 32).value = Aanpassing_commercieel
    Cells(Lastrow, 33).value = overhead
    Cells(Lastrow, 34).value = speciale_afspraak_bedrag
    Cells(Lastrow, 35).value = opbrengst_totaal
    Cells(Lastrow, 36).value = opbrengst_maand
    Cells(Lastrow, 37).value = kosten_totaal
    Cells(Lastrow, 38).value = kosten_maand
    Cells(Lastrow, 39).value = marge_totaal
    Cells(Lastrow, 40).value = marge_maand
    Cells(Lastrow, 41).value = margeperc
    Cells(Lastrow, 42).value = verkoopresultaat
    Cells(Lastrow, 43).value = verkoopresultaatnto
    Cells(Lastrow, 44).value = verkoop_perc
    Cells(Lastrow, 45).value = margebruto
    Cells(Lastrow, 46).value = marge_bto_perc
    Cells(Lastrow, 47).value = overhead_kosten
    Cells(Lastrow, 48).value = marge_netto
    Cells(Lastrow, 49).value = marge_nto_perc
    Cells(Lastrow, 50).value = marge_basis
    Cells(Lastrow, 51).value = marge_basis_perc
    Cells(Lastrow, 52).value = afschropbr
    Cells(Lastrow, 53).value = verkoopresultaatnto
    Cells(Lastrow, 54).value = verzopbr
    Cells(Lastrow, 55).value = verzmarge
    Cells(Lastrow, 56).value = hsbopbr
    Cells(Lastrow, 57).value = hsbmarge
    Cells(Lastrow, 58).value = roopbr
    Cells(Lastrow, 59).value = romarge
    Cells(Lastrow, 60).value = bopbr
    Cells(Lastrow, 61).value = bmarge
    Cells(Lastrow, 62).value = vvopbr
    Cells(Lastrow, 63).value = vvmarge
    Cells(Lastrow, 64).value = overheadopbr
    Cells(Lastrow, 65).value = overheadmarge
    Cells(Lastrow, 66).value = brandstofopbr
    Cells(Lastrow, 67).value = brandstofmarge
    Cells(Lastrow, 68).value = renteopbr
    Cells(Lastrow, 69).value = rentemarge
    Cells(Lastrow, 70).value = commopbr
    Cells(Lastrow, 71).value = commmarge
    Cells(Lastrow, 72).value = contract_status
    Cells(Lastrow, 73).value = invuller
End Sub

Second Macro that collaborates with the first macro ("Blad19") is the "Input SQL" Sheet. "Blad 1 is the "Input Kostprijsanalyse" Sheet. "Blad 7" is the Logfile Sheet.
Cell O14 is the calcnr cell


VBA Code:
Sub Bulk()

'screen updating
Application.ScreenUpdating = False

'declare
Dim calcnr As String

Dim i As Integer
Dim iCount As Integer

Dim DoubleCount As Integer
Dim TotalCount As Integer

'determine
i = 1
iCount = Application.WorksheetFunction.CountA(Blad19.Range("A:A")) - 1

'loop
For i = 1 To iCount

'read
calcnr = Range("calcnr").value

If calcnr = "" Then

    calcnr = Blad19.Range("A2")
   
    Range("calcnr") = calcnr
   
End If

'check
If Not Blad7.Range("H:H").Find(calcnr) Is Nothing Then

    DoubleCount = DoubleCount + 1

Else
   
    'execute
    Module2.Log
       
    TotalCount = TotalCount + 1
       
End If

'erase
Blad19.Activate
Rows(2).Select
Selection.Delete

Blad1.Activate
Range("O14").Select
Selection.ClearContents

Next i

'screen updating
Application.ScreenUpdating = True

'erase
frmWait.lblWait.Caption = ""

'close
frmWait.Hide

'message
MsgBox TotalCount & " calculatie(s) gelogd" & vbCrLf & DoubleCount & " dubbele calculatie(s) niet toegevoegd"

End Sub

Module 2 is the Macro above the Bulk Macro
 
Upvote 0
What is the Bulk Macro intended to do ?

It seems to be looping but apart from the counter incrementing, there seems to be no real dependency on the i.
It seems to be doing the same thing over and over again. Although deleting Row 2 over and over again and clearing O14 in the loop are a bit concerning.
What cell address is does the named range calcnr refer to ?
 
Upvote 0
What is the Bulk Macro intended to do ?

It seems to be looping but apart from the counter incrementing, there seems to be no real dependency on the i.
It seems to be doing the same thing over and over again. Although deleting Row 2 over and over again and clearing O14 in the loop are a bit concerning.
What cell address is does the named range calcnr refer to ?

Hi Alex,

The calcnr refers to =INDIRECT("InputSQL[Calculatie Nummer]"), which is cell A from the "InputSQL" (Blad19 - as mentioned) sheet. It is a data validation cell, so it has a dropdown list.

Below is a small example of the "Input Kostprijsanalyse" sheet. The blue circular mark is the Calcnr. Now nothing is in the "InputSQL" sheet where it retracts the information from.
But basically all other cells refer to that sheet too. So when I select "Offertenummer, which is Calcnr in english all other information get's filled in automatically.

How the program works is like this:

Firstly, I add a line containing client information about their car, this includes personal information but also information about the car this information ranges from cell A TO CX
I put this information in the "Input SQL" sheet. The "Input Kostprijsanalyse" sheet then retrieves the information from the "InputSQL" sheet whenever I select Cell O14 (Calcnr).

Then I also have a "Logfile" sheet. The bulk macro bascially does this: Puts the first calcnr of "Input SQL" into cell O14 of "Input Kostprijsanalyse" all information gets generated and pushed into the logfile. Then deletes the second line of "Input SQL" (first line is the header) and goes onto the next Calcnr puts into O14 on and on. Sometimes I push more than 1000 lines through it, but it takes a while.

I know it is complicated and it is hard to explain, but we also have confidential information of course.



1642074440692.png
 
Upvote 0
I don't know enough about all the parts of the code to make major changes.
In terms of just your original subject line you could replace the "erase" section of the Bulk routine with the below:

VBA Code:
    'erase
    Blad19.Rows(2).Delete
    
    Blad1.Range("O14").ClearContents

Depending on what other parts of the code are doing, an option might be to delete a block of rows at the end based rather than deleting 1 row each time it runs through the loop.
 
Upvote 0

Forum statistics

Threads
1,214,881
Messages
6,122,074
Members
449,064
Latest member
MattDRT

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