简体   繁体   中英

Inserting a range into the last column of used data in a sheet

I have an array of doubles that I am trying to write to a certain sheet in excel.

The thing is this sheet already has data in it. There are plenty of code snippets out there that shows me how to find the last column of used data and returns the NUMBER of the column. But I am trying to write the data in my array using the range function which needs a letter as a string. Is there a better method to do this?

Here is my code so far:

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)

For transferring array to worksheet, you need to have Range's size the same as array's size. In order to do it, you can use handy Resize property. Here's how things work:

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

Thank's to Qharr I was able to get some code working using the Cells.Address member. Here's my code snippet I found to be working:

            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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM