VBA Tips, Tricks and useful Code Snippets

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
If you have any to add to this, appreciate it if you would post any you use regularly. I'm fairly new to vba and stuff like this helps with learning. Sorry if something like this has been posted before but couldn't see any:

1. Copy + Paste Values
Instead of this:

PHP:
Range("A1:A10").Select
Selection.Copy
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteValues


Do this:
PHP:
Range("D1:D10").Value2 = Range("A1:A10").Value2



2. 1 Liner IF Statement
If you are only checking for 1 condition you can simply do
PHP:
If Range("A1") = "Red" Then Range("B1") = 1



3. Reference VBA Sheet Names
0a28a64688.png


Instead of:
PHP:
Sheets("Sheet1").Range("D1:D10").Value2 = Sheets("Sheet1").Range("A1:A10").Value2

use:
PHP:
MainSheet.[D1:D10].Value2 = MainSheet.[A1:A10].Value2

This way if you change the sheet tab names, all your macros will continue to work.



4. Refresh all Pivot Tables in Workbook
PHP:
Sub RefreshPivots()
Dim WS As Worksheet
Dim PT As PivotTable
    
    For Each WS In Worksheets
            For Each PT In WS.PivotTables
                PT.RefreshTable
            Next PT
    Next WS

End Sub



5. Find/Replace values in a Range
PHP:
Sub NoBlanks()
Range("A1:D50").Select
    For Each r In Selection
        If r.Text = "" Then
            r.Value = 0
        End If
    Next r
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Number 1 Could be done like this:

Code:
Range("A1:A10").Copy
Range("D1").PasteSpecial Paste:=xlPasteValues
 
Last edited:
Upvote 0
Code:
MainSheet.[D1:D10].Value2 = MainSheet.[A1:A10].Value2
The brackets cause the argument to be evaluated by Excel, not VBA. It uses an unnecessary, out-of-process function call and is slow.
 
Last edited:
Upvote 0
Code:
MainSheet.[D1:D10].Value2 = MainSheet.[A1:A10].Value2
The brackets cause the argument to be evaluated by Excel, not VBA. It uses an unnecessary, out-of-process function call and is slow.

Thanks for the heads up.

So this is better?
Code:
Mainsheet.Range("D1:D10").Value2
 
Upvote 0
Depends, I reckon. For code that is running interactively with a user who will never notice milliseconds versus microseconds, it doesn't matter. For code operating in a busy loop, it could get ugly.
 
Upvote 0
I do always wonder what "Slow" means.
Some people think if a script takes. 2.7 seconds to run down 100K rows of data that is slow.
Others think .247 seconds is "Slow"
 
Upvote 0

Forum statistics

Threads
1,221,442
Messages
6,159,905
Members
451,601
Latest member
terrynelson55

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