Minimize and Optimize VBA Code !!

Pramodpandit123

New Member
Joined
Apr 18, 2020
Messages
30
Office Version
  1. 2016
Platform
  1. Windows
As I am just starter in VBA the code that I've made(with help of this forum via @Yongle ) is too bulky and runs too slow. I need to reduce the code size and make it run faster !!

VBA Code:
Option Explicit

Private Sub RUN()
    Dim ws As Worksheet, ocol As Long, x As Long, y As Long, orow As Long, m As Long, n As Long, o As Long
    Dim cel As Range, cel4 As Range, cel5 As Range, cel6 As Range, cel2 As Range, cel0 As Range, cel3 As Range, cel7 As Range, a As Range
    Dim shp1 As Shape, shp2 As Shape, shp3 As Shape, shp4 As Shape, shp As Shape
    Set ws = ActiveSheet
    orow = 4
    ocol = 4
    x = ws.Range("A4").Value
    y = ws.Range("A5").Value
'number shapes
    Set cel = Range("E6")
    Set cel0 = cel.Offset(orow * y, 0)
    Set cel2 = cel.Offset(1, 0)
    Set cel3 = cel0.Offset(-1, 0)
    Set cel4 = cel.Offset(0, 2)
    Set cel5 = cel.Offset(3, 0)
    Set cel6 = cel.Offset(5, 0)
    Set cel7 = cel.Offset(2, 0)
    For m = 1 To x
        Set shp = ws.Shapes.AddShape(msoShapeOval, cel.Left, cel.Top, cel.Width, cel.Width)
        Set shp1 = ws.Shapes.AddShape(msoShapeOval, cel0.Left, cel0.Top, cel0.Width, cel0.Width)
       Set shp2 = ws.Shapes.AddLine(cel2.Left + cel2.Width / 2, cel2.Top, cel2.Left + cel2.Width / 2, cel2.Top + cel2.Height)
       Set shp3 = ws.Shapes.AddLine(cel3.Left + cel3.Width / 2, cel3.Top, cel3.Left + cel3.Width / 2, cel3.Top + cel3.Height)
        shp.Select
         With Selection.ShapeRange
            .Fill.Visible = msoFalse
            With .TextFrame
             .Characters.Text = m
                .Characters.Font.ColorIndex = 3
                .HorizontalAlignment = xlHAlignCenter
                .VerticalAlignment = xlVAlignCenter
            End With
            With .Line
                .Visible = msoTrue
                .ForeColor.RGB = RGB(255, 0, 0)
                .Transparency = 0
            End With
        End With
        shp1.Select
        With Selection.ShapeRange
            .Fill.Visible = msoFalse
            With .TextFrame
             .Characters.Text = m
                .Characters.Font.ColorIndex = 3
                .HorizontalAlignment = xlHAlignCenter
                .VerticalAlignment = xlVAlignCenter
            End With
            With .Line
                .Visible = msoTrue
                .ForeColor.RGB = RGB(255, 0, 0)
                .Transparency = 0
            End With
             shp2.Select
        With Selection.ShapeRange.Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 0, 0)
        .DashStyle = msoLineLongDashDotDot
        .Weight = 1.5
            End With
            shp3.Select
        With Selection.ShapeRange.Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 0, 0)
        .DashStyle = msoLineLongDashDotDot
        .Weight = 1.5
            End With
             For n = 1 To y - 1
       Set shp4 = ws.Shapes.AddLine(cel5.Left + cel5.Width / 2, cel5.Top, cel6.Left + cel6.Width / 2, cel6.Top + cel6.Height)
            shp4.Select
        With Selection.ShapeRange.Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 0, 0)
        .DashStyle = msoLineLongDashDotDot
        .Weight = 1.5
            End With
             Set cel5 = cel5.Offset(orow, 0)
        Set cel6 = cel6.Offset(orow, 0)
            Next
            cel4.Select
            With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Interior.Pattern = xlSolid
        .Interior.PatternColorIndex = xlAutomatic
        .Interior.ThemeColor = xlThemeColorAccent6
        .Interior.TintAndShade = 0.4
        .Interior.PatternTintAndShade = 0
        End With
        For o = 1 To y
        cel7.Value = "C1"
         cel7.Select
            With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        End With
        Set cel7 = cel7.Offset(orow, 0)
        Next
        End With
        Set cel5 = cel.Offset(3, 0)
        Set cel6 = cel.Offset(5, 0)
        Set cel7 = cel.Offset(2, 0)
        Set cel = cel.Offset(0, ocol)
        Set cel0 = cel0.Offset(0, ocol)
        Set cel2 = cel2.Offset(0, ocol)
        Set cel3 = cel3.Offset(0, ocol)
        Set cel5 = cel5.Offset(0, ocol)
        Set cel6 = cel6.Offset(0, ocol)
        Set cel7 = cel7.Offset(0, ocol)
        Set a = cel.Offset(0, ocol)
        If m < x - 1 Then
        Set cel4 = cel.Offset(0, ocol - 2)
        End If
        Next

'alpha shapes
    Set cel = Range("C8")
    Set cel0 = cel.Offset(0, ocol * x)
    Set cel2 = cel.Offset(0, 1)
    Set cel3 = cel0.Offset(0, -1)
      Set cel4 = cel.Offset(2, 0)
        Set cel5 = cel.Offset(0, 3)
    Set cel6 = cel.Offset(0, 5)
            For m = 1 To y
        Set shp = ws.Shapes.AddShape(msoShapeOval, cel.Left, cel.Top, cel.Width, cel.Width)
        Set shp1 = ws.Shapes.AddShape(msoShapeOval, cel0.Left, cel0.Top, cel0.Width, cel0.Width)
        Set shp2 = ws.Shapes.AddLine(cel2.Left, cel2.Top + cel2.Height / 2, cel2.Left + cel2.Width, cel2.Top + cel2.Height / 2)
          Set shp3 = ws.Shapes.AddLine(cel3.Left, cel3.Top + cel3.Height / 2, cel3.Left + cel3.Width, cel3.Top + cel3.Height / 2)
        shp.Select
        With Selection.ShapeRange
            .Fill.Visible = msoFalse
            With .TextFrame
             .Characters.Text = Chr(m + 64)
                .Characters.Font.ColorIndex = 3
                .HorizontalAlignment = xlHAlignCenter
                .VerticalAlignment = xlVAlignCenter
            End With
            With .Line
                .Visible = msoTrue
                .ForeColor.RGB = RGB(255, 0, 0)
                .Transparency = 0
            End With
        End With
        shp1.Select
        With Selection.ShapeRange
            .Fill.Visible = msoFalse
            With .TextFrame
             .Characters.Text = Chr(m + 64)
                .Characters.Font.ColorIndex = 3
                .HorizontalAlignment = xlHAlignCenter
                .VerticalAlignment = xlVAlignCenter
            End With
            With .Line
                .Visible = msoTrue
                .ForeColor.RGB = RGB(255, 0, 0)
                .Transparency = 0
            End With
            shp2.Select
        With Selection.ShapeRange.Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 0, 0)
        .DashStyle = msoLineLongDashDotDot
        .Weight = 1.5
            End With
            shp3.Select
        With Selection.ShapeRange.Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 0, 0)
        .DashStyle = msoLineLongDashDotDot
        .Weight = 1.5
        End With
        For n = 1 To x - 1
       Set shp4 = ws.Shapes.AddLine(cel5.Left, cel5.Top + cel5.Height / 2, cel6.Left + cel6.Width, cel6.Top + cel6.Height / 2)
            shp4.Select
        With Selection.ShapeRange.Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 0, 0)
        .DashStyle = msoLineLongDashDotDot
        .Weight = 1.5
            End With
             Set cel5 = cel5.Offset(0, ocol)
        Set cel6 = cel6.Offset(0, ocol)
            Next
         cel4.Select
            With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Interior.Pattern = xlSolid
        .Interior.PatternColorIndex = xlAutomatic
        .Interior.ThemeColor = xlThemeColorAccent6
        .Interior.TintAndShade = 0.4
        .Interior.PatternTintAndShade = 0
        End With
        End With
        Set cel5 = cel.Offset(0, 3)
        Set cel6 = cel.Offset(0, 5)
        Set cel = cel.Offset(orow, 0)
        Set cel0 = cel0.Offset(orow, 0)
        Set cel2 = cel2.Offset(orow, 0)
        Set cel3 = cel3.Offset(orow, 0)
        Set cel5 = cel5.Offset(orow, 0)
        Set cel6 = cel6.Offset(orow, 0)
        If m < y - 1 Then
        Set cel4 = cel.Offset(orow - 2, 0)
        End If
        Next

ws.Range("B4").Select

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
that looks rather different to the code I last saw :unsure:
 
Upvote 0
if you want help it would be a good idea to show the expected output from the code and explain how you plan to use it etc
- there are lots of issues in your code and I think it would be simplest to start afresh
 
Upvote 0
I modified the according to my need by using your code as Foundation/Base.Its main purpose is to use it as visual representation to show Gridlines ie as column seperator.
What are the issues do you see in code ? Would you be kind enough to just point my mistakes/improvements to be made on it briefly so that i could further will dig on that subject and learn !!
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

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