函数 returns 临时 sheet
Function returns temporary sheet
是否可以创建一个 return 临时 sheet 的函数?
假设我有 Sub 如下
Sub My_Sub()
Dim harm As Worksheet
Set harm = Sheets("my_sheet")
Dim lastRow As Long, arr
lastRow = harm.Range("A" & harm.Rows.Count).End(xlUp).Row
arr = harm.Range("T2:V" & lastRow).Value
MsgBox arr(2,5)+1
End Sub
现在我正在处理 harm = Sheets("my_sheet")
,它会加载整个 sheet。现在我想 select 的一部分 sheet 并做同样的操作所以我想写一个函数来创建临时的 sheet, return 所以在 My_Sub
我会 Set harm = ReturnSheet()
.
可能吗?我想从函数中加载伪 sheet,所以我不需要更改 My_Sub
中的任何内容(我的意思是那些带有列字母索引的范围)。
Function ReturnSheet() As Worksheet
Dim Rng As Range
Dim lastRow As Long
Dim lastCol As Long
Set Rng = Selection
lastRow = Selection.Rows.Count
lastCol = Selection.Columns.Count
ReturnSheet.Range("A2").Resize(lastRow, lastCol).Value = Rng
End Function
现在 Object variable or with block variable not set
ReturnSheet.Range("A2").Resize(lastRow, lastCol).Value = Rng
尝试使用下一个 Function
。它 returns 表示所选单元格没有第一行的范围:
Function ReturnRange(Optional boolAllRange As Boolean = False) As Range
Dim rng As Range: Set rng = Selection
If rng.rows.count = 1 Then Exit Function
If boolAllRange Then
Set ReturnRange = rng
Else
Set ReturnRange = rng.Offset(1).Resize(rng.rows.count - 1, rng.Columns.count)
End If
End Function
您可以使用下一个 Sub
:
进行测试
Sub testReturnRange()
Dim rng As Range
Set rng = ReturnRange 'eliminating the header
If Not rng Is Nothing Then Debug.Print rng.Address
Set rng = ReturnRange(True) 'header inclusive...
If Not rng Is Nothing Then Debug.Print rng.Address
End Sub
是否可以创建一个 return 临时 sheet 的函数? 假设我有 Sub 如下
Sub My_Sub()
Dim harm As Worksheet
Set harm = Sheets("my_sheet")
Dim lastRow As Long, arr
lastRow = harm.Range("A" & harm.Rows.Count).End(xlUp).Row
arr = harm.Range("T2:V" & lastRow).Value
MsgBox arr(2,5)+1
End Sub
现在我正在处理 harm = Sheets("my_sheet")
,它会加载整个 sheet。现在我想 select 的一部分 sheet 并做同样的操作所以我想写一个函数来创建临时的 sheet, return 所以在 My_Sub
我会 Set harm = ReturnSheet()
.
可能吗?我想从函数中加载伪 sheet,所以我不需要更改 My_Sub
中的任何内容(我的意思是那些带有列字母索引的范围)。
Function ReturnSheet() As Worksheet
Dim Rng As Range
Dim lastRow As Long
Dim lastCol As Long
Set Rng = Selection
lastRow = Selection.Rows.Count
lastCol = Selection.Columns.Count
ReturnSheet.Range("A2").Resize(lastRow, lastCol).Value = Rng
End Function
现在 Object variable or with block variable not set
ReturnSheet.Range("A2").Resize(lastRow, lastCol).Value = Rng
尝试使用下一个 Function
。它 returns 表示所选单元格没有第一行的范围:
Function ReturnRange(Optional boolAllRange As Boolean = False) As Range
Dim rng As Range: Set rng = Selection
If rng.rows.count = 1 Then Exit Function
If boolAllRange Then
Set ReturnRange = rng
Else
Set ReturnRange = rng.Offset(1).Resize(rng.rows.count - 1, rng.Columns.count)
End If
End Function
您可以使用下一个 Sub
:
Sub testReturnRange()
Dim rng As Range
Set rng = ReturnRange 'eliminating the header
If Not rng Is Nothing Then Debug.Print rng.Address
Set rng = ReturnRange(True) 'header inclusive...
If Not rng Is Nothing Then Debug.Print rng.Address
End Sub