Can anyone simply the following VBA for me? many thanks

marcusbox

New Member
Joined
Jul 9, 2014
Messages
10
Hi all,

Could anybody simply the following code with faster execution? Besides, how can I separate the last script "If Range("AH295") <> "" Then Update_Histogram" into another Sub without crashing the original one? many thanks

Private Const BecordToSheetName = "Tickers"



Sub Worksheet_Calculate()
Application.EnableEvents = False
With ThisWorkbook.Sheets(BecordToSheetName)
Dim r As Long
r = Application.WorksheetFunction.CountA(.columns(34)) + 1
If Range("M12") <> 0 Then
.Cells(r, 34).value = Range("M12").value
.Cells(r, 38).value = Range("Z12").value
.Cells(r, 35).value = Range("M31").value
.Cells(r, 36).value = Range("M10").value
.Cells(r, 37).value = Range("M83").value
Range("N12").value = Range("AF12").value
End If
End With
If Range("AH295") <> "" Then Update_Histogram
Application.EnableEvents = True
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Not quite sure what you want but if you want to speed up the code, maybe you need to turn off automatic calculation.

Bracket your code (just like you did for EnableEvents) as in

Code:
    Dim oldCalc: oldCalc = Application.Calculation
    Application.Calculation = xlCalculationManual
    '...
    Application.Calculate
    Application.Calculation = oldCalc

Also, using EnableEvents with error trapping is a prescription for a disaster. See Monitor Events particularly the section 'An Excel specific trap: the EnableEvents property'

Hi all,

Could anybody simply the following code with faster execution? Besides, how can I separate the last script "If Range("AH295") <> "" Then Update_Histogram" into another Sub without crashing the original one? many thanks

Private Const BecordToSheetName = "Tickers"



Sub Worksheet_Calculate()
Application.EnableEvents = False
With ThisWorkbook.Sheets(BecordToSheetName)
Dim r As Long
r = Application.WorksheetFunction.CountA(.columns(34)) + 1
If Range("M12") <> 0 Then
.Cells(r, 34).value = Range("M12").value
.Cells(r, 38).value = Range("Z12").value
.Cells(r, 35).value = Range("M31").value
.Cells(r, 36).value = Range("M10").value
.Cells(r, 37).value = Range("M83").value
Range("N12").value = Range("AF12").value
End If
End With
If Range("AH295") <> "" Then Update_Histogram
Application.EnableEvents = True
End Sub
 
Upvote 0
The code you posted ran on my laptop in 16.42 milliseconds (0.01642 seconds) on the first run and 4.62 milliseconds on a second run with hard-coded numbers.
Obviously not running the Update_Histogram program as we have no idea what that program is (don't like the way to find the last row in Column 34 code though).

I suspect that 16.42 milliseconds is fast enough and so if I was you I'd be looking at the Update_Histogram program.

As for

Besides, how can I separate the last script "If Range("AH295") <> "" Then Update_Histogram" into another Sub without crashing the original one?

If the sub that you have shown is "the original one" then you can just delete the line. If the code shown is not "the original one" then you haven't provided enough info to answer the question.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,335
Members
452,636
Latest member
laura12345

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