代码拒绝定义不在sheet1中的活动页面上的范围

在Sheet1上有一个列表框,其中有一个sheetnames列表。 当有人双击列表中的名字时,代码应该激活该表格,select一些数据并从中创build一个图表。

代码很好,直到我要求它在另一个表上定义一个范围。 我有很多不同的错误信息,而且我可以告诉,代码只是拒绝做任何不在sheet1上的东西。 如果有人能够解释为什么,这将是辉煌的。

代码:列表框被称为Stocklist

Option Explicit Sub StockList_DblClick(ByVal Cancel As MSForms.ReturnBoolean) Call stockgraph End Sub Private Sub stockgraph() Application.ScreenUpdating = False Dim stockrange As Range Dim daterange As Range Dim security_name As String Dim finalrow As Integer Dim stockarray() As Double Dim datearray() As String Dim cell As Range security_name = Empty security_name = StockList.Value If security_name = Empty Then MsgBox ("something's gone wrong, excel doesn't recognise that value") ' DEBUG Worksheets(security_name).Activate ' --> this bit works fine finalrow = ActiveSheet.Cells(1, 1).End(xlDown).row ' --> as does this Set stockrange = Sheets(security_name).Range(Cells(2, 3), Cells(finalrow, 3)) ' --> This gives a 1004 error, so does using activesheet ' --> if I don't reference a sheet, despite it not being the activesheet, the ranges are defined on sheet1 ' --> and yet, the code was perfectly fine defining finalrow Set daterange = Sheets(security_name).Range(Cells(2, 1), Cells(finalrow, 1)) ReDim stockarray(1 To finalrow - 1) As Double ' row 1 is a header so 2 to finalrow = 1 to finalrow-1 ReDim datearray(1 To finalrow - 1) As String For Each cell In stockrange stockarray(cell.row - 1) = cell.Value Next cell For Each cell In daterange datearray(cell.row - 1) = cell.text Next cell Sheets("Top 10 holdings").Activate ' Create graph Dim c As Chart Dim s1 As Series ActiveSheet.Cells(50, 50) = stockarray ActiveSheet.Shapes.AddChart.Select Set c = ActiveChart Set s1 = c.SeriesCollection(1) c.ChartType = xlLine s1.Values = stockarray Application.ScreenUpdating = True End Sub 

如果没有完全限定用作分界点的内部单元格引用,则不能以此方式构造单元格区域引用。

 With Sheets(security_name) finalrow = .Cells(1, 1).End(xlDown).row Set stockrange = .Range(.Cells(2, 3), .Cells(finalrow, 3)) Set daterange = .Range(.Cells(2, 1), .Cells(finalrow, 1)) End With