今天有人提出这样一个问题,
ExcelVBA取序号与合计之间的数据 |
数据如下:
分析一下,问题关键:
问题: 1.我要在“序号”两字后面开始取数,因为序号是合并的,所以。。。 2.我要取合计前面的数据,所以要查找“合计”所在的行号 |
下面用代码解决以上2个问题,取数部分暂时不做了
解决过程的测试图如下
代码共享如下:
Sub endLastRow()
Dim lastRow As Integer
With ActiveSheet
lastRow = .Range("A3").End(xlDown).row
End With
MsgBox (lastRow)
End Sub
Sub FindTotalRow()
Dim rngFound As Range
Dim ws As Worksheet
Set 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 Then
Dim k As Long
k = rngFound.row
MsgBox "找到'合计'在第 " & k & " 行。"
Else
MsgBox "'合计'未在第一列中找到。"
End If
End Sub
Sub InStrLastRow()
Dim ws As Worksheet
Dim lastRow As Integer
Dim endRow As Integer
Dim ts As String
Dim ss As String
ss = "合计"
Set ws = ActiveSheet
With ws
endRow = .Cells(rows.Count, "A").End(xlUp).row
For j = 1 To endRow
ts = .Cells(j, 1)
If ts <> "" Then
If InStr(1, ts, ss) Then
lastRow = j
Exit For
End If
End If
Next
End With
MsgBox "最后是:" & endRow & Chr(13) & "合计是:" & lastRow
End Sub
Sub 查找序号Row()
Dim ws As Worksheet
Dim sRng As Range
Dim sRow As Integer
Dim sendRow As Integer
Dim ts As String
Dim ss As String
ss = "序号"
Set ws = ActiveSheet
With ws
Set sRng = .Cells.Find(ss)
sRow = sRng.row
If Not sRng.Offset(1).MergeCells Then sendRow = sRng.Offset(1).row
End With
MsgBox ss & "-起始行号:" & sRow & Chr(13) & "它后面的有效行号:" & sendRow
ss = "情况说明"
With ws
Set sRng = .Cells.Find(ss)
sRow = sRng.row
If Not sRng.Offset(1).MergeCells Then sendRow = sRng.Offset(1).row
End With
MsgBox ss & "-起始行号:" & sRow & Chr(13) & "它后面的有效行号:" & sendRow
End Sub
如果对你有帮助,请转给更多人免费学习,复制