Excel VBA – 在列的使用行上的地面
我正在尝试将使用的列的范围转换为使用楼层的小时数据。
作为Excel中的函数,我有=FLOOR(A2, "1:00")
所以2016-07-01 07:59:59.0000000会变成01-07 / 2016 7:00
我想在VBA中为第一行是一个头的列A做这个。 我想我需要转换为dateTime后,但还没有想过这个(应该不难)。
我试过这个:
.Range(Cells(2, 1), Cells(Rows.Count, 1).End(xlUp)) = _ Application.WorksheetFunction.Floor(Cells(2, 1), Cells(Rows.Count, 1).End(xlUp), "1:00")
但是得到错误的参数数量错误。
这个:
.Range("A:A") = Application.WorksheetFunction.Floor("A:A", "1:00")
给出types不匹配。
不知道如何继续。
VBA中的WorksheetFunction.Floor
方法与工作表=Floor
函数有点不同:
WorksheetFunction.Floor(Arg1, Arg2)
Arg1
和Arg2
需要是double
types的。
所以你将需要使用1/24
而不是"1:00"
(这是相同的,因为1小时是1/24的一天)和值.Cells(i, "A").Value
而不是一个单元格参考名称"A:A"
。 你也需要一个循环来实现整个列中每个使用过的单元格。
Option Explicit 'First line at your module ensures you declare any variables Public Sub FloorFormat() Dim lastRow As Long With Worksheets("Sheet1") 'Your sheet name here lastRow = .Range("A" & .Rows.Count).End(xlUp).Row 'find last used row Dim i As Long For i = 1 To lastRow 'do the following for all used rows in column A .Cells(i, "A").Value = Application.WorksheetFunction.Floor(.Cells(i, "A").Value, 1 / 24) Next i End With End Sub