Macro untuk membuat urutan nomer dari angka 1 hingga sesuai yang anda
input dalam inputbox
Sub AddSerialNumbers()
Dim i As Integer
For i = 1 To i
ActiveCell.Value = i
ActiveCell.Offset(1, 0).Activate
Next i
Last:Exit Sub
End Sub
Meng insert column di antara column yang anda pilih, jumlahnya sesuai
banyak column yang anda isi dalam inputbox
Sub InsertMultipleColumns()
Dim i As Integer
Dim j As Integer
ActiveCell.EntireColumn.Select
For j = 1 To i
Selection.Insert Shift:=xlToRight,
CopyOrigin:=xlFormatFromRightorAbove
Last:Exit Sub
End Sub
Macro ini hampir sama dengan yang atas, tetapi ini berguna untuk
menginsert row.
Sub InsertMultipleRows()
Dim i As Integer
Dim j As Integer
ActiveCell.EntireRow.Select
Columns")
For j = 1 To i
Selection.Insert Shift:=xlToDown,
CopyOrigin:=xlFormatFromRightorAbove
Next j
Last:Exit Sub
End Sub
Cells.Select
Cells.EntireColumn.AutoFit
End Sub
Sub AutoFitRows()
Cells.Select
Cells.EntireRow.AutoFit
End Sub
Sub RemoveWrapText()
Cells.Select
Selection.WrapText = False
Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
End Sub
7. Unmerge Cells
Sub UnmergeCells()
Selection.UnMerge
End Sub
8. Open Calculator
Sub OpenCalculator()
Application.ActivateMicrosoftApp Index:=0
Sub dateInHeader()
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&D"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
End With
ActiveWindow.View = xlNormalView
End Sub
Sub customHeader()
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = myText
.RightHeader = ""
.LeftFooter = ""
.RightFooter = ""
End With
End Sub
Formatting Codes
Kode VBA ini untuk mengubah format dari cells/range cells di excel
Sub HighlightDuplicateValues()
myCell.Interior.ColorIndex = 36
End If
Next myCell
End Sub
Cancel As Boolean)
Target.Cells.EntireRow.Address
Range(strRange).Select
End Sub
Sub TopTen()
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.Count).S
tFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Top
.Rank = 10
.Percent = False
End With
With Selection.FormatConditions(1).Font
.Color = -16752384
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Sub HighlightRanges()
HighlightRange.Interior.ColorIndex = 36
Next RangeName
End Sub
Begitu anda jalankan macro ini, akan menanyakan nilai standar minimal
yang akan di highlight.
Sub HighlightGreaterThanValues()
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue,
Operator:=xlGreater, Formula1:=i
Selection.FormatConditions(Selection.FormatConditions.Count).S
tFirstPriority
With Selection.FormatConditions(1)
.Font.Color = RGB(0, 0, 0)
End With
End Sub
Macro untuk mencari tau nilai terrendah dari Batasan nilai yang anda input.
Sub HighlightLowerThanValues()
Dim i As Integer
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue,
Operator:=xlLower, Formula1:=i
Selection.FormatConditions(Selection.FormatConditions.Count).S
tFirstPriority
With Selection.FormatConditions(1)
.Font.Color = RGB(0, 0, 0)
End Sub
Macro untuk menghighlight angka angka negative dari suatu range yang
anda pilih.
Sub highlightNegativeNumbers()
If WorksheetFunction.IsNumber(Rng) Then
Rng.Font.Color= -16776961
End If
End If
Next
End Sub
Macro untuk mancari suatu kata, dan akan langsung mewarnai kata kata
yang anda cari.
Sub highlightValue()
Dim I As Long
If ActiveWindow.RangeSelection.Count> 1 Then
myTxt= ActiveWindow.RangeSelection.AddressLocal
Else
myTxt= ActiveSheet.UsedRange.AddressLocal
End If
Exit Sub
End If
GoTo LInput
End If
For I = 0 To myRg.Rows.Count-1
With myRg.Range("A1").Offset(I, 0)
.Font.ColorIndex= 1
For J = 1 To Len(.Text)
.Characters(J, Len(myStr)).Font.ColorIndex= 3
End With
Next I
End Sub
Sub highlightCommentCells()
Selection.SpecialCells(xlCellTypeComments).Select
Selection.Style= "Note"
End Sub
Sub highlightAlternateRows()
If rng.RowMod 2 = 1 Then
rng.Value= rng^ (1 / 3)
Else
End If
Next rng
End Sub
Macro untuk menghighlight (mewarnai) cells cells yang salah ejaan (b.
inggris).
BONUS 99 KODE VBA SIAP PAKAI INDOTRAININGCENTER.COM 0858 1749 0339
Sub HighlightMisspelledCells()
Next rng
End Sub
Sub highlightErrors()
Dim i As Integer
If WorksheetFunction.IsError(rng) Then
i = i + 1 rng.Style = "bad"
End If
Next rng
MsgBox "There are total " & i & " error(s) in this worksheet."
End Sub
Macro untuk mencari suatu kata yang anda inginkan dan mewarnainya.
Sub highlightSpecificValues()
Dim i As Integer
If rng = c Then
rng.Style = "Note"
i = i + 1
End If
Next rng
MsgBox "There are total " & i &" "& c & " in this worksheet."
End Sub
Mencari cells yang hanya ada space saja tanpa ada isi lainnya.
Sub blankWithSpace()
rng.Style = "Note"
End If
Next rng
End Sub
Sub highlightMaxValue()
rng.Style = "Good"
End If
Next rng
End Sub
Sub highlightMinValue()
rng.Style = "Good"
End If
Next rng
End Sub
Kode macro untuk mencari nilai nilai yang unik dalam suatu range.
Sub highlightUniqueValues()
rng.FormatConditions.Delete
Dim uv As UniqueValues
Set uv = rng.FormatConditions.AddUniqueValues
uv.Interior.Color = vbGreen
End Sub
Sub columnDifference()
Range("H7:H8,I7:I8").Select
Selection.ColumnDifferences(ActiveCell).Select
Selection.Style= "Bad"
End Sub
Sub rowDifference()
Range("H7:H8,I7:I8").Select
Selection.RowDifferences(ActiveCell).Select
Selection.Style= "Bad"
End Sub
Printing Codes
Kode kode macro untuk menjalankan perintah print.
Sub printComments()
With ActiveSheet.PageSetup
End With
End Sub
Sub printNarrowMargin()
With ActiveSheet.PageSetup
.LeftMargin= Application
.InchesToPoints(0.25)
.RightMargin= Application.InchesToPoints(0.25)
.TopMargin= Application.InchesToPoints(0.75)
.BottomMargin= Application.InchesToPoints(0.75)
.HeaderMargin= Application.InchesToPoints(0.3)
.FooterMargin= Application.InchesToPoints(0.3)
End With
ActiveWindow.SelectedSheets.PrintOutCopies:=1, Collate:=True,
IgnorePrintAreas:=False
End Sub
Sub printSelection()
Selection.PrintOutCopies:=1, Collate:=True
End Sub
Sub printCustomSelection()
Value")
Exit Sub
End If
Value")
Exit Sub
End If
Collate:=True
End Sub
Worksheet Codes
Macro untuk mengontrol dan mengatur sheets dalam excel.
Sub HideWorksheet()
BONUS 99 KODE VBA SIAP PAKAI INDOTRAININGCENTER.COM 0858 1749 0339
Dim ws As Worksheet
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
Sub UnhideAllWorksheet()
Dim ws As Worksheet
ws.Visible = xlSheetVisible
Next ws
End Sub
Sub DeleteWorksheets()
Dim ws As Worksheet
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
Next ws
End Sub
Sub ProtectAllWorskeets()
Dim ws As Worksheet
Dim ps As String
ws.Protect Password:=ps
Next ws
End Sub
Sub Resize_Charts()
Dim i As Integer
For i = 1 To ActiveSheet.ChartObjects.Count
With ActiveSheet.ChartObjects(i)
.Width = 300
.Height = 200
End With
Next i
End Sub
Sub InsertMultipleSheets()
Dim i As Integer
Multiple Sheets")
End Sub
Sub ProtectWS()
End Sub
Sub UnprotectWS()
ActiveSheet.Unprotect "mypassword"
End Sub
Sub SortWorksheets()
Dim i As Integer
Dim j As Integer
Worksheets")
For i = 1 To Sheets.Count
For j = 1 To Sheets.Count - 1
Sheets(j).Move After:=Sheets(j + 1)
End If
Sheets(j).Move After:=Sheets(j + 1)
End If
End If
Next j
Next i
End Sub
Sub lockCellsWithFormulas()
With ActiveSheet
.Unprotect
.Cells.Locked = False
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
End With
End Sub
Macro untuk mengecek bila ada sheets yang kosong maka akan di hapus.
Sub deleteBlankWorksheets()
Dim Ws As Worksheet
Application.ScreenUpdating= False
Application.DisplayAlerts= False
If Application.WorksheetFunction.CountA(Ws.UsedRange) = 0 Then
Ws.Delete
End If
Next
Application.ScreenUpdating= True
Application.DisplayAlerts= True
End Sub
Sub UnhideRowsColumns()
Columns.EntireColumn.Hidden = False
Rows.EntireRow.Hidden = False
End Sub
Sub SaveWorkshetAsPDF()
Dimws As Worksheet
End Sub
Sub DisablePageBreaks()
Application.ScreenUpdating= False
Next Sht
Next wb
Application.ScreenUpdating= True
End Sub
Workbook Codes
Kumpulan macro untuk workbook excel.
Sub FileBackUp()
ThisWorkbook.name
End Sub
Sub CloseAllWorkbooks()
wbs.Close SaveChanges:=True
Next wb
End Sub
Macro untuk mengopy sheet yang aktif menjadi file workbook baru.
Sub CopyWorksheetToNewWorkbook()
ThisWorkbook.ActiveSheet.Copy _
Before:=Workbooks.Add.Worksheets(1)
End Sub
Macro untuk mengirimkan file workbook yang aktif vie email outlook.
Sub Send_Mail()
With OutMail
.to = "Sales@indotrainingcenter.com"
.Attachments.Add ActiveWorkbook.FullName
.display
End With
End Sub
Application.Dialogs(xlDialogSendMail).Show
End Sub
Sub auto_open()
file."
End Sub
Sub auto_close()
excelchamps.com"
End Sub
Sub VisibleWorkbooks()
Dim i As Integer
i = i + 1
End If
Next book
MsgBox i
End Sub
If you want to hide all the subtotals, just run this code.
Sub HideSubtotals()
Dim pt As PivotTable
Dim pf As PivotField
Set pt = ActiveSheet.PivotTables(ActiveCell.PivotTable.name)
If pt Is Nothing Then
Exit Sub
End If
pf.Subtotals(1) = True
pf.Subtotals(1) = False
Next pf
End Sub
wbs.Close SaveChanges:=True
Next wb
End Sub
PivotName = "PivotTable2"
Data_Sheet.Activate
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Pivot_Sheet.PivotTables(PivotName). _
ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase,
SourceData:=NewRange)
Pivot_Sheet.PivotTables(PivotName).RefreshTable
'Complete Message
Pivot_Sheet.Activate
End Sub
Sub activateGetPivotData()
Application.GenerateGetPivotData = True
End Sub
Sub deactivateGetPivotData()
Application.GenerateGetPivotData = False
End Sub
Charts Codes
Gunakan macro ini untuk mangatur chart dari pivot table.
Code dibawah ini untuk mengubah chart menjadi clustered column chart
Sub ChangeChartType()
ActiveChart.ChartType = xlColumnClustered
End Sub
Sub ConvertChartToPicture()
ActiveChart.ChartArea.Copy
ActiveSheet.Range("A1").Select
ActiveSheet.Pictures.Paste.Select
End Sub
Pertama tama pilih chart yang mau ditambah kan title, kemudian jalankan
macro ini
Sub AddChartTitle()
Dim i As Variant
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = i
Last:
Exit Sub
Advanced Codes
Code macro level II.
Sub HideSubtotals()
Dim pt As PivotTable
Dim pf As PivotField
If pt Is Nothing Then
Exit Sub
End If
pf.Subtotals(1) = True
pf.Subtotals(1) = False
Next pf
End Sub
Kalau anda mempunyai 100 sheet misalnya dalam 1 workbook, macro ini
membantu untuk membuat index untuk semua sheets.
Sub TableofContent()
Dim i As Long
Application.DisplayAlerts = False
Worksheets("Table of Content").Delete
Application.DisplayAlerts = True
On Error GoTo 0
ThisWorkbook.Sheets.Add Before:=ThisWorkbook.Worksheets(1)
For i = 1 To Sheets.Count
With ActiveSheet
.Hyperlinks.Add _
Anchor:=ActiveSheet.Cells(i, 1), _
Address:="", _
ScreenTip:=Sheets(i).Name, _
TextToDisplay:=Sheets(i).Name
End With
Next i
End Sub
Selection.Copy
ActiveSheet.Pictures.Paste.Select
End Sub
Sub LinkedPicture()
Selection.Copy
ActiveSheet.Pictures.Paste(Link:=True).Select
End Sub
Sub Speak()
Selection.Speak
End Sub
ActiveSheet.ShowDataForm
End Sub
Worksheets("Goal_Seek").Activate
.GoalSeek_ Goal:=Target, _
ChangingCell:=Range("C2")
End With
Exit Sub
End Sub
search_string = query
chromePath = "C:Program
FilesGoogleChromeApplicationchrome.exe"
End Sub
Formula Codes
Code macro yang berhubungan dengan formula dan fungsi excel.
Sub ConvertToValues()
Select Case MsgBox("You Can't Undo This Action. " & "Save
Case Is = vbYes
ThisWorkbook.Save
Case Is = vbCancel
Exit Sub
End Select
If MyCell.HasFormula Then
MyCell.Formula = MyCell.Value
End If
Next MyCell
End Sub
Untuk menghilangkan space yang tidak perlu dalam suatu select range.
Sub RemoveSpaces()
Select Case MsgBox("You Can't Undo This Action. " & "Save
Workbook First?", _
vbYesNoCancel, "Alert")
Case Is = vbYesThisWorkbook.Save
Case Is = vbCancel
Exit Sub
End Select
myCell = Trim(myCell)
End If
Next myCell
End Sub
End Function
rng.Select
If IsNumeric(ActiveCell.Value) Then
End If
End If
Next
End Sub
rvrse = VBA.strReverse(cell.Value)
End Function
ActiveSheet.ShowDataForm
End Sub
Sub ActivateA1()
Application.ReferenceStyle = xlA1
Else
Application.ReferenceStyle = xlA1
End If
End Sub
Sub TimeStamp()
Dim i As Integer
For i = 1 To 24
ActiveCell.Offset(RowOffset:=1, ColumnOffset:=0).Select
Next i
End Sub
Sub date2day()
Selection.Value = Selection.Value
With tempCell
.Value = Day(tempCell)
.NumberFormat = "0"
End With
End If
Next tempCell
End Sub
Sub date2year()
Selection.Value = Selection.Value
With tempCell
.Value = Year(tempCell)
.NumberFormat = "0"
End With
End If
Next tempCell
End Sub
Rng.Value = VBA.Int(Rng.Value)
End If
Next
Selection.NumberFormat = "dd-mmm-yy"
End Sub
Sub removeDate()
End If
End Sub
Sub convertUpperCase()
Rng.Value = UCase(Rng)
End If
Next
End Sub
Sub convertLowerCase()
If Application.WorksheetFunction.IsText(Rng) Then
Rng.Value= LCase(Rng)
End If
Next
End Sub
Sub convertProperCase()
If WorksheetFunction.IsText(Rng) Then
Rng.Value= WorksheetFunction.Proper(Rng.Value)
End If
Next
Mengubah hanya huruf pertama dalam kalimat menjadi huruf besar lainnya
kecil
Sub convertTextCase()
If WorksheetFunction.IsText(Rng) Then
End If
Next rng
End Sub
Sub removeChar()
Dim rc As String
Next
End Sub
Dim S As String
Dim N As Long
S = Application.WorksheetFunction.Trim(rng.Text)
N = 0
End If
WordCnt = WordCnt + N
Next rng
MsgBox "There are total " & Format(WordCnt, "#,##0") & " words
End Sub
Sub removeApostrophes()
Selection.Value = Selection.Value
End Sub
Sub removeDecimals()
rng.Value= Int(rng)
rng.NumberFormat= "0"
Next rng
End Sub
Mengalikan semua angka dalam selected range dengan angka yang kita
inginkan.
Sub multiplyWithNumber()
"Input Required")
If WorksheetFunction.IsNumber(rng) Then
rng.Value = rng * c
Else
End If
Next rng
End Sub
Sub addNumber()
DimiAs Integer
If WorksheetFunction.IsNumber(rng) Then
rng.Value= rng+ i
Else
End If
Next rng
End Sub
Sub getSquareRoot()
Dim i As Integer
If WorksheetFunction.IsNumber(rng) Then
rng.Value= Sqr(rng)
Else
End If
Next rng
End Sub
Dimi As Integer
If WorksheetFunction.IsNumber(rng) Then
rng.Value = rng ^ (1 / 3)
Else
End If
Nextrng
End Sub
Sub addcAlphabets()
Dim i As Integer
For i= 65 To 90
ActiveCell.Value= Chr(i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Sub addsAlphabets()
Dim i As Integer
For i= 97 To 122
ActiveCell.Value= Chr(i)
ActiveCell.Offset(1, 0).Select
Next i
Sub convertToNumbers()
Selection.Value= Selection.Value
rng.Value= WorksheetFunction.Arabic(rng)
End If
Next rng
End Sub
Sub removeNegativeSign()
Selection.Value= Selection.Value
If WorksheetFunction.IsNumber(rng)
End If
Next rng
End Sub
Sub replaceBlankWithZero()
Selection.Value= Selection.Value
rng.Value= "0"
Else
End If
Next rng
End Sub