Cells(2, currentMonth + 16) = Cells(2, currentMonth + 13) / mth2 / Cells(2, currentMonth + 14) / mth1這句代碼怎麼變成結果不要寫成值,而帶公式寫入
时间: 2024-04-12 17:28:05 浏览: 8
要将这段代码修改为带有公式的写入,您可以使用 `Formula` 属性来设置单元格中的公式。以下是修改后的代码示例:
```vb
Cells(2, currentMonth + 16).Formula = "=Cells(2, currentMonth + 13) / mth2 / Cells(2, currentMonth + 14) / mth1"
```
这样,代码将会将公式作为字符串写入单元格,而不是将结果作为值进行写入。请确保 `currentMonth`、`mth1` 和 `mth2` 是正确声明和赋值的变量。
相关问题
Cells(2, currentMonth + 16) = (last / daysInMonth2) / (curr / daysInMonth1) 這句代碼怎麼變成結果不要寫成值,而帶公式寫入
要将公式写入单元格而不是写入值,可以使用 `.Formula` 属性来设置单元格的公式。在你的代码中,可以将 `Cells(2, currentMonth + 16) = (last / daysInMonth2) / (curr / daysInMonth1)` 修改为以下代码:
```vb
Cells(2, currentMonth + 16).Formula = "=(" & last & "/" & daysInMonth2 & ")/(" & curr & "/" & daysInMonth1 & ")"
```
这样,公式将会以字符串形式写入单元格,并在 Excel 中正确计算结果。
lastrow = ActiveSheet.Cells(Rows.Count, "H").End(xlUp).Row For i = 1 To lastrow - 2 Cells(2, currentMonth + 15).Offset(i).FillDown Next i 這一段循環要可以不用循環實現嗎?
是的,您可以使用一行代码来代替该循环。以下是简化后的代码:
```vba
Range(Cells(2, currentMonth + 15), Cells(lastrow - 2, currentMonth + 15)).FillDown
```
这行代码将在指定范围内填充下来,而无需使用循环。请确保将 `currentMonth` 替换为您要使用的实际值。