今天有人提出这样一个问题,
ExcelVBA取序号与合计之间的数据 |
数据如下:
分析一下,问题关键:
问题: 1.我要在“序号”两字后面开始取数,因为序号是合并的,所以。。。 2.我要取合计前面的数据,所以要查找“合计”所在的行号 |
下面用代码解决以上2个问题,取数部分暂时不做了
解决过程的测试图如下
代码共享如下:
Sub endLastRow()Dim lastRow As IntegerWith ActiveSheetlastRow = .Range("A3").End(xlDown).rowEnd WithMsgBox (lastRow)
End Sub
Sub FindTotalRow()Dim rngFound As RangeDim ws As WorksheetSet ws = ActiveSheet' 或者指定某个worksheet:Set ws = ThisWorkbook.Sheets("Sheet1")' 在第一列查找"合计"Set rngFound = Cells.Find(What:="合计", LookIn:=xlValues, LookAt:=xlWhole) ' ws.Columns(1)If Not rngFound Is Nothing ThenDim k As Longk = rngFound.rowMsgBox "找到'合计'在第 " & k & " 行。"ElseMsgBox "'合计'未在第一列中找到。"End If
End Sub
Sub InStrLastRow()Dim ws As WorksheetDim lastRow As IntegerDim endRow As IntegerDim ts As StringDim ss As Stringss = "合计"Set ws = ActiveSheetWith wsendRow = .Cells(rows.Count, "A").End(xlUp).rowFor j = 1 To endRowts = .Cells(j, 1)If ts <> "" ThenIf InStr(1, ts, ss) ThenlastRow = jExit ForEnd IfEnd IfNextEnd WithMsgBox "最后是:" & endRow & Chr(13) & "合计是:" & lastRow
End Sub
Sub 查找序号Row()Dim ws As WorksheetDim sRng As RangeDim sRow As IntegerDim sendRow As IntegerDim ts As StringDim ss As Stringss = "序号"Set ws = ActiveSheetWith wsSet sRng = .Cells.Find(ss)sRow = sRng.rowIf Not sRng.Offset(1).MergeCells Then sendRow = sRng.Offset(1).rowEnd WithMsgBox ss & "-起始行号:" & sRow & Chr(13) & "它后面的有效行号:" & sendRowss = "情况说明"With wsSet sRng = .Cells.Find(ss)sRow = sRng.rowIf Not sRng.Offset(1).MergeCells Then sendRow = sRng.Offset(1).rowEnd WithMsgBox ss & "-起始行号:" & sRow & Chr(13) & "它后面的有效行号:" & sendRow
End Sub
如果对你有帮助,请转给更多人免费学习,复制