Meskipun tanggal Microsoft Excel Formula hanya dapat menggunakan tanggal masuk antara 1/1/1900 dan 12/31/9999, Anda dapat menggunakan Microsoft Visual Basic kustom untuk aplikasi fungsi untuk menghitung usia (tahun) dari seseorang atau sesuatu yang pertama dibuat sebelum tanggal 1 Januari 1900.
Menggunakan makro untuk menghitung usia
Microsoft menyediakan contoh pemrograman hanya sebagai ilustrasi, tanpa jaminan apa pun baik tersurat maupun tersirat. Termasuk, namun tidak terbatas pada, jaminan tersirat mengenai kelayakan untuk diperdagangkan atau kesesuaian untuk keperluan tertentu. Artikel ini mengasumsikan bahwa Anda telah terbiasa dengan bahasa pemrograman yang ditunjukkan dan dengan alat yang digunakan untuk membuat dan mendebug prosedur. Teknisi dukungan Microsoft dapat membantu menjelaskan fungsionalitas prosedur tertentu, namun mereka tidak akan memodifikasi contoh untuk memberikan fungsionalitas tambahan atau menyusun prosedur untuk memenuhi persyaratan khusus Anda.
Excel memasuki tanggal sebelum ke 1/1/1900 sebagai teks. Fungsi ini bekerja untuk tanggal dimasukkan sebagai teks awal dengan 1/1/0001, tanggal normal, dan dapat menangani tanggal ketika tanggal mulai sebelum 1900 dan mengakhiri tanggal setelah tahun 1900. Untuk menggunakan makro, ikuti langkah berikut:
- Mulai Excel. Lihat lembar kerja yang Anda ingin menggunakan fungsi.
- Tekan ALT + F11 untuk beralih ke Editor Visual Basic.
- Pada Masukkan menu, klik Modul.
- Ketik kode berikut dalam modul:
' This is the initial function. It takes in a start date and an end date.
Public Function AgeFunc(stdate As Variant, endate As Variant)
' Dim our variables.
Dim stvar As String
Dim stmon As String
Dim stday As String
Dim styr As String
Dim endvar As String
Dim endmon As String
Dim endday As String
Dim endyr As String
Dim stmonf As Integer
Dim stdayf As Integer
Dim styrf As Integer
Dim endmonf As Integer
Dim enddayf As Integer
Dim endyrf As Integer
Dim years As Integer
' This variable will be used to modify string length.
Dim fx As Integer
fx = 0
' Calls custom function sfunc which runs the Search worksheet function
' and returns the results.
' Searches for the first "/" sign in the start date.
stvar = sfunc("/", stdate)
' Parse the month and day from the start date.
stmon = Left(stdate, sfunc("/", stdate) - 1)
stday = Mid(stdate, stvar + 1, sfunc("/", stdate, sfunc("/", stdate) + 1) - stvar - 1)
' Check the length of the day and month strings and modify the string
' length variable.
If Len(stday) = 1 Then fx = fx + 1
If Len(stmon) = 2 Then fx = fx + 1
' Parse the year, using information from the string length variable.
styr = Right(stdate, Len(stdate) - (sfunc("/", stdate) + 1) - stvar + fx)
' Change the text values we obtained to integers for calculation
' purposes.
stmonf = CInt(stmon)
stdayf = CInt(stday)
styrf = CInt(styr)
' Check for valid date entries.
If stmonf < 1 Or stmonf > 12 Or stdayf < 1 Or stdayf > 31 Or styrf < 1 Then
AgeFunc = "Invalid Date"
Exit Function
End If
' Reset the string length variable.
fx = 0
' Parse the first "/" sign from the end date.
endvar = sfunc("/", endate)
' Parse the month and day from the end date.
endmon = Left(endate, sfunc("/", endate) - 1)
endday = Mid(endate, endvar + 1, sfunc("/", endate, sfunc("/", endate) + 1) - endvar - 1)
' Check the length of the day and month strings and modify the string
' length variable.
If Len(endday) = 1 Then fx = fx + 1
If Len(endmon) = 2 Then fx = fx + 1
' Parse the year, using information from the string length variable.
endyr = Right(endate, Len(endate) - (sfunc("/", endate) + 1) - endvar + fx)
' Change the text values we obtained to integers for calculation
' purposes.
endmonf = CInt(endmon)
enddayf = CInt(endday)
endyrf = CInt(endyr)
' Check for valid date entries.
If endmonf < 1 Or endmonf > 12 Or enddayf < 1 Or enddayf > 31 Or endyrf < 1 Then
AgeFunc = "Invalid Date"
Exit Function
End If
' Determine the initial number of years by subtracting the first and
' second year.
years = endyrf - styrf
' Look at the month and day values to make sure a full year has passed.
If stmonf > endmonf Then
years = years - 1
End If
If stmonf = endmonf And stdayf > enddayf Then
years = years - 1
End If
' Make sure that we are not returning a negative number and, if not,
' return the years.
If years < 0 Then
AgeFunc = "Invalid Date"
Else
AgeFunc = years
End If
End Function
' This is a second function that the first will call.
' It runs the Search worksheet function with arguments passed from AgeFunc.
' It is used so that the code is easier to read.
Public Function sfunc(x As Variant, y As Variant, Optional z As Variant)
sfunc = Application.WorksheetFunction.Search(x, y, z)
End Function
- Simpan berkas tersebut.
- Tipe data sebagai berikut:
A1 01/01/1887
A2 02/02/1945
Dalam sel A3, masukkan rumus berikut= AgeFunc)startdate,enddate)
di mana startdate sel merujuk Anda kencan pertama (A1) dan enddate adalah sel referensi kencan kedua Anda (A2).
Hasilnya harus menjadi 58.
CATATAN: Memeriksa semua tanggal sebelum 1/1/1900 untuk validitas. Tanggal dimasukkan sebagai teks tidak Diperiksa oleh Excel.
Untuk informasi lebih lanjut tentang cara menggunakan kode contoh dalam artikel ini, klik
nomor artikel di bawah ini untuk melihat artikel di dalam Basis Pengetahuan Microsoft:
212536
(http://support.microsoft.com/kb/212536/EN-US/
)
Bagaimana menjalankan kode contoh dari artikel Basis Pengetahuan
ID Artikel: 245104 - Kajian Terakhir: 20 September 2011 - Revisi: 2.0
Berlaku bagi:
- Microsoft Office Excel 2007
- Microsoft Excel 2002 Standard Edition
- Microsoft Excel 2000 Standard Edition
| kbhowtomaster kbmt KB245104 KbMtid |
Penerjemahan MesinPENTING: Artikel ini diterjemahkan menggunakan perangkat lunak mesin penerjemah Microsoft dan bukan oleh seorang penerjemah. Microsoft menawarkan artikel yang diterjemahkan oleh seorang penerjemah maupun artikel yang diterjemahkan menggunakan mesin sehingga Anda akan memiliki akses ke seluruh artikel baru yang diterbitkan di Pangkalan Pengetahuan (Knowledge Base) dalam bahasa yang Anda gunakan. Namun, artikel yang diterjemahkan menggunakan mesin tidak selalu sempurna. Artikel tersebut mungkin memiliki kesalahan kosa kata, sintaksis, atau tata bahasa, hampir sama seperti orang asing yang berbicara dalam bahasa Anda. Microsoft tidak bertanggung jawab terhadap akurasi, kesalahan atau kerusakan yang disebabkan karena kesalahan penerjemahan konten atau penggunaannya oleh para pelanggan. Microsoft juga sering memperbarui perangkat lunak mesin penerjemah.
Klik disini untuk melihat versi Inggris dari artikel ini:
245104
(http://support.microsoft.com/kb/245104/en-us/
)