Make VBA Faster

Sara.

New Member
Joined
Jun 16, 2011
Messages
3
I'm looking to make my vba code run faster. Any suggestions?

Sub ShadeAllSw()
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
ActiveSheet.DisplayPageBreaks = False
ActiveSheet.ChartObjects("Chart 3").Activate
Dim DrObj
Dim Pict
Set DrObj = ActiveChart.Shapes
'Deletes pictures off of the sheet

For Each Pict In DrObj
If Left(Pict.Name, 8) = "Freeform" Then
Pict.Select
Pict.Delete
End If
Next

ActiveSheet.ChartObjects("Chart 3").Activate
Dim depth
Dim numrows As Integer, rowcount As Integer
Dim ZoneCounter As Integer

Dim MyRange As Variant
MyRange = Sheets("Graphs").Range("A1:AC10000")

depth = MyRange(34, 15)
numzones = MyRange(41, 14)
numrows = MyRange(44, 14) * 2

ReDim topbot(numzones, 2) As Double
ReDim LogArray(numrows, 3) As Double

ZoneCounter = 1
Do While ZoneCounter <= numzones
topbot(ZoneCounter, 1) = MyRange(ZoneCounter + 1, 2)
topbot(ZoneCounter, 2) = MyRange(ZoneCounter + 1, 3)

ZoneCounter = ZoneCounter + 1
Loop


rowcount = 1
Do While rowcount <= numrows
LogArray(rowcount, 1) = MyRange(rowcount + 34, 17)
LogArray(rowcount, 2) = MyRange(rowcount + 34, 29)
LogArray(rowcount, 3) = MyRange(rowcount + 34, 28)


If LogArray(rowcount, 2) > 100 Then
LogArray(rowcount, 2) = 100
End If
rowcount = rowcount + 1
Loop

Dim depthcounter As Double

Dim FlagStart As Double
Dim FlagEnd As Double
Dim IsPay As Boolean

ZoneCounter = 1
Do While ZoneCounter <= numzones

FlagStart = 0
FlagEnd = 0
IsPay = False

Dim row As Double

depthcounter = topbot(ZoneCounter, 1)

Do While depthcounter <= topbot(ZoneCounter, 2)
row = 2 * (depthcounter - depth) + 1
If LogArray(2 * (depthcounter - depth) + 1, 3) = 0.5 Then
IsPay = True
If FlagStart = 0 Then
FlagStart = LogArray(2 * (depthcounter - depth) + 1, 1)
End If
End If
If IsPay = True And LogArray(2 * (depthcounter - depth) + 1, 3) = 0 Then
FlagEnd = LogArray(2 * (depthcounter - depth - 0.5) + 1, 1)
IsPay = False
End If
If FlagEnd = 0 And depthcounter = topbot(ZoneCounter, 2) Then
FlagEnd = topbot(ZoneCounter, 2)
End If
If FlagStart > 0 And FlagEnd > 0 Then
ShadeLeft FlagStart, FlagEnd
FlagStart = 0
FlagEnd = 0
End If
depthcounter = depthcounter + 0.5

Loop

ZoneCounter = ZoneCounter + 1
Loop

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
ActiveSheet.DisplayPageBreaks = True
End Sub
Sub ShadeLeft(PayStart As Double, PayEnd As Double)

ActiveSheet.ChartObjects("Chart 3").Activate
Dim MyRange As Variant
MyRange = Sheets("Graphs").Range("A1:AC10000")
Dim depth
depth = MyRange(34, 15)
Dim rowcount As Double
Set myCht = ActiveChart
Xleft = myCht.PlotArea.InsideLeft
xwidth = myCht.PlotArea.InsideWidth
Ytop = myCht.PlotArea.InsideTop
Yheight = myCht.PlotArea.InsideHeight
'Yheight = 4172.5
Xmin = myCht.Axes(1).MinimumScale
Xmax = myCht.Axes(1).MaximumScale
Ymax = myCht.Axes(2).MinimumScale
Ymin = myCht.Axes(2).MaximumScale
Dim numrows As Double
numrows = MyRange(44, 14) * 2

ReDim LogArray(numrows, 2) As Double


rowcount = 1
Do While rowcount <= numrows
LogArray(rowcount, 1) = MyRange(rowcount + 34, 17)
LogArray(rowcount, 2) = MyRange(rowcount + 34, 29)


If LogArray(rowcount, 2) > 100 Then
LogArray(rowcount, 2) = 100
End If
rowcount = rowcount + 1
Loop

' first point
Xnode = Xleft
Ynode = Ytop + (Ymax - PayStart) * Yheight / (Ymax - Ymin)
Set myBuilder = myCht.Shapes.BuildFreeform(msoEditingAuto, Xnode, Ynode)

' remaining points
For i = PayStart To PayEnd
Xnode = Xleft + (LogArray((2 * (i - depth)), 2) - Xmin) * xwidth / (Xmax - Xmin)
Ynode = Ytop + (Ymax - LogArray((2 * (i - depth)), 1)) * Yheight / (Ymax - Ymin)
myBuilder.AddNodes msoSegmentLine, msoEditingAuto, Xnode, Ynode
Next i

Xnode = Xleft
Ynode = Ytop + (Ymax - PayEnd) * Yheight / (Ymax - Ymin)
myBuilder.AddNodes msoSegmentLine, msoEditingAuto, Xnode, Ynode

Xnode = Xleft
Ynode = Ytop + (Ymax - PayStart) * Yheight / (Ymax - Ymin)
myBuilder.AddNodes msoSegmentLine, msoEditingAuto, Xnode, Ynode

Set myShape = myBuilder.ConvertToShape

With myShape
.Fill.ForeColor.SchemeColor = 5
.PictureFormat.TransparentBackground = msoCTrue
.Fill.Transparency = 0.5
If PayStart = PayEnd Then
.Line.Visible = True
Else
.Line.Visible = False
End If
End With
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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