使用命名表设置范围

冻结。 我试图设置一个公开的范围( dataIrradianceRangedataTempRange ),但不断收到对象失败错误的方法范围。 任何人都可以告诉我哪里错了。

 Public Sub getData() 'for each loop in data book to find right columns for sites 'each site has two columns in the data book...irradiance and amb temp For j = 2 To siteCount * 2 Step 2 If dataSheet.Cells(1, j).Value = siteNameArray(i) Then Set dataIrradianceRange = dataSheet.Range(Cells(5, j), Cells(lastRow, j)) Set dataTempRange = dataSheet.Range(Cells(5, j + 1), Cells(lastRow, j + 1)) Exit For End If Next j End Sub 

尝试全面的资格

  With dataSheet Set dataIrradianceRange = .Range(.Cells(5, j), .Cells(lastRow, j)) Set dataTempRange = .Range(.Cells(5, j + 1), .Cells(lastRow, j + 1) End With