如何根据最小截止值dynamic删除数据

我是在路易斯安那州的一家小型石油公司工作的地质学家。 我构成了我们的技术部门,不幸的是我的编码经验非常有限。 过去我使用过非常基本的vba编码,但是在日常工作中,我没有太多编码,所以我已经忘记了大部分。 我最近发现了这个网站,对我来说这已经成为一个很好的资源。 我已经能够从以前的问题中收集一些代码,但是我又一次被卡住了。

我正在研究一个macros,从路易斯安那州的DNR状态下载生产信息,然后计算出下载数据的某些值。 到目前为止,我有代码检索数据,sorting,并计算出我需要的值。

现在我需要能够修剪这些数据。 每口井将有不同的时间生产石油。 目前我的macros将公式扩展到单元格50(数据将永远不会达到这一点,我select它只是为了确保公式适用于所有单元格)。 大部分计算值是0我想创build一个macros,删除包含数字1或更大的最后一个单元格后的所有值。

包含我要删除的数据的单元格在单元格L中

请让我知道是否还有其他你需要的东西,或者如果我没有很好地解释我的问题。

这里是我已经生成的计算单元格L中的数据的代码

Sub Oil_production_by_year() ' ' Oil_production_by_year Macro ' ' Dim ws As Worksheet For Each ws In Sheets ws.Activate Range("L2").Select ActiveCell.FormulaR1C1 = _ "=SUMIFS(RC[-8]:R[2998]C[-8],RC[-11]:R[2998]C[-11],"">=""&RC[8],RC[-11]:R[2998]C[-11],""<=""&RC[9])" Range("L2").Select ActiveWindow.ScrollColumn = 10 ActiveWindow.ScrollColumn = 9 ActiveWindow.ScrollColumn = 8 ActiveWindow.ScrollColumn = 7 ActiveWindow.ScrollColumn = 6 ActiveWindow.ScrollColumn = 5 ActiveWindow.ScrollColumn = 6 ActiveWindow.SmallScroll Down:=-6 Selection.AutoFill Destination:=Range("L2:L51"), Type:=xlFillDefault Range("L2:L51").Select Next ws End Sub 

你好,Josiah,祝你新年快乐。

此代码假定:

dataCol是数据的引用列,用于确定自动填充SUMIFS公式的行数。 目前它被设置为第五栏(栏E),所以改变这个以适合你的数据。

trimVal是一个variables,它保存单元格值将被删除的值。 目前设置为1。

当一个公式单元格被删除时,同一列中的任何数据将被上移。

 Option Explicit Sub Oil_production_by_year() ' ' Oil_production_by_year Macro ' Dim ws As Worksheet Dim stRow As Long, formulaCol As Long, c As Long Dim dataCol As Long, dataEndRow As Long, trimVal As Long stRow = 2 formulaCol = 12 trimVal = 1 'an assumption of colE for the data column 'change this to suit 'means that the formula will only fill the rows reqd dataCol = 5 For Each ws In Sheets With ws dataEndRow = .Cells(Rows.Count, dataCol).End(xlUp).Row With ws.Cells(stRow, formulaCol) .FormulaR1C1 = _ "=SUMIFS(RC[-8]:R[2998]C[-8],RC[-11]:R[2998]C[-11],"">=""&RC[8],RC[-11]:R[2998]C[-11],""<=""&RC[9])" .AutoFill Destination:=ws.Range(ws.Cells(stRow, formulaCol), ws.Cells(dataEndRow, formulaCol)), _ Type:=xlFillDefault End With For c = dataEndRow To stRow Step -1 If .Cells(c, formulaCol).Value < trimVal Then .Cells(c, formulaCol).Delete xlShiftUp Else Exit For End If Next c End With Next ws End Sub 

仍然不知道你想删除什么样的数据…我build议改变代码,如下所示:

 Option Explicit Sub Oil_production_by_year() Dim wsh As Worksheet, iLastCell As Integer, r As Integer For Each wsh In ThisWorkbook.Worksheets iLastCell = GetLastCell(wsh) wsh.Range("L2").FormulaR1C1 = "=SUMIFS(RC[-8]:R[2998]C[-8],RC[-11]:R[2998]C[-11],"">=""&RC[8],RC[-11]:R[2998]C[-11],""<=""&RC[9])" wsh.Range("L2").AutoFill Destination:=wsh.Range("L2:L" & iLastCell), Type:=xlFillDefault r = iLastCell Do While r > 2 If wsh.Range("L" & r).Value = 0 Then 'delete entire row wsh.Range("L" & r).EntireRow.Delete xlShiftUp End If If wsh.Range("L" & r).Value > 0 Then Exit Do End If r = r - 1 Loop Next End Sub Function GetLastCell(wsh As Worksheet, Optional sCol As String = "A") GetLastCell = wsh.Range(sCol & wsh.Rows.Count).End(xlUp).Row End Function 

根据需要更改代码;)