1

我试图在我的电子表格中运行方程式,该方程式将通过“I”列并删除从现在起90天内没有到期日期的每一行...在换句话说,我试图将我的电子表格格式化为只给我一份即将在未来90天内过期的所有内容。我放星星的地方就是我无法插入方程的地方。我不知道如何插入公式,但如果它是在单元格中运行,它看起来像这样= IF(AND(I11-900),1,0)= 1。我会怎么改变Q11是如此这样,当公式运行将适用于每一个细胞在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 
+0

什么在'Q11'? – shahkalpesh

回答

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 

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

+0

你如何指定某个列在 –

+0

中运行此公式在上面的代码中,列I是第9列。我已经取消了配方的需要。在上面的代码中检查日期是否在90天内完成。 – shahkalpesh

+0

所以,如果我想改变代码在N列运行,我会改变9到14,并将其中的所有术语编辑为N?我试图运行它,它不断给我一个类型不匹配的错误 –

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循环中快速搜索Google搜索,以便找到您要找的内容。

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 
+0

+1。我认为这也是一个很好的方法。 – shahkalpesh

相关问题