1

我陷入了函数()和子()的死胡同。我的代码:

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式表示法)。

谢谢你的帮助。

4

1 回答 1

2

一些事情...

  1. 当您Dim Firstrow, FirstCol As Integer在 VBA 中声明变量时,只有最后一个变量将被声明为Interger. 第一个将被声明为variant. 改用这个。Dim Firstrow As Integer, FirstCol As Integer
  2. 使用行时,请避免将行变量声明为Integer. 然后声明为Long。在 Excel 2007+ 中,将它们声明为Integer可能会导致Overflow错误。
  3. 避免使用.Select/.Activate 有趣的阅读
  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
于 2013-10-29T18:06:57.187 回答