Sabtu, 01 Februari 2014

Bagaimana Caranya membuat Macro Excel

Sebagai lanjutan dari posting sebelumnya tentang bagaimana membuat otomatisasi menggunakan macro excel vba dibawah ini disampaikan cara membuat program macro excel untuk tingkatan selanjutnya.
 
Bagaimana Caranya membuat program macro excel

1. Caranya dengan membuat rekaman dengan fasilitas macro recorder.
http://mymacrosformula.blogspot.com
2. Setelah selesai rekaman, kita edit hasil rekaman dan cari yang kemungkinan tidak bisa jalan kalau di play ulang.

3. Beberapa hal dalam macro yang biasanya di edit sendiri setelah selesai merekam macro, yaitu diantaranya : http://mymacrosformula.blogspot.com

  • a. Navigasi cell (mengarahkan cell menuju kemana) http://mymacrosformula.blogspot.com
  • b. Navigasi cell special ( mengarahkan cell ke arah tertentu spt : paling atas, paling bawah, paling ujung dsb)
  • c. Mengganti ganti file workbook yang aktif dan mengganti sheet yang aktif dimana macro bekerja.

4. Membuat macro excel menjadi fleksibel dengan menggunakan macro cell reference, tujuannya agar macro tetap jalan walau ada perubahan tempat cell.contoh macro reference seperti :

  • a. Menggunakan statement dim http://mymacrosformula.blogspot.com/
  • b. Menggunakan define name atau range yang diberi nama http://mymacrosformula.blogspot.com/
  • c. Kalau ada pengerjaan filter, mesti cek lagi tabelnya dijadikan define agar tidak berubah ubah.



5. Menggunakan template macro yang biasa dipakai untuk mengedit dan menyesuaikan macro yang sudah jadi hasil rekaman. Seperti contoh contoh macro dibawah ini : http://mymacrosformula.blogspot.com/


Keterangan
Macro code
Select cell
Range("B2").Select
menuju kespecial cell
ActiveCell.SpecialCells(xlLastCell).Select
Selection.End(xlDown).Select
mengisi cell
ActiveCell.FormulaR1C1 = "jjjj"
menghapus isi cell
Selection.ClearContents
mengaktifkan file excel
Windows("DETAIL 1.xlsx").Activate
mengaktifkan atau memilih sheet
Sheets("Clearing").Select
http://mymacrosformula.blogspot.com/
copy atau salin isi cell
Selection.Copy
Selection.Cut
paste hasil salinan 'http://mymacrosformula.blogspot.com/
ActiveSheet.Paste
Application.CutCopyMode = False
paste special 'http://mymacrosformula.blogspot.com/
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks :=False, Transpose:=False
blok special cell
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
membuat filetr dan criteria 'http://mymacrosformula.blogspot.com/
ActiveSheet.Range("$A$1:$X$245").AutoFilter Field:=2
ActiveSheet.Range("$A$1:$X$245").AutoFilter Field:=14, _
Criteria1:= "=*Cttt frree*", Operator:=xlAnd
Range("J1").Select
Selection.AutoFilter
rplace dan find cell 'http://mymacrosformula.blogspot.com/
ActiveCell.Replace What:="-", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False 'http://mymacrosformula.blogspot.com/
Cells.Find(What:="-", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Replace What:="-", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.FindNext(After:=ActiveCell).Activate

replace selection cell
    Application.Goto Reference:="frshe4"

    Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False 'http://mymacrosformula.blogspot.com/
hati hati setelah find mesti pakai plus minus offset
ActiveCell.Offset(0, -1).Select 'http://mymacrosformula.blogspot.com/
ActiveCell.Offset(0, 1).Select 'http://mymacrosformula.blogspot.com/
format style coma
Selection.Style = "Comma"
Selection.NumberFormat = "_(* #,##0.0_);_(* (#,##0.0);_(* ""- _
""??_);_(@_)" 'http://mymacrosformula.blogspot.com/
Selection.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
format setelah copy dari text
Selection.TextToColumns Destination:=Range("B12"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _ 'http://mymacrosformula.blogspot.com/
:=";", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
Selection.NumberFormat = "0"
jalankan macro yang lain
Application.Run _
"ambilnmvendor1"
Application.Run _ 'http://mymacrosformula.blogspot.com/
"'Lap.xls'!HAPUS_entry"
logika if
If Range("B17").Value = "0" Then
Else
Application.Run _
"copyname"
End If
menangkap jumlah data hasil filter
ActiveCell.FormulaR1C1 = rng.Columns(1). _
SpecialCells(xlCellTypeVisible).Count – 1
menggunakan statement dim untuk memanggil 1 unsur supaya tidak ditulis berulang ulang
'http://mymacrosformula.blogspot.com/
Windows("FILE MACRO LAP.xls").Activate
Dim Transaksilainnya As String
Dim Transaksidetail As String
Dim JenisTransaksi As String
Dim rng As Range 'http://mymacrosformula.blogspot.com/

Transaksilainnya = (Range("b1"))
Transaksidetail = (Range("b4"))
JenisTransaksi = (Range("b5"))

'contoh penggunaan :

Windows(TrsDtlFl).Activate

Set rng = ActiveSheet.AutoFilter.Range
ActiveCell.FormulaR1C1 = rng.Columns(1). _
SpecialCells(xlCellTypeVisible).Count - 1  
'http://mymacrosformula.blogspot.com/

6. satu lagi ada caranya agar macro menjadi lebih cepat dan tidak memusingkan karena di file excel tidak terlihat makro jalan yaitu dengan
 screen updating



Tidak ada komentar:

Posting Komentar

Page View