在列中的每个单元格中运行方程式

时间:2012-12-18 18:15:06

标签: vba if-statement excel-vba excel-formula calculated-columns

我试图在我的电子表格中运行一个方程式,该方程式将通过列“I”并删除在90天内没有到期日期的每一行...换句话说我试图格式化我的电子表格只是给我一个在接下来的90天内到期的所有内容的列表。我放置星星的行是我在插入等式时遇到困难的地方。我不确定如何插入等式,但如果它单独在单元格中运行,它看起来像这样= IF(AND(I11-900),1,0)= 1。我将如何改变Q11以便在运行等式时它将适用于I列中的每个单元而不仅仅是I 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

3 个答案:

答案 0 :(得分:3)

我目前没有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

答案 1 :(得分:2)

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

编辑:以此为基础开始。
您可以删除宏录制器生成的不必要代码。

答案 2 :(得分:0)

我认为您要做的是将For Next循环更改为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

如果你需要更多指导,快速谷歌搜索For Next循环可能会找到你想要的东西。