将范围插入工作表中已使用数据的最后一列

时间:2017-12-24 19:17:03

标签: excel vb.net range

我有一系列双打,我试图写入excel中的某张表。

事情是这张表中已有数据。有很多代码片段,告诉我如何找到已使用数据的最后一列并返回列的NUMBER。但我正在尝试使用范围函数在数组中写入数据,该函数需要一个字母作为字符串。有没有更好的方法来做到这一点?

到目前为止,这是我的代码:

Dim lCol As Integer
        With GlobVars.Wksht
            lCol = .Cells(1, .Columns.Count).End(Excel.XlDirection.xlToLeft).Column
        End With
        lCol += 1
        GlobVars.Wksht.Range("I need something in these parentheses that will relate the lcol value and whatever row I want to put it in").Value = deltaxyarr 'copies the deltaxyarr as a range into the current active worksheet (data reduction)

2 个答案:

答案 0 :(得分:0)

要将数组传输到工作表,您需要使Range的大小与数组的大小相同。为此,您可以使用方便的Resize属性。以下是工作原理:

Sub TransferArray()

    Dim arr As Variant

    'Copy range into array.
    'This array is 1) always 2-dimensional and 2) its lower bound is always 1.
    arr = Range("A1:F10")

    'To transer array, the receiving range should accomodate array fully.
    'To do it, we can use Resize property.
    'This means, all we need is to choose top left cell of our receiving range.
    'Here K1 cell is this very cell.
    'The RowSize argument means how many rows we should expand down,
    'and ColumnSize - how many column we should expand right.
    'Apparently, these numbers are upper bound of 1-st dimension
    'and upper bound of 2-nd dimension, respectively.
    Range("K1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr

End Sub

答案 1 :(得分:0)

感谢Qharr我能够使用Cells.Address成员获得一些代码。这是我发现的代码片段:

            lCol = .Cells(1, .Columns.Count).End(Excel.XlDirection.xlToLeft).Column

            lCol += 1
            GlobVars.Wksht.Range(GlobVars.Wksht.Cells(1, lCol).Address).Resize(UBound(deltaxyarr, 1), UBound(deltaxyarr, 2)).Value = deltaxyarr