我正在尝试将格式化的日期值复制到另一个包含数据的单元格,因此无法格式化正在接收格式化日期的单元格。这是我尝试使用的代码片段
Sub test_sub()
Dim wsDash As Worksheet
Set wsDash = ActiveSheet
Dim dateStartCell As Range
Dim dateEndCell As Range
Dim allDates As Collection
Dim currentDateSter As Variant
Dim currentDate As Date
Dim II As Integer
Dim TargetDate As Variant
II = 60
Set dateStartCell = wsDash.Cells(7, 2)
Set dateEndCell = wsDash.Cells(7, 6)
Set allDates = GetDatesRange(dateStartCell.Value, dateEndCell.Value)
For Each currentDateSter In allDates
currentDate = CDate(currentDateSter)
wsDash.Cells(II, 1) = currentDate
wsDash.Cells(II, 1).NumberFormat = "yyyymmdd"
wsDash.Cells(II, 2) = "[""VNR""," & """" & wsDash.Cells(II, 1).Value & """" & ",""INTRO""]"
II = II + 1
Next currentDateSter
End Sub
问题在于,仅查看单元格时,格式没有被遵循。有没有一种方法可以使单元格仅使用格式化的值并删除下划线日期?我尝试创建一种自定义格式,每边带有特殊字符以说明额外的数据,然后在“ YYYYMMDD”中设置日期格式,但这似乎不起作用。
可以早些做些什么来将日期原始设置为所需格式吗?我使用功能GetDatesRange()
获取日期范围Function GetDatesRange(dateStart As Date, dateEnd As Date) As Collection
Dim dates As New Collection
Dim currentDate As Date
currentDate = dateStart
Do While currentDate <= dateEnd
dates.Add currentDate
currentDate = DateAdd("d", 1, currentDate)
Loop
Set GetDatesRange = dates
End Function
也尝试复制值不起作用,因为这只会以整数值而非数字格式显示日期。因此,总的来说需要发生的是日期要进入的单元格的格式为yyyymmdd,并且其两侧都有数据。