Szkolenie
Certyfikacja
Microsoft Office Specialist: Excel (Office 2016) - Certifications
Microsoft Office Specialist (MOS) 認定資格を取得して、Excel 2016 を最大限に活用するために必要なスキルを身に付けていることを証明しましょう。
Ta przeglądarka nie jest już obsługiwana.
Przejdź na przeglądarkę Microsoft Edge, aby korzystać z najnowszych funkcji, aktualizacji zabezpieczeń i pomocy technicznej.
Mimo że formuły dat programu Microsoft Excel mogą używać tylko dat wprowadzonych między 1 stycznia 1900 r. a 31 grudnia 9999 r., możesz użyć niestandardowej funkcji Microsoft Visual Basic for Applications, aby obliczyć wiek (w latach) osoby lub czegoś, co zostało utworzone przed 1 stycznia 1900 r.
Firma Microsoft podaje przykłady programowania tylko dla celów ilustracyjnych, nie udzielając żadnej rękojmi, wyrażonej wprost ani dorozumianej, w tym także, ale nie tylko, dorozumianej rękojmi co do przydatności handlowej lub do określonych celów. W tym artykule zakłada się, że czytelnik zna demonstrowany język programowania oraz narzędzia używane do tworzenia i debugowania procedur. Wykwalifikowani pracownicy pomocy technicznej firmy Microsoft mogą pomóc w wyjaśnieniu, jak działa określona procedura, ale nie będą modyfikować tych przykładów ani dodawać żadnych funkcji i konstruować nowych procedur w celu dostosowania ich do określonych potrzeb użytkownika.
Program Excel wprowadza daty przed 1 stycznia 1900 r. jako tekst. Ta funkcja działa dla dat wprowadzonych jako tekst rozpoczynający się od 1/1/0001, daty normalne i może obsługiwać daty, gdy data początkowa jest przed 1900 i data zakończenia jest po 1900 roku. Aby użyć makra, wykonaj następujące kroki:
Uruchom program Excel. Wyświetl arkusz, w którym chcesz użyć funkcji.
Naciśnij klawisze ALT+F11, aby przełączyć się do Edytora Visual Basic.
W menu Wstaw kliknij polecenie Moduł.
W module wpisz następujący kod:
' 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
Zapisz plik.
Wpisz następujące dane:
A1 01/01/1887
A2 02/02/1945
W komórce A3 wprowadź następującą formułę:
=AgeFunc(startdate,enddate)
Data początkowa jest odwołaniem do pierwszej daty (A1), a data zakończenia jest odwołaniem do drugiej daty (A2).
Wynik powinien wynosić 58.
Uwaga
Sprawdź wszystkie daty przed 1/1/1900 pod kątem ważności. Daty wprowadzone jako tekst nie są sprawdzane przez program Excel.
Aby uzyskać więcej informacji na temat korzystania z przykładowego kodu w tym artykule, zobacz How to run sample code from Knowledge Base Articles in Office 2010 (Jak uruchomić przykładowy kod z artykułów bazy wiedzy w pakiecie Office 2010).
Szkolenie
Certyfikacja
Microsoft Office Specialist: Excel (Office 2016) - Certifications
Microsoft Office Specialist (MOS) 認定資格を取得して、Excel 2016 を最大限に活用するために必要なスキルを身に付けていることを証明しましょう。