gpt4 book ai didi

vba - 如何将for next循环中的日期放在excel的单元格中

转载 作者:行者123 更新时间:2023-12-04 21:38:12 24 4
gpt4 key购买 nike

我目前正在尝试在excel中做一个简单的for next循环。我正在处理日期,所以我声明了开始日期和结束日期。我只需要在不同的单元格中显示这两个日期中包含的日期。

我只使用整数得到了这个,但我还没有得到日期。

我用 MsgBox 进行了测试,它可以工作(按 enter 键)直到到达最后一个日期(结束日期),但我无法将它放在一系列单元格中,可能解决方案很简单,但我花了很多时间在这个...
请帮我

这在我的代码中

Sub looping()

Dim fecha_ini As Date
Dim fecha_fin As Date
Dim conteo As Date
Dim rango_inicio As Range


fecha_ini = #1/1/2015#
fecha_fin = #1/15/2015#

Set rango_inicio = Sheets("Sheet1").Range("a1")

For conteo = fecha_ini To fecha_fin

Range("A" & 1).Value = conteo

Next conteo

最佳答案

试试这样的..

无需将 Range 设置为 A1,只需使用带有变量的 .Cells 循环而不是您当前使用的数字来设置值。
即, .Cells(row, col) 而不是 .Range("A"& 1)

sub Looping()
Dim lRow As Long
Dim fecha_str As String
Dim fecha_date As Date

For lRow = 1 to 15

fecha_str = "1/" & lRow & "/2015"
fecha_date = CDate(fecha_str)
Sheets("Sheet1").Cells(lRow, "A").Value = fecha_date

Next lRow
End Sub

关于vba - 如何将for next循环中的日期放在excel的单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27871583/

24 4 0