VBA / Excel应用程序定义或对象定义的错误

在我的Excel VBAmacros中获取应用程序定义或对象定义的错误,我不知道为什么。 我猜测它与我如何调用单元格引用有关。 我已经看了一些其他类似的问题。 他们是非常相似的,但我错过了我的错误的地方。

代码如下:

Set AllianceSheet = Worksheets(2) Dim YearBeginRange As Range Dim YearEndRange As Range Dim Year As Integer Dim BeginRow As Long Dim EndRow As Long Dim CountryRange As Range 'Dim WS As Worksheet 'Dim WSName As String Year = 1965 'WSName = "Ally-" & Year Set YearBeginRange = AllianceSheet.Range("C1", AllianceSheet.Range("C1").End(xlDown)).Find(What:=Year, After:=AllianceSheet.Range("C1"), LookIn:=xlFormulas, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If YearBeginRange Is Nothing Then MsgBox Year & "Begin year not found" Else 'MsgBox Year & "Begin year found!" 'Debug.Print YearBeginRange.Row 'Debug.Print TypeName(YearBeginRange.Row) BeginRow = YearBeginRange.Row End If Set YearEndRange = AllianceSheet.Range("C1", AllianceSheet.Range("C1").End(xlDown)).Find(What:=Year + 1, After:=AllianceSheet.Range("C1"), LookIn:=xlFormulas, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If YearBeginRange Is Nothing Then MsgBox Year & "End year not found" Else EndRow = YearEndRange.Row End If Set CountryRange = AllianceSheet.Range(AllianceSheet.Cells(1, BeginRow), AllianceSheet.Cells(1, EndRow)) 'Debug.Print BeginRow 'Debug.Print EndRow 'Debug.Print WSName End Sub 

错误在于:

 Set CountryRange = AllianceSheet.Range(AllianceSheet.Cells(1, BeginRow), AllianceSheet.Cells(1, EndRow)) 

我知道我错过了一些非常基本的东西,所以提前感谢。 FYI,BeginRow = 65930,EndRow = 81430。

你有参数.Cells颠倒 – 它应该是row, column ,而不是column, row

 Set CountryRange = AllianceSheet.Range(AllianceSheet.Cells(BeginRow, 1), _ AllianceSheet.Cells(EndRow, 1))