舍入不会出现在单元格中

时间:2016-09-19 15:02:27

标签: excel vba excel-vba

我需要每周平均一张已偶尔输入数据的工作表,所以我想出了下面的代码。它的工作原理除了舍入外。变量" avehold"

Sub Sort_em()
Dim dhold As Date

Dim src As Worksheet
Dim dst As Worksheet
Dim rowCounter As Long
Dim ztorage() As Variant
Dim holdDate As Date
Dim startDate As Date
Dim endDate As Date
Dim rng As Range
Dim lst As Long
Dim holdrack As String
Dim avehold As Single
Dim ztorRow As Integer
Dim ztorCol As Integer
Dim endSrch As String
Dim startSrch As String
Set src = Sheets("Readings")
Set dst = Sheets("Weekly_Avg")


rowCounter = 2

holdDate = src.Cells(rowCounter, 1)
startDate = src.Cells(rowCounter, 1).Text
startDate = Format(startDate, "d-mmm-yy")
holdrack = src.Cells(rowCounter, 1).Text
endDate = src.Cells(rowCounter, 1).Text
endSrch = Format(endDate, "d-mmm-yy")
Do While src.Cells(rowCounter, 1) <> ""
    endDate = src.Cells(rowCounter, 1).Text
    lst = Get_Last(1, dst.Cells) + 1
    Do Until WeekdayName(Weekday(endDate)) = "Wednesday" Or DateDiff("d", startDate, endDate) > 7
        endDate = DateAdd("d", 1, endDate)
    Loop
    endSrch = Format(endDate, "d-mmm-yy")
    Set rng = src.UsedRange.Find(endSrch, LookIn:=xlValues, LookAt:=xlWhole)
    Do Until Not rng Is Nothing
        endDate = DateAdd("d", -1, endDate)
        endSrch = Format(endDate, "d-mmm-yy")
        Set rng = src.UsedRange.Find(endSrch, LookIn:=xlValues, LookAt:=xlWhole)
    Loop


    ztorage = src.Range(Cells(rowCounter, 1).Address, Cells(rng.Row, 12).Address)
    dst.Cells(lst, 1) = ztorage(1, 1)
    dst.Cells(lst, 2) = ztorage(1, 2)
    For ztorCol = 3 To UBound(ztorage, 2)
        For ztorRow = 1 To UBound(ztorage, 1)
            avehold = avehold + ztorage(ztorRow, ztorCol)
        Next ztorRow

avehold包含数字,正确舍入到1位小数(73.8)

       avehold = Round(avehold / (ztorRow - 1), 1)

但是当分配给单元格时,将忽略舍入。 (73.8000030517578)

        dst.Cells(lst, ztorCol) = avehold

        avehold = 0
    Next ztorCol
     rowCounter = rowCounter + ztorRow - 1
    If src.Cells(rowCounter, 1) = "" Then Exit Do
    startDate = src.Cells(rowCounter, 1).Text
    startSrch = Format(startDate, "d-mmm-yy")

Loop

End Sub

我做错了什么?

1 个答案:

答案 0 :(得分:0)

好吧,我想通了,avehold应该被宣布为双人,而不是单身。