Knowee
Questions
Features
Study Tools

VBA寫法:在Excel儲存格B1的value為既有「日期」的年份,儲存格C1的value為儲存格B1的年份+1年。儲存格B2的value為另一新的年份,而儲存格C2的value為儲存格B2的年份+1年。在Excel儲存格A1:A10填寫了既有「日期」(d/m/yyyy)。現在以儲存格B2一的value取代儲存格A1:A10內B1日期的yyyy,及以儲存格C2的value取代儲存格A1:A10內C1日期的yyyy;但保留每一個儲存格內的d/m。

Question

VBA寫法:在Excel儲存格B1的value為既有「日期」的年份,儲存格C1的value為儲存格B1的年份+1年。儲存格B2的value為另一新的年份,而儲存格C2的value為儲存格B2的年份+1年。在Excel儲存格A1:A10填寫了既有「日期」(d/m/yyyy)。現在以儲存格B2一的value取代儲存格A1:A10內B1日期的yyyy,及以儲存格C2的value取代儲存格A1:A10內C1日期的yyyy;但保留每一個儲存格內的d/m。

🧐 Not the exact question you are looking for?Go ask a question

Solution

以下是使用VBA來完成這個任務的步驟:

  1. 首先,我們需要在Excel中開啟VBA編輯器。你可以使用快捷鍵Alt + F11來開啟它。

  2. 在VBA編輯器中,選擇插入 -> 模組,來新增一個新的模組。

  3. 在新的模組中,我們可以開始編寫我們的程式碼。以下是一個範例程式碼:

Sub UpdateYear()
    Dim rng As Range
    Dim cell As Range
    Dim oldYear As String
    Dim newYear As String

    ' 取得B1和C1的年份
    oldYear = Year(Range("B1").Value)
    newYear = Year(Range("C1").Value)

    ' 取得B2和C2的年份
    Dim oldYear2 As String
    Dim newYear2 As String
    oldYear2 = Year(Range("B2").Value)
    newYear2 = Year(Range("C2").Value)

    ' 更新A1:A10的年份
    Set rng = Range("A1:A10")
    For Each cell In rng
        If Year(cell.Value) = oldYear Then
            cell.Value = DateSerial(newYear, Month(cell.Value), Day(cell.Value))
        ElseIf Year(cell.Value) = oldYear2 Then
            cell.Value = DateSerial(newYear2, Month(cell.Value), Day(cell.Value))
        End If
    Next cell
End Sub
  1. 這個程式碼會先取得B1和C1的年份,然後取得B2和C2的年份。然後,它會遍歷A1:A10的每一個儲存格,如果儲存格的年份等於B1的年份,則將其替換為C1的年份;如果儲存格的年份等於B2的年份,則將其替換為C2的年份。

  2. 最後,我們可以運行這個程式碼來更新我們的儲存格。你可以使用快捷鍵F5來運行程式碼,或者在VBA編輯器的選單中選擇運行 -> 運行子程序。

This problem has been solved

Similar Questions

Why is the DATE function in MS Excel used?ACalculate the number of days between two dates.BConvert a serial number representing a date into a formatted text string.CDetermine the current date and time.DCreate a date value based on individual year, month, and day values.

Convert the rate stored in the strRate variable to its decimal equivalent by converting to a number using the Val function then dividing by 100. Then assign the result to the cell B1 in the January worksheet.Concatenate the string literal constant “Prepared By ” to the name stored in the strName variable and assign the result to the cell A19 in the January worksheet.

Give clear explanations for the following errors as they may be encountered in VBa) Syntax Error………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………b) Run-time errors………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………c) Logic Errors

On the Year1 sheet, in cell B8, enter a formula to display the value of cell B7 from the Salaries sheet

4. In Open Office Calc, ___ function displays current date in the selected cell.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.