地址属性/函数返回单元格地址作为整数对/列表/数组

我在一个函数()和一个子()的死胡同。 我的代码:

sub test() Dim Firstrow, FirstCol As Integer Workbooks("wb").Activate Workbook(1).Worksheet(1).Select FirstRow = 16 FirstCol = 20 LastCell = FindLastcell(FirstRow,FirstCol) 'LastCell is in "RiCj" format FirstCell = Cells(FirstRow, FirstCol).Address(ReferenceStyle:=xlR1C1) 'FirstCell is in "RiCj" format RngSelect = Range(FirstCell, LastCell).Select 'Range Method has failed. Obvious. See [1] [more code to copy as text on Notepad the selection] End Sub 

现在我的function:

 Public Function FindLastCell(ByVal int1 As Integer, ByVal int2 As Integer) As Variant ' what kind of return dim shall I choose ? ' find first empty cell in a range and return its adress LastRow = Cells(Rows.Count, int1).End(xlUp).Row LastCol = Cells(int2, Columns.Count).End(xlToLeft).Column FindLastCell = Cells(LastRow, LastCol).Address(ReferenceStyle:=xlR1C1) End Function 

在尝试了很多变体之后,我无法得到期望的结果。 事实上最好的是:

  • 我的函数应该返回一个整数的列表或数组来作为这个样式的单元格地址Cells(int1,int2)
  • 在我的sub()中,写这样的东西:

    RngSelect =范围(Cells(i,j),Cells(k,l))

我不知道如何在我的function或我的错误中实现这个无用的激怒。

[1] http://msdn.microsoft.com/en-us/library/office/ff838238.aspx如果对范围地址使用文本参数,则必须以A1风格的表示法指定地址(不能使用R1C1风格的符号)。

谢谢你的帮助。

一些事情…

  1. 当您将variables声明为Dim Firstrow, FirstCol As Integer在VBA Dim Firstrow, FirstCol As Integer声明为只有最后一个variables将声明为Interger 。 第一个将被宣布为一个variant 。 改用这个。 Dim Firstrow As Integer, FirstCol As Integer
  2. 在使用行时,避免将行variables声明为Integer 。 声明那么Long 。 在Excel 2007+中,将它们声明为Integer可能会导致Overflow错误。
  3. 避免使用。select.Select/.Activate INTERESTING READ
  4. 完全限定你的对象。 例如, Cells对象可能会给您一个错误或意外的结果。
  5. 避免使用Worksheet(1)的数字。 请使用实际名称或工作表的代号。 这是为了确保您使用正确的纸张,以防纸张混洗。

现在到您的查询。

你不需要一个function。 看到这个

 Dim wb As Workbook Dim ws As Worksheet Sub test() Dim Firstrow As Long, FirstCol As Long Dim FirstCell As String, LastCell As String Dim RngSelect As Range Firstrow = 16: FirstCol = 20 '~~> Change this to the relevant path Set wb = Workbooks.Open("C:\wb.xlsx") Set ws = wb.Sheets("Sheet1") With ws FirstCell = Split(.Cells(, FirstCol).Address, "$")(1) & Firstrow LastRow = .Cells(.Rows.Count, FirstCol).End(xlUp).Row LastCol = .Cells(Firstrow, .Columns.Count).End(xlToLeft).Column LastCell = Split(.Cells(, LastCol).Address, "$")(1) & LastRow Set RngSelect = ws.Range(FirstCell & ":" & LastCell) Debug.Print RngSelect.Address End With ' '[more code to copy as text on Notepad the selection] ' End Sub 

但是,如果你仍然需要一个function,那么看看这个。

 Dim wb As Workbook Dim ws As Worksheet Sub test() Dim Firstrow As Long, FirstCol As Long Dim FirstCell As String, LastCell As String Dim RngSelect As Range Firstrow = 16: FirstCol = 20 '~~> Change this to the relevant path Set wb = Workbooks.Open("C:\wb.xlsx") Set ws = wb.Sheets("Sheet1") With ws FirstCell = Split(.Cells(, FirstCol).Address, "$")(1) & Firstrow LastCell = FindLastCell(FirstCol, Firstrow) Set RngSelect = ws.Range(FirstCell & ":" & LastCell) Debug.Print RngSelect.Address End With ' '[more code to copy as text on Notepad the selection] ' End Sub Public Function FindLastCell(ByVal int1 As Long, ByVal int2 As Long) As String Dim LastRow As Long, LastCol As Long With ws LastRow = .Cells(.Rows.Count, int1).End(xlUp).Row LastCol = .Cells(int2, .Columns.Count).End(xlToLeft).Column FindLastCell = .Cells(LastRow, LastCol).Address End With End Function