vba – 范围循环提供Jan1900date错误

在过去的四天里,我一直在和栈交换社区的成员一起工作,同时我在VBA上进行了广泛的工作(所以我一直使用vba 4天:))。 我有一个问题,要求我采取的date是呈现和标准格式(date是不同的整个表)。 vba – 在单元格中设置数据显示的date

这工作为我想要完成的。 有一个副作用,如果一个单元格不包含数据,它将默认为01 / Jan / 1900。

代码如下。

timestart = 1 #Column 1 completeBad = 2 #Column 2 timeSet = 24 #Column 24 completeTime = 25 #Column 25 lastrow = 1000 Range(Cells(2, timeSet), Cells(lastrow, completeTime)).NumberFormat = "@" Range(Cells(2, timeSet), Cells(lastrow, timeSet)).NumberFormat = "@" Range(Cells(2, timestart), Cells(lastrow, timestart)).Value = Evaluate("""'"" & INDEX(TEXT(" & Range(Cells(2, timestart), Cells(lastrow, timestart)).Address(0, 0) & ",""dd/mmm/yyyy""),)") Range(Cells(2, completeBad), Cells(lastrow, completeBad)).Value = Evaluate("""'"" & INDEX(TEXT(" & Range(Cells(2, completeBad), Cells(2, completeBad)).Address(0, 0) & ",""dd/mmm/yyyy""),)") For Q = 2 To lastrow Cells(Q, timeSet).Value = Cells(Q, timestart).Value Cells(Q, completeTime).Value = Cells(Q, completeBad).Value next Q 

这里没什么疯狂的。 只是设置单元格的格式/文本,并将数据复制到为文本设置的单元格。

如果timestartcompleteBad单元中没有数据,就会显示01/Jan/1900大量的单元格。

我试图让这个循环遍历每个单元格。 我的代码如下:

 timestart = 1 #Column 1 completeBad = 2 #Column 2 timeSet = 24 #Column 24 completeTime = 25 #Column 25 lastrow = 1000 For Q = 2 To lastrow If Cells(Q, timestart) <> "" Then Range(Cells(Q, timestart), Cells(Q, timestart)).Value = Evaluate("""'"" & INDEX(TEXT(" & Range(Cells(Q, timestart), Cells(Q, timestart)).Address(0, 0) & ",""dd/mmm/yyyy""),)") Cells(Q, timeSet).Value = Cells(Q, timestart).Value End If If Cells(Q, projCom) <> "" Then Range(Cells(Q, completeBad), Cells(Q, completeBad)).Value = Evaluate("""'"" & INDEX(TEXT(" & Range(Cells(Q, completeBad), Cells(Q, completeBad)).Address(0, 0) & ",""dd/mmm/yyyy""),)") Cells(Q, completeTime).Value = Cells(Q, completeBad).Value End If 

我尝试使用range来select一个单元格,至less在代码修改我的数据的range

这返回#VALUE! 在我所有的细胞(我的date列我打电话)。 我已经尝试了多种方法来一次只修改一个单元格,并且一直收到#VALUE! 或者我的代码错误,我不明白为什么。 下面是另一个不适用于我的循环的例子。 有人可以通过调用上面的完整代码或我的代码(下面的代码replace我在for循环中使用的range ,我只是不想重新键入所有内容)来解释我在做什么错了。

 Cells(Q, timestart).Value = Evaluate("""'"" & INDEX(TEXT(" & Cells(Q, timestart).Address(0, 0) & ",""dd/mmm/yyyy""),)") 

所以这会忽略空白:

 timestart = 1 '#Column 1 completeBad = 2 '#Column 2 timeSet = 24 '#Column 24 completeTime = 25 '#Column 25 lastrow = 1000 With Sheets(looping) With .Range(.Cells(2, timestart), .Cells(lastrow, timestart)) .Value = Evaluate("""'"" & INDEX(IF(" & .Address(0, 0) & "<>"""",TEXT(" & .Address(0, 0) & ",""dd/mmm/yyyy""),""""),)") End With With .Range(.Cells(2, completeBad), .Cells(lastrow, completeBad)) .Value = Evaluate("""'"" & INDEX(IF(" & .Address(0, 0) & "<>"""",TEXT(" & .Address(0, 0) & ",""dd/mmm/yyyy""),""""),)") End With End With