VBA四舍五入到单元格值

时间:2018-08-31 16:07:49

标签: vba rounding

我正尝试将AD:AD列中的每个填充单元格四舍五入到小数点后四位,并在下一个单元格为空时结束。

我认为类似的方法可以工作,但是在cell.value上会出错。

Sub Round_4()
For Each cell In [AD:AD]
    If cell = "" Then Exit Sub
    cell.Value = WorksheetFunction.Round(cell.Value, 4)
Next cell 

结束子

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您只能使用

直到第一个空单元格工作
// imp is the taxes amount, per example 13%
imp = $('#imp').val();
    if(imp != '' && typeof(imp) != "undefined" ){
        impAmount = (subTotalO * parseFloat(imp));
        $('#impAmount').val(impAmount.toFixed(4));
        total1 = total + impAmount;
    }else{
        $('#impAmount').val(0);
        total1 = total;
    }
$('#totalAfterImp').val( total1 );
calculateAmountDue();

请注意,这是使用Activesheet参考。您可以使用With语句包装父表。

答案 1 :(得分:0)

您可以这样做:

Dim myCell As Range
Dim myRange As Range

Set myRange = Excel.Application.ThisWorkbook.Worksheets(worksheetNameGoesHereInDoubleQuotes).Range("AD:AD")

For Each myCell In myRange
    If Not IsEmpty(myCell) Then
        myCell.Value = Application.WorksheetFunction.Round(CDbl(myCell.Value), 4)
    'me being lazy with my range assignment
    ElseIf IsEmpty(myCell) Then
        Exit For
    End If
Next
相关问题