帮我合并以下宏程序 Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range, cell As Range Dim arr() As Variant Dim cnt As Long Dim isCopying As Boolean ' 如果B1单元格为空,直接退出Sub过程 If Me.Range("B1").Value = "" Then Exit Sub If Not Intersect(Target, Me.Range("B1")) Is Nothing Then Sheets("点位提取").Range("C5:C200").ClearContents If Me.Range("AH34").Value = True Then Me.ListBox1.AddItem "数据已被清空 " & Format(Now, "hh:mm:ss") Me.ListBox1.ListIndex = Me.ListBox1.ListCount - 1 End If Set rng = Me.Range("B1:B2000") cnt = 0 isCopying = False For Each cell In rng If cell.Value = ":BEGIN" Then isCopying = True ReDim arr(2000) If Me.Range("AH34").Value = True Then Me.ListBox1.AddItem "开始提取数据 " & Format(Now, "hh:mm:ss") Me.ListBox1.ListIndex = Me.ListBox1.ListCount - 1 End If ElseIf cell.Value = ":END" Then isCopying = False ReDim Preserve arr(cnt - 1) Sheets("点位提取").Range("C5").Resize(cnt, 1).Value = Application.Transpose(arr) If Me.Range("AH34").Value = True Then Me.ListBox1.AddItem "数据已进行提取完毕 " & Format(Now, "hh:mm:ss") Me.ListBox1.ListIndex = Me.ListBox1.ListCount - 1 End If Exit For End If If isCopying And cell.Value <> ":BEGIN" Then arr(cnt) = rng.Cells(cell.Row, 1).Value cnt = cnt + 1 End If Next cell End If On Error GoTo ErrorHandler Exit Sub ErrorHandler: If Me.Range("AH36").Value = True Then Me.ListBox2.AddItem Err.Description & " " & Format(Now, "hh:mm:ss") Me.ListBox2.ListIndex = Me.ListBox2.ListCount - 1 End If End Sub Private Sub Worksheet_Change(ByVal Target As Range) Dim i As Integer Dim j As Integer Dim k As Integer Dim ws As Worksheet Set ws = ActiveSheet If Target.Address = "$B$2" Then For i = 9 To 40 For j = 2 To 7 If ws.Cells(i, j).Value = ws.Cells(8, 5).Value Then For k = 3 To 4 ws.Cells(i, j + k - 2).Value = ws.Cells(Application.Match(ws.Cells(i, 1).Value, ws.Range("AK9:AK40"), 0) + 8, k).Value Next k End If Next j Next i End If End Sub
时间: 2023-06-18 21:05:54 浏览: 398
统一表头的数据合并1234.rar_Excel 宏_excel_宏
可以将两个Sub过程合并为一个,代码如下:
```
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, cell As Range
Dim arr() As Variant
Dim cnt As Long
Dim isCopying As Boolean
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim ws As Worksheet
Set ws = ActiveSheet
' 如果B1单元格为空,直接退出Sub过程
If Me.Range("B1").Value = "" Then Exit Sub
If Not Intersect(Target, Me.Range("B1")) Is Nothing Then
Sheets("点位提取").Range("C5:C200").ClearContents
If Me.Range("AH34").Value = True Then
Me.ListBox1.AddItem "数据已被清空 " & Format(Now, "hh:mm:ss")
Me.ListBox1.ListIndex = Me.ListBox1.ListCount - 1
End If
Set rng = Me.Range("B1:B2000")
cnt = 0
isCopying = False
For Each cell In rng
If cell.Value = ":BEGIN" Then
isCopying = True
ReDim arr(2000)
If Me.Range("AH34").Value = True Then
Me.ListBox1.AddItem "开始提取数据 " & Format(Now, "hh:mm:ss")
Me.ListBox1.ListIndex = Me.ListBox1.ListCount - 1
End If
ElseIf cell.Value = ":END" Then
isCopying = False
ReDim Preserve arr(cnt - 1)
Sheets("点位提取").Range("C5").Resize(cnt, 1).Value = Application.Transpose(arr)
If Me.Range("AH34").Value = True Then
Me.ListBox1.AddItem "数据已进行提取完毕 " & Format(Now, "hh:mm:ss")
Me.ListBox1.ListIndex = Me.ListBox1.ListCount - 1
End If
Exit For
End If
If isCopying And cell.Value <> ":BEGIN" Then
arr(cnt) = rng.Cells(cell.Row, 1).Value
cnt = cnt + 1
End If
Next cell
If Target.Address = "$B$2" Then
For i = 9 To 40
For j = 2 To 7
If ws.Cells(i, j).Value = ws.Cells(8, 5).Value Then
For k = 3 To 4
ws.Cells(i, j + k - 2).Value = ws.Cells(Application.Match(ws.Cells(i, 1).Value, ws.Range("AK9:AK40"), 0) + 8, k).Value
Next k
End If
Next j
Next i
End If
End If
On Error GoTo ErrorHandler
Exit Sub
ErrorHandler:
If Me.Range("AH36").Value = True Then
Me.ListBox2.AddItem Err.Description & " " & Format(Now, "hh:mm:ss")
Me.ListBox2.ListIndex = Me.ListBox2.ListCount - 1
End If
End Sub
```
阅读全文