删除值小于500的行和舍入值大于500的行VB.net?

问题描述

我在excel中有一列,其中的数字带有小数,如果要删除的数字小于500,则要删除,如果大于500,则要四舍五入以使其不带小数。

我使用下面的代码,该代码不会出现任何错误,但无法完成工作,请检查此代码

    Private Sub DeleteCells_T08()
        Dim xl As New Excel.Application
        Dim wb As Excel.Workbook
        Dim ws As Excel.Worksheet
        Dim Rng As Excel.Range
        Dim i As Integer
        Dim currentcell As Excel.Range
        Dim lRow As Long
        xl.displayAlerts = False
        wb = xl.Workbooks.Open("C:\Patches\Main_Master_VB.xlsm")
        ws = wb.Sheets("Result_T08")
        With ws
            lRow = .Range("B" & .Rows.Count).End(Excel.XlDirection.xlUp).Row
        End With
        Rng = ws.Range("B2","B" & lRow)
        For i = Rng.Rows.count To 1 Step -1
            If Rng.Cells(i).Value < 500 Then
                Rng.Rows(i).EntireRow.Delete
            Else
                For Each currentcell In Rng
                    currentcell.Value = xl.WorksheetFunction.Round(currentcell.Value,0)
                Next currentcell
            End If
        Next i
        xl.displayAlerts = True
    End Sub

非常感谢您的帮助

Moheb Labib

解决方法

我找到了答案

    Private Sub DeleteCells()
        Dim xl As New Excel.Application
        Dim wb As Excel.Workbook
        Dim ws As Excel.Worksheet
        Dim Rng As Excel.Range
        Dim lRow As Integer
        Dim i As Integer
        xl.DisplayAlerts = False
        wb = xl.Workbooks.Open("C:\Patches\Main_Master_VB.xlsm")
        ws = wb.Sheets("Result_T08")
        With ws
            lRow = .Range("B" & .Rows.Count).End(Excel.XlDirection.xlUp).Row
        End With
        Rng = ws.Range("B2","B" & lRow)
        For i = lRow To 1 Step -1
            Dim line As Excel.Range
            line = CType(Rng.Cells(i,1),Excel.Range)
            If CDbl(line.Value) < 500 Then
                line.EntireRow.Delete()
            Else
                line.Value = xl.WorksheetFunction.Round(CDbl(line.Value),0)
            End If
        Next
        xl.DisplayAlerts = True
        wb.Close(SaveChanges:=True)
        xl.Quit()
    End Sub