Dim Ran1 As Range Set Ran1 = Intersect(ActiveSheet.UsedRange, ActiveWorkbook.Sheets("机况2").Range("B:B"))
时间: 2024-06-07 20:07:16 浏览: 134
这段代码的作用是将当前活动工作表(ActiveSheet)中已使用的区域(UsedRange)与另一个名为“机况2”的工作表中的B列(Range("B:B"))进行交集运算(Intersect),并将结果存储在名为Ran1的Range对象中。换句话说,Ran1对象将包含当前活动工作表中与“机况2”工作表中B列重叠的单元格。
相关问题
VBA 中Intersect(ActiveSheet.UsedRange, X) 如何设置X为B列非空单元格
您可以用以下代码来设置X为B列非空单元格:
```
Set X = Intersect(ActiveSheet.UsedRange, Range("B:B").SpecialCells(xlCellTypeConstants))
```
这样会将B列中非空的单元格赋值给X,然后与ActiveSheet中已使用的单元格范围取交集,得到最终的结果。
帮我合并以下宏程序 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
可以将两个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
```
阅读全文