Sub ProcessAccountingReport() ' 1. 宣告變數 (好的習慣) Dim ws As Worksheet Dim LastRow As Long Dim i As Long Set ws = ThisWorkbook.Sheets("RawData") ' 2. 找出最後一列 (關鍵!因為每個月的資料筆數不同) LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ' 3. 新增標題欄位 ws.Range("E1").Value = "營業稅(5%)" ws.Range("F1").Value = "含稅總額" ' 4. 迴圈計算 (從第2列算到最後一列) For i = 2 To LastRow ' 計算營業稅 (D欄 * 0.05) ws.Cells(i, 5).Value = ws.Cells(i, 4).Value * 0.05 ' 計算總額 (D欄 + E欄) ws.Cells(i, 6).Value = ws.Cells(i, 4).Value + ws.Cells(i, 5).Value Next i Range("E1:F1").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 12611584 .TintAndShade = 0 .PatternTintAndShade = 0 End With With Selection.Font .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 End With Selection.Font.Bold = True MsgBox "計算完成!" End Sub