Professional Documents
Culture Documents
Sub FileBackUp()
ThisWorkbook.SaveCopyAs Filename:=ThisWorkbook.Path & _
"" & Format(Date, "mm-dd-yy") & " " & _
ThisWorkbook.name
End Sub
Sub UnhideAllWorksheet()
im ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
End Sub
Sub DeleteWorksheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.name <> ThisWorkbook.ActiveSheet.name Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
Next ws
End Sub
Sub CopyWorksheetToNewWorkbook()
ThisWorkbook.ActiveSheet.Copy _
Before:=Workbooks.Add.Worksheets(1)
End Sub
Sub ProtectAllWorskeets()
Dim ws As Worksheet
Dim ps As String
ps = InputBox("Enter a Password.", vbOKCancel)
For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:=ps
Next ws
End Sub
Sub ConvertToValues()
Dim MyRange As Range
Dim MyCell As Range
Select Case MsgBox("You Can't Undo This Action. " & "Save Workbook First?", vbYesNoCancel,
"Alert")
Case Is = vbYes
ThisWorkbook.Save
Case Is = vbCancel
Exit Sub
End Select
Set MyRange = Selection
For Each MyCell In MyRange
If MyCell.HasFormula Then
MyCell.Formula = MyCell.Value
End If
Next MyCell
End Sub
Sub RemoveSpaces()
Dim myRange As Range
Dim myCell As Range
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
Set myRange = Selection
For Each myCell In myRange
If Not IsEmpty(myCell) Then
myCell = Trim(myCell)
End If
Next myCell
End Sub
Coding VBA (Macro) Memberi Tanda Data yang Dianggap Ganda (Duplikat)
Sub HighlightDuplicateValues()
Dim myRange As Range
Dim myCell As Range
Set myRange = Selection
For Each myCell In myRange
If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
myCell.Interior.ColorIndex = 36
End If
Next myCell
End Sub
Sub SaveAsPDF()
Selection.ExportAsFixedFormat Type:=xlTypePDF, OpenAfterPublish:=True
End Sub
Sub PasteAsPicture()
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Pictures.Paste.Select
End Sub
Sub TopTen()
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Top
.Rank = 10
.Percent = False
End With
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Sub ProtectWS()
ActiveSheet.Protect "mypassword", True, True
End Sub
Sub UnprotectWS()
ActiveSheet.Unprotect "mypassword"
End Sub
Sub ConvertUpperCase()
Dim rng As Range
For Each rng In Selection
rng = UCase(rng)
Next rng
End Sub
Sub ConvertLowerCase()
Dim rng As Range
For Each rng In Selection
rng = LCase(rng)
Next rng
End Sub
Coding VBA (Macro) Menyesuaikan Kolom dan Baris Sesuai Ukuran atau
Lebar Text
Sub AutoFitColumns()
Cells.Select
Cells.EntireColumn.AutoFit
End Sub
Sub AutoFitRows()
Cells.Select
Cells.EntireRow.AutoFit
End Sub
Sub SortWorksheets()
Dim i As Integer
Dim j As Integer
Dim iAnswer As VbMsgBoxResult
iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
& "Clicking No will sort in Descending Order", _
vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
For i = 1 To Sheets.Count
For j = 1 To Sheets.Count - 1
If iAnswer = vbYes Then
If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
Sheets(j).Move After:=Sheets(j + 1)
End If
ElseIf iAnswer = vbNo Then
If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
Sheets(j).Move After:=Sheets(j + 1)
End If
End If
Next j
Next i
End Sub
Sub Speak()
Selection.Speak
End Sub
Sub auto_close()
MsgBox "Bye Bye! Don't forget to check other cool stuff on excelchamps.com"
End Sub
Coding VBA (Macro)Konversi Format Tanggal ke Text
Sub date2day()
Dim tempCell As Range
Selection.Value = Selection.Value
For Each tempCell In Selection
If IsDate(tempCell) = True Then
With tempCell
.Value = Day(tempCell)
.NumberFormat = "0"
End With
End If
Next tempCell
End Sub
Sub date2year()
Dim tempCell As Range
Selection.Value = Selection.Value
For Each tempCell In Selection
If IsDate(tempCell) = True Then
With tempCell
.Value = Year(tempCell)
.NumberFormat = "0"
End With
End If
Next tempCell
End Sub
Sub customHeader()
Dim myText As String
myText = InputBox("Enter your text here", "Enter Text")
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = myText
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
End With
End Sub
Coding VBA (Macro) Menghapus Karakter
Sub removeChar()
Dim Rng As Range
Dim rc As String
rc = InputBox("Character(s) to Replace", "Enter Value")
For Each Rng In Selection
Selection.Replace What:=rc, Replacement:=""
Next
End Sub
Sub removeDecimals()
Dim lnumber As Double
Dim lResult As Long
Dim rng As Range
For Each rng In Selection
rng.Value= Int(rng)
rng.NumberFormat= "0"
Next rng
End Sub
Sub lockCellsWithFormulas()
With ActiveSheet
.Unprotect
.Cells.Locked = False
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
.Protect AllowDeletingRows:=True
End With
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
End Sub
Sub deleteBlankWorksheets()
Dim Ws As Worksheet
On Error Resume Next
Application.ScreenUpdating= False
Application.DisplayAlerts= False
For Each Ws In Application.Worksheets
If Application.WorksheetFunction.CountA(Ws.UsedRange) = 0 Then
Ws.Delete
End If
Next
Application.ScreenUpdating= True
Application.DisplayAlerts= True
End Sub
Coding VBA (Macro) Memberikan Tanda pada Data yang Dianggap Unik
Sub highlightUniqueValues()
Dim rng As Range
Set rng = Selection
rng.FormatConditions.Delete
Dim uv As UniqueValues
Set uv = rng.FormatConditions.AddUniqueValues
uv.DupeUnique = xlUnique
uv.Interior.Color = vbGreen
End Sub
Ini adalah coding untuk membuat huruf kapital atau huruf besar secara otomatis dimulai dari
posisi pointer aktif sampai kebawah
Sub HurufBesar()
Dim i As Integer
For i = 65 To 90
ActiveCell.Value = Chr(i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Dan kalau yang ini adalah coding bagaimana membuat huruf A-Z secara otomatis tetapi bukan
kapital melainkan huruf kecil dan sama dimulai dari pointer yang aktif sampai ke bawah.
Sub HurufKecil()
Dim i As Integer
For i = 97 To 122
ActiveCell.Value = Chr(i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Oke lanjut dan dibawah ini adalah coding untuk membuat nomor otomatis dari mulai angka 0-9
silakan Anda ketikkan
Sub Angka()
Dim i As Integer
For i = 48 To 57
ActiveCell.Value = Chr(i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Nah, coding diatas Anda tuliskan melalui modul di VBA Editor dan untuk membuat modul
tentunya saya kira sudah memahaminya Anda tinggal klik Insert kemudian Module baru Anda
tuliskan codingnya.
Setelah Anda membuat desain interface UserForm seperti diatas langkah berikutnya silakan
Double klik untuk CommandButton "Sembunyikan Sheet" dan tuliskan coding berikut ini
Sub SembunyikanSheet()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> ThisWorkbook.ActiveSheet.Name Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
Langkah berikutnya untuk perintah atau coding VBA memunculkan Worksheet silakan tuliskan
code berikut ini
Sub MunculkanSheet()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> ThisWorkbook.ActiveSheet.Name Then
ws.Visible = xlSheetVisible
End If
Next ws
End Sub
sumber : excel id
Dalam sebuah aplikasi biasanya ada fitur untuk mencari data,,bagi user sangat perlu di
butuhkan karna data yang kita simpan ada yang ratusan maupun ribuan, tentu kita sangat sulit
apabila kita ingin mencari data yang kita perlukan, baiklah kali ini saya akan membahas tentang
bagaimana mencari data dengan mudah di listview menggunakan combobox berdasarkan
kategori yang kita inginkan,,Misalnya pencarian Kode, Nama, Alamat, Nomor Transaksi dan
sebagainya. ketika kita pilih contohnya nama barang di combobox dan kemudian kita ketikan
nama barang yang kita inginkan maka nama barang yang kita pilih tersebut tampil di tabel
listview....coding yang di pakai di listview sangatlah jauh berbeda dengan listbox..
komponen yang di perlukan Listview,Combobox dan textbox.
Baiklah buatlah tabel barang seperti di gambar.
Kemudian desainlah form seperti berikut:
Programing VBA yang saya buat kali ini adalah menampilkan data yang dicari dari database pada ListBox, hal ini tidak terlalu
sulit tapi pada saat program dijalankan loading pencariannya akan agak sedikit lama, ini merupakan salah satu kelemahan dari
pada Programing VBA ms.Excell.
Ok, Langsung saja...
2. Selanjutnya membuat Form pencarian, Klik Tab Developer pada Menu Bar Excell, lalu Klik Icon Visual Basic, Rancang lah
Form nya seperti berikut :
Exit Sub
End If
With Worksheets("Sheet1")
Set rngNames = .Range("B3", .Range("B" & Rows.Count).End(xlUp))
End With
With rngNames
arrNames = Evaluate(.Address & "&CHAR(45)&ROW(" & .Address & ")")
End With
arrNames = Application.Transpose(arrNames)
arrResults = Filter(arrNames, TextBox1.Value)
ListBox1.Clear
UserForm_Activate
If UBound(arrResults) = -1 Then
ListBox1.AddItem "Data Tidak Ada"
Else
For i = LBound(arrResults) To UBound(arrResults)
lngRow = Mid(arrResults(i), InStrRev(arrResults(i), "-") + 1)
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = Worksheets("Sheet1").Range("A" & lngRow)
.List(.ListCount - 1, 1) = Worksheets("Sheet1").Range("C" & lngRow)
.List(.ListCount - 1, 2) = Worksheets("Sheet1").Range("D" & lngRow)
.List(.ListCount - 1, 3) = Worksheets("Sheet1").Range("E" & lngRow)
.List(.ListCount - 1, 4) = Worksheets("Sheet1").Range("F" & lngRow)
.List(.ListCount - 1, 5) = Worksheets("Sheet1").Range("G" & lngRow)
End With
Next i
End If
End Sub
4. Jalankan Programnya, caranya Klik tanda panah Hijau pada menu Bar
5. Masuk kata kunci data yang dicari, pada Tabel penerimaan yang saya buat adalah "No.Surat Jalan"
6. Klik Tombol cari, maka data yang berhubungan dengan "No.Surat Jalan" yang saya masukkan ditampilkan pada ListBox,
seperti Gambar Berikut ini ;