我创建了一个新工作表,但我无法在循环中设置新工作表中的单元格值。
Dim dueWs As Worksheet
Sub CreateDue()
Dim i, intsal As Integer
Dim firstDue As String
Set dueWs = Sheets.Add
dueWs.Name = "Due date Table"
firstDue = Range("B11").Value
instal = Range("B7").Value
With dueWs
.Range("A1").Value = "Instalment"
.Range("B1").Value = "Due date"
For i = 1 To instal
.Range("A" & i + 1).Value = i
.Range("B" & i + 1).Value = firstDue
firstDue = DateAdd("m", 1, firstDue)
Next i
End With
End Sub
2条答案
按热度按时间nue99wik1#
创建顺序列表
Option Explicit
,它会强制你声明所有变量。它会警告你关于 "intsal" 的错别字。As
子句,否则将声明为As Variant
,例如:Dim i As Long, Instal As Long, firstDue As Date
.Set ws = wb.Sheets(...)
.Set rg = ws.Range(...)
..Range("B" & i + 1).Value = DateAdd("m", i - 1, firstDue)
.mm5n2pyu2#
在我的评论中说了什么,你也可以避免循环: