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)。現在以儲存格C2一的value取代儲存格A1:A10內C1日期的yyyy,然後以儲存格B2的value取代儲存格A1:A10內B1日期的yyyy;但保留每一個儲存格內的d/m。

Question

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

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

Solution 1

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

  1. 首先,我們需要在VBA中建立一個新的子程序。在VBA編輯器中,點擊插入,然後選擇模塊。在新的模塊中,我們可以開始編寫我們的子程序。
Sub UpdateDates()
  1. 接著,我們需要定義我們將要使用的變數。我們需要一個範圍來代表A1:A10的儲存格,以及兩個變數來存儲B1和B2的年份。
Dim rng As Range
Dim year1 As Integer
Dim year2 As Integer

Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:A10")
year1 = Year(ThisWorkbook.Sheets("Sheet1").Range("B1").Value)
year2 = Year(ThisWorkbook.Sheets("Sheet1").Range("B2").Value)
  1. 現在,我們可以遍歷範圍中的每一個儲存格,並更新日期。我們將使用VBA的Replace函數來替換年份。
Dim cell As Range
For Each cell In rng
    cell.Value = Replace(cell.Value, year1, year2)
Next cell
  1. 最後,我們需要結束我們的子程序。
End Sub

這個子程序將遍歷A1:A10的每一個儲存格,並將日期中的年份從B1的年份更改為B2的年份。請注意,這個子程序假設所有的日期都是以"/"分隔的,並且年份是在日期的最後。如果你的日期格式不同,你可能需要修改這個子程序。

This problem has been solved

Solution 2

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

  1. 首先,我們需要開啟VBA編輯器。在Excel中,你可以按下Alt + F11來開啟VBA編輯器。

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

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

Sub ReplaceYear()
    Dim rng As Range
    Dim cell As Range
    Dim oldYear1 As String
    Dim oldYear2 As String
    Dim newYear1 As String
    Dim newYear2 As String

    ' 取得舊的和新的年份
    oldYear1 = Year(Range("B1").Value)
    newYear1 = Year(Range("C1").Value)
    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) = oldYear1 Then
            cell.Value = DateSerial(newYear1, 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. 你可以按下F5或者選擇執行 > 執行子程序來執行這個VBA程式碼。

這個VBA程式碼會遍歷A1:A10的每一個儲存格,並檢查儲存格的年份是否等於B1或B2的年份。如果是,則將儲存格的年份替換為C1或C2的年份,但保留原來的月份和日期。

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

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

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

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.