有人可以指出我的錯誤。
我正在嘗試將范圍物件設定為由 Range1 和 Range1 的偏移量組成。有一張我將要傳入的值表;因此范圍物件變數中的變數。
原始錯誤:運行時錯誤“1004”object_global 的方法“范圍”失敗。本質上,我正在尋找(示例)結果: Range("A2:B94") 其中 A 是已知的,2-是未知的,B94 是從 A2 偏移的 n 列和 n 行。
下面是使用 resize 方法修改的 sub,但仍然有錯誤。
Sub comptst()
Dim line0 As Long, nrow0 As Long, ncol0 as long, diff0 As Long
Dim k As Integer
Dim rng0 As Range
Application.DisplayAlerts = False 'turns off alert display for deleting files sub-routine
For k = 6 To Sheets.Count 'tst_1 is indexed at position 5.
ThisWorkbook.Sheets(k).Activate
Set fsobj = New Scripting.FileSystemObject
If Not fsobj.FileExists(Range("A1")) Then MsgBox "File is missing on sheet index-" & k
Cells(1, 1).Select 'find starting row number
Do
If ActiveCell.Value = "Latticed" Then
b0 = ActiveCell.row 'starting row position
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
DoEvents '
Loop
Cells(4, 1).Select
Do Until ActiveCell.row = b0
line0 = ActiveCell.Value
nrow0 = ActiveCell.Offset(0, 1).Value
ncol0 = ActiveCell.Offset(0, 2).Value - 1
diff0 = b0 - 1
Set rng0 = ThisWorkbook.Sheets(k).Cells(line0 diff0, 1).Resize(nrow0, ncol0)
diff0 = diff0 - 1
Debug.Print rng0.Address
ActiveCell.Offset(1, 0).Select
DoEvents
Loop
Next k
End Sub
uj5u.com熱心網友回復:
您可以使用Range.Resize- 方法。Cells此外,在編碼時使用 - 屬性通常更容易,因為它為行和列采用數字引數。
此外,您應該始終指定您正在使用的作業表,否則 VBA 將使用 ActiveSheet 并且可能是也可能不是您要使用的作業表。
您的變數宣告存在一些問題:
o Dim line0, nrow0, ncol0 As Double將line0and宣告nrow0為 Variant 并且僅宣告ncol0為 Double。您需要為每個變數指定型別。
o 您應該將所有處理行號或列號的變數宣告為Long. Integer可能會給您帶來溢位并且Double沒有多大意義,因為數字從來沒有小數部分。
如果我正確理解您的代碼,您可以使用
Dim line0 as Long, nrow0 as Long, ncol0 As Long, diff0 As Long
With ThisWorkbook.Sheets(1) ' <-- Replace with the book & sheet you want to work with
Set rng0 = .Cells(line0 diff0, 1).resize(nrow0, ncol0)
End With
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492086.html
上一篇:獲取單擊單元格的值
