Want to make recorded macro code simple

Sufiyan97

Well-known Member
Joined
Apr 12, 2019
Messages
1,538
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I want to make below recorded macro code simple and short

VBA Code:
Sub Default()
'
' Default Macro
'

'
    Range("B3:F50").Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Font
        .Name = "Calibri"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With Selection.Font
        .Name = "Calibri"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With Selection.Font
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "=TODAY()"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "=TEXT(R[-1]C,""dddd"")"
    Range("A2:A3").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("G1").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "My Text"
    Range("M15").Select
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
The short version looks like ...

VBA Code:
Sub Sufiyan97()

    With Range("B3:F50")

        With .Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With

        .Borders(xlDiagonalDown).LineStyle = xlNone
        .Borders(xlDiagonalUp).LineStyle = xlNone

        With .Borders
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With

        With .Font
            .ColorIndex = xlAutomatic
            .Name = "Calibri"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor
        End With
    End With

    Range("A2").Formula = Date
    Range("A3").Formula = Format(Date, "dddd")
    Range("G1").Value = "My Text"
End Sub
 
Upvote 0
Solution
The Macro Recorder is very literal, and records each cell selection.
In VBA, you usually don't really need to select range before working with it,
So most cases where one line ends "Select" and the next line begins with "Selection" can be combined into one, like GWteB did.

Not only is it shorter, but removing the "Select" statements will also help to speed up your code.
 
Upvote 0
The short version looks like ...

VBA Code:
Sub Sufiyan97()

    With Range("B3:F50")

        With .Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With

        .Borders(xlDiagonalDown).LineStyle = xlNone
        .Borders(xlDiagonalUp).LineStyle = xlNone

        With .Borders
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With

        With .Font
            .ColorIndex = xlAutomatic
            .Name = "Calibri"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor
        End With
    End With

    Range("A2").Formula = Date
    Range("A3").Formula = Format(Date, "dddd")
    Range("G1").Value = "My Text"
End Sub

Thank you very much GWteB, Works perfect and faster.

The Macro Recorder is very literal, and records each cell selection.
In VBA, you usually don't really need to select range before working with it,
So most cases where one line ends "Select" and the next line begins with "Selection" can be combined into one, like GWteB did.

Not only is it shorter, but removing the "Select" statements will also help to speed up your code.

as I don't know how to write VBA codes I generally prefer Macro recording for small repetitive tasks,

and Yes now GWteB code works faster.
 
Upvote 0
as I don't know how to write VBA codes I generally prefer Macro recording for small repetitive tasks,
What I provided was a "quick little tip" on how to make recorded code shorter and a little more efficient.
Make minor edits to recorded VBA code is a good way to get started in understanding and writing your own VBA.
 
Upvote 0
What I provided was a "quick little tip" on how to make recorded code shorter and a little more efficient.
Make minor edits to recorded VBA code is a good way to get started in understanding and writing your own VBA.

Yes, I understand it.
I will try to edit recorded macros now onwards
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
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