簡體   English   中英

對象“ _Worksheet”的“方法”范圍失敗

[英]'Method 'Range' of object '_Worksheet' failed

到目前為止,我從沒有對此有任何疑問。 請查看下面的代碼,它位於工作簿中的“模塊”上(而不是工作表中的代碼),當嘗試設置范圍時,代碼會中斷。

Private Sub UpdateTickerList()
    Dim MyWS As Worksheet
    Dim a, b, c As Integer
    Dim NewStockRng As Range
    Dim RealTickFeed As Range
    'On Error Resume Next (i took this out to get the error)
    'initializes variables
    a = 0
    b = 0


    'defines the worksheet we are going to work on
    Set MyWS = Workbooks("Portfolio.xlsm").Worksheets("Feed")
    'finds last row
    b = MyWS.Range("a10000").End(xlUp).Row + 1
    'finds how large is the new stock universe
    a = 8 'UBound(NPSeCompran)

    'defines a new range in which to copy the new symbols
    Set NewStockRng = MyWS.Range(Cells(b, 1), Cells(b + a - 1, 1)) 'i need to 
    'use b+a-1 to reflect the fact that if i
    'have row97 as my first row and 11 elements then i need to count row 97 
    'as#1, otherwise i end up with one more row
    'copies the stocks to the range
    NewStockRng.value = Application.Transpose(NPSeCompran)

    'now sort the list
    Set RealTickFeed = MyWS.Range("a3").CurrentRegion
    RealTickFeed.Sort key1:=MyWS.Range("a3"), Header:=xlYes

    'now get rid of duplicates
    RealTickFeed.RemoveDuplicates Columns:=Array(1, 9), Header:=xlYes

    ErrorHandler:
End Sub

我將放在Cells()

Set NewStockRng = Range(MyWS.Cells(b, 1),MyWS.Cells(b + a - 1, 1))

(可能還有其他問題)

編輯#1:

這是一個簡單的示例:

Sub qwerty()
    a = 12
    b = 15
    Sheets("Sheet2").Activate
    Set MyWS = Worksheets("Sheet1")

    Set NewStockRng = Range(MyWS.Cells(b, 1), MyWS.Cells(b + a - 1, 1))

    MsgBox NewStockRng.Address(o, o) & vbCrLf & NewStockRng.Parent.Name
End Sub

在此處輸入圖片說明

編輯#2:

關鍵問題是Cells()本身的范圍已經很小。 從而:

Range(whatever.Cells(1,1), whatever.Cells(3,3))

是完全合格的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM