Rabu, 23 November 2011

Bedah Tunas Gadget: Change Case

Kali ini saya akan mencoba membedah source code dari salah satu fitur Tunas Gadget yaitu Change Case. Change Case bertujuan untuk merubah huruf besar dan kecil pada sel Excel menjadi HURUF BESAR (UPPERCASE), huruf kecil (lower case), Huruf kalimat (Sentense case) dan Huruf Judul (Title Case). Jika fitur ini mudah didapatkan di Microsoft Word, maka untuk di Microsoft Excel kita bisa menggunakan fitur ini.

Di sini saya menggunakan form sebagai Graphic User Interface (GUI) atau antar muka agar kita bisa lebih mudah menjalankan fungsinya dibanding kita menggunakan function-function yang terpisah. Karena dalam fitur Change Case ini terdapat 8 function sehingga akan lebih mudah jika kita menggunakan form daripada 8 function tersebut.

Kita dapat membuat sebuah file baru yang disimpan sebagai file Excel Add-in atau sebagai file excel biasa. Saran saya kita memilih sebagai file Excel Add-in.

Selanjutnya kita membuat form dengan menekan tombol Alt + F8 (Microsoft Visual Basic) lalu Insert > User Form sehingga muncul seperti gambar berikut:


Dengan menggunakan tombol-tombol di Toolbox, silahkan desain form kosong tersebut seperti form di bawah.


Perhatikan nomor-nomornya. Contoh nomor 1 adalah untuk Form, maka isi propertiesnya sesuai petunjuk gambar di bawah ini. Begitu juga untuk nomor 2 hingga 8, ikuti petunjuk propertiesnya.

1. Form


2. UPPERCASE

3. lower case

4. Sentence case

5. Title Case

6. Textbox

7. Run Macro


8. Exit



Setelah form sudah selesai didesain, selanjutnya kita masuk ke bagian source code/scriptnya. Untuk scriptnya pilih View > Code lalu gunakan script berikut:

1. Form
Private Sub UserForm_Activate()
    cmd_run.SetFocus
End Sub


Private Sub UserForm_QueryClose _
(cancel As Integer, CloseMode As Integer)
    
If CloseMode = vbFormControlMenu Then
    cancel = True
End If


End Sub

2. Tombol Radio UPPER CASE
Private Sub opt_upper_Click()
    Label2.Caption = "Text will be changed as " & vbCrLf & "UPPER CASE."
End Sub

3. Tombol Radio lower case
Private Sub opt_lower_Click()
    Label2.Caption = "Text will be changed as " & vbCrLf & "lower case."
End Sub

4. Tombol Radio Sentence case
Private Sub opt_sentence_Click()
    Label2.Caption = "Text will be changed as " & vbCrLf & "Sentence case."
End Sub

5. Tombol Radio Title Case
Private Sub opt_title_Click()
    Label2.Caption = "Text will be changed as " & vbCrLf & "Title Case."
End Sub


7. Tombol Run Macro
Private Sub cmd_run_Click()


On Error GoTo ErrorHandler:


If opt_upper = False And opt_lower = False And opt_sentence = False And opt_title = False Then
    MsgBox "Please choose a case how text should be changed", vbExclamation + vbOKOnly, "Choose Method"
Else
If opt_upper = True Then
    Dim cell_upper As Range
    For Each cell_upper In Selection.Cells
    If cell_upper.HasFormula = False Then
    cell_upper = UCase(cell_upper)
    End If
    Next
Else
If opt_lower = True Then
    Dim cell_lower As Range
    For Each cell_lower In Selection.Cells
    If cell_lower.HasFormula = False Then
    cell_lower = LCase(cell_lower)
    End If
    Next
Else
If opt_sentence = True Then
    For Each cell In Selection.Cells
    s = cell.Value
    Start = True
    For i = 1 To Len(s)
    ch = Mid(s, i, 1)
          Select Case ch
          Case "."
          Start = True
          Case "?"
          Start = True
          Case "a" To "z"
          If Start Then ch = UCase(ch): Start = False
          Case "A" To "Z"
          If Start Then Start = False Else ch = LCase(ch)
          End Select
    Mid(s, i, 1) = ch
    Next
    cell.Value = s
    Next
Else
If opt_title = True Then
    Dim cell_title As Range
    For Each cell_title In Selection.Cells
    If cell_title.HasFormula = False Then
    cell_title = Application.Proper(cell_title)
    End If
    Next
End If
End If
End If
End If
End If


GoTo WrapUp:


ErrorHandler:


    MsgBox "Error Macro. Please contact doddy_151619@yahoo.com for further enhancement.", vbCritical + vbOKOnly, "Error Macro"
    
WrapUp:


    Application.Interactive = True
    Application.ScreenUpdating = True
    
End Sub

8. Tombol Exit
Private Sub cmd_cancel_Click()
    Unload Me
End Sub

Selanjutnya pilih ThisWorkbook pada window Project – VBA Project dan gunakan script berikut pada window sebelah kanan:


Sub Open_Form_Change_Case()
 With Form_Change_Case
     .Show vbModeless
 End With
End Sub

Untuk menjalankan macro ini silahkan balik ke window Microsoft Excel lalu tekan Alt + F8 lalu pilih Open_Form_Change_Case dan klik Run.

Untuk cara penggunaan macro ini, silahkan buka artikel Tunas Gadget: Change Case.

Selamat mencoba!

0 komentar:

Posting Komentar

Backlinks to my website? ping fast  my blog, website, or RSS feed for Free Tips dan Trik Sukses Bisnis OnlineindotradingUAW BMKGNTTfree indotradingcms | indotradingsakpa
Search Engine
www.e-referrer.com