【问题标题】:Address property/function to return cell address as a pair/list/array of Integers将单元格地址作为整数对/列表/数组返回的地址属性/函数
【发布时间】:2013-10-30 03:14:50
【问题描述】:

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

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

在尝试了许多变体后,我无法得到想要的结果。事实上,最好的情况是:

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

    RngSelect = Range(Cells(i,j),Cells(k,l)).Select

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

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

感谢您的帮助。

【问题讨论】:

  • 为什么需要使用 R1C1 表示法?如果你从你的 sub 和 function 中删除两个 ReferenceStyle:=xlR1C1 参数,它将起作用。
  • 为什么不制作另一个没有这个的 findlastcell 版本:'Address(ReferenceStyle:=xlR1C1)'?
  • @Joe:我所有的床单都是 LC 风格的,这对我来说要容易得多。但你是对的,这是一个解决方案。
  • @Alan Waage:我尝试在一对 Longs 中获取地址,但没有成功。一个肮脏的黑客可能是使用 Double。

标签: excel vba


【解决方案1】:

几件事...

  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) 中使用数字。使用工作表的实际名称或代号。这是为了确保您使用正确的工作表,以防工作表被打乱。

现在开始您的查询。

你不需要函数。看到这个

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

【讨论】:

  • 非常好,感谢您提供的所有提示。我知道我不需要函数,但我需要在我的程序的范围内找到单元格,所以函数的原因(+一些培训:-)。事实上,我仍然在使用激活/选择方法。 wb(1) 只是为了帖子。我使用真实姓名,例如 wb("wb")。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
相关资源
最近更新 更多