gpt4 book ai didi

VBA Excel 这个循环的一半有效

转载 作者:行者123 更新时间:2023-12-04 20:54:15 25 4
gpt4 key购买 nike

所以当我运行这个宏时,A 列将被 P 填充。的和 C 列将填充 7的,正如我所期望的那样。

但是,我还需要用 0 填充 H 和 I 列的和 1的分别。

但是在工作簿上,每个工作表的 H 和 I 列都未填写。只有 H2 和 I2 将被填充。在某些工作表中,H2 和 I2 将分别是日期 (1/0/1900)、(1/1/1900)。在其他工作表中,H2 和 I2 将分别只有 0 和 1。

为什么会这样?为什么有些列日期不正确而其他列是数字?如何解决这个问题?

Dim i As Long
Dim LastRow As Long

For i = 1 To Worksheets.count
With Sheets(i)
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("A2:A" & LastRow).Value2 = "P"
LastRow = .Cells(Rows.Count, "C").End(xlUp).Row
.Range("C2:C" & LastRow).Value2 = "7"
LastRow = .Cells(Rows.Count, "H").End(xlUp).Row
.Range("H2:H" & LastRow).Value2 = "0"
LastRow = .Cells(Rows.Count, "I").End(xlUp).Row
.Range("I2:I" & LastRow).Value2 = "1"
End With
Next i

最佳答案

我猜你已经运行了两次代码,你看不到它再改变了。要在每次运行时查看它添加更多值:

Sub Test()
Dim i As Long
Dim LastRow As Long

For i = 1 To Worksheets.Count
With Sheets(i)
.Columns("A:XFD").NumberFormat = "General"
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
.Range("A2:A" & LastRow).Value2 = "P"
LastRow = .Cells(Rows.Count, "C").End(xlUp).Row + 1
.Range("C2:C" & LastRow).Value2 = "7"
LastRow = .Cells(Rows.Count, "H").End(xlUp).Row + 1
.Range("H2:H" & LastRow).Value2 = "0"
LastRow = .Cells(Rows.Count, "I").End(xlUp).Row + 1
.Range("I2:I" & LastRow).Value2 = "1"
End With
Next i
End Sub
(1/0/1900) 的问题和 (1/1/1900)是这些单元格被格式化为日期。和 0被翻译成 1/0/190011/1/1900 .为了解决这个问题,在上面的代码中,单元格被格式化为 General用这条线:
.Columns("A:XFD").NumberFormat = "General"

关于VBA Excel 这个循环的一半有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51710475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com