Showing posts with label VBA. Show all posts
Showing posts with label VBA. Show all posts

Mengambil Nama Sheet

April 27, 2019 Add Comment

Setiap membuat laporan, saya selalu mencantumkan nama sheet yang sedang terpakai sebagai sebuah judul laporan. Karena saya menamai suatu sheet biasanya mewakili isinya. Misalnya isinya adalah laporan bulan Januari, maka saya biasa menamainya dengan Laporan Januari. 

Bisa saja saya mengetiknya satu-satu, tetapi jika dalam satu worksheet saya mempunyai 30 sheet tentunya sangat menjengkelkan jika harus mengetiknya satu persatu. Lalu saya berusaha membuat fungsi sendiri untuk membuat pekerjaan tersebut menjadi lebih enteng.

Kita akan menggunakan Visual Basic for Application (VBA) untuk membuat fungsi tersebut. 

Langkah-langkahnya sebagai berikut:

1.  Buka Microsoft Excel anda.
2.  Tekan Alt + F11 untuk membuka Visual Basic Editor, atau anda bisa juga melalui menu :
Tools -> Macro -> Visual Basic Editor.
3. Di Visual Basic Editor, click menu Insert -> Module.
4. Lalu ketikkan script seperti dibawah ini.
Function nama_sheet() 
nama_sheet = ActiveSheet.Name 
End Function
5. Tutup Visual Basic Editor dengan menekan tombol Alt + Q.
6. Lalu sorot cell yang ingin menampung nama sheet.
7. Ketikkan =nama_sheet().
8. Nama sheet anda akan terpampang di cell tersebut.

Mudah sekali kan ? Sekarang anda ngga usah mengetik nama sheet anda satu persatu secara manual. Dan jika anda merubah nama sheet anda, maka judul laporan anda akan berubah secara otomatis mengikuti nama sheet tersebut.

The MsgBox Function

February 24, 2017 Add Comment



You’re probably already familiar with the VBA MsgBox function — I use it quite a bit in the examples throughout this book. 

The MsgBox function, which accepts the arguments shown in Table 15-1, is handy for displaying information and getting simple user input. It’s able to get user input because it’s a function. 

A function, as you recall, returns a value. In the case of the Msgbox function, it uses a dialog box to get the value that it returns. Keep reading to see exactly how it works.


Here’s a simplified version of the syntax for the MsgBox function:
MsgBox(prompt[, buttons][, title])
You can use the MsgBox function in two ways:
  1. To simply show a message to the userIn this case, you don’t care about the result returned by the function.
  2. To get a response from the user. In this case, you do care about the result returned by the function. The result depends on the button that the user clicks.
If you use the MsgBox function by itself, don’t include parentheses around the arguments. The following example simply displays a message and does not return a result. When the message is displayed, the code stops until the user clicks OK.
Sub MsgBoxDemo()
MsgBox “Click OK to begin printing.”
Sheets(“Results”).PrintOut
End Sub

Figure 15-1 shows how this message box looks.

You can also use the MsgBox function result without using a variable, as the following example demonstrates:
Sub GetAnswer2()
If MsgBox(“Continue?”, vbYesNo) = vbYes Then
         ‘ ...[code if Yes is clicked]...
Else
          ‘ ...[code if Yes is not clicked]...
End If

End Sub