在每一个细胞在一列运行方程(Run equation in every cell in a colu

2019-10-17 11:54发布

我试图运行在我的电子表格中的公式,将经过柱“I”,并删除不具有从现在起的90天内到期日的每一行......换句话说,我正在尝试我的电子表格格式化为只要给我,在未来90天内到期的所有内容的列表。 这里我把星行就是我有困难插入公式。 我不知道如何插入公式,但如果它是在细胞本身运行它看起来像这样= IF(AND(I11-900),1,0)= 1。 我会怎么改变Q11是,这样的方式在运行时公式将适用于在我列的每一个细胞,而不仅仅是我11

Sub DeleteNow()


Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long


With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
End With

With Sheets("Copy")
    .Select
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    .DisplayPageBreaks = False
    Firstrow = .UsedRange.Cells(1).Row
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    For Lrow = Lastrow To Firstrow Step -1
        With .Cells(Lrow, "I")

            If Not IsError(.Value) Then

                If ******************** Then .EntireRow.Delete

            End If

        End With

    Next Lrow


End With

ActiveWindow.View = ViewMode
With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
End With

End Sub

Answer 1:

Sub deleteRowsWithDateNotIn90Days()
Dim lastRow As Integer
Dim firstRow As Integer
Dim ctr As Integer

Dim currentCell As Range
Dim valueOfIColumn
Dim isWithin90Days As Boolean

lastRow = 17
firstRow = 1

Application.ScreenUpdating = False
With Sheets("Copy")
    For ctr = lastRow To firstRow Step -1
        Set currentCell = .Cells(ctr, 9)
        valueOfIColumn = currentCell.Value
        isWithin90Days = valueOfIColumn >= Date And valueOfIColumn <= (Date + 90)

        If Not isWithin90Days Then
            Debug.Print "deleting row of cell " + currentCell.Address
            currentCell.EntireRow.Delete
        End If
    Next
End With
Application.ScreenUpdating = True
End Sub

编辑:使用此作为上手的基础。
您可以通过删除宏记录产生不必要的代码。



Answer 2:

我没有XL在我身上的那一刻,所以可能有一些语法错误,但是这应该是你更容易了很多,很容易理解和更新。 请注意,我刚刚建立了代码的核心部分,我离开了你所有的Application层次的东西出来。

With Sheets("Copy")

    '.Select -> no need to select anything, you can work right with the object
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    .DisplayPageBreaks = False

    Dim myCol as Integer
    myCol = 9

    'the below assumes your data sets starts in column A and you want to filter on column I
    .UsedRange.AutoFilter myCol, xlLast90Days 'this "xlLast90Days" is most likely not right, but if you do it manually while recording a macro, you will get the correct syntax

     Dim rngDelete as Range
     On Error Resume Next 'in case there are no visible cells
     Set rngDelete = Intersect(.UsedRange, .UsedRange.Offset(1), .Columns(myCol)).SpecialCells(xlCellTypeVisible) 'assumes first row of usedrange is header row

     'if there are values over 90 delete them
     If not rngDelete is Nothing Then rngDelete.EntireRow.Delete

End With


Answer 3:

我想你会什么想要做的是改变你对于接下来循环到一个For Each循环。 这样,你可以只拉的每一个元素从数组中,并修改它,然后把它放回去。 像这样:

'Psuedo code for learing, won't work if used.
Dim gRange as Range 'Generic
Dim testRange as Range

Set testRange = Worksheets("This").Range("Test Column")

For Each gRange in testRange
    If(moreThan90Days)
        gRange.EntireRow.Delete
    End If
Next gRange

如果您需要更多的指令快速谷歌搜索。对于接下来的循环可能会调高你在找什么。



文章来源: Run equation in every cell in a column