JumboCactuar
Well-known Member
- Joined
- Nov 16, 2016
- Messages
- 788
- Office Version
- 365
- Platform
- 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:
Do this:
2. 1 Liner IF Statement
If you are only checking for 1 condition you can simply do
3. Reference VBA Sheet Names
Instead of:
use:
This way if you change the sheet tab names, all your macros will continue to work.
4. Refresh all Pivot Tables in Workbook
5. Find/Replace values in a Range
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
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