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

时间:2013-10-29 17:40:37

标签: excel excel-vba vba

我在函数()和子()中走到了尽头。我的代码:

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

现在我的职能:

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

在尝试了很多变种之后,我无法获得理想的结果。事实上最好的是:

  • 我的函数将返回一个列表或整数数组,用作此样式的单元格地址单元格(int1,int2)
  • 在我的sub()中,写下这样的东西:

      

    RngSelect =范围(单元格(i,j),单元格(k,l))。选择

我不知道如何在我的函数或我的子错误中实现这个whitout trigering。

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

很少......

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

    您不需要功能。见这个

    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
    

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

    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