Greg Elliott
Greg Elliott

Reputation: 1

Writing values in an array to an excel range - VBA

I am trying to write values from an array to a range (just one row) into an excel sheet. The vba script asks for a start and end date, and then generates the months for those dates in a specific format MMMYY (Jan19, Feb19, Mar19, etc). It stores each of those values in an array. Then I want it to write those values to a row in the excel sheet based on a selected cell where those values would start.

With what I have written it only writes Jan19 across the row. I've messed around with it, but I am not sure what I am doing wrong. I appreciate the help! Here is what I have so far.

Sub AddYearHeaders()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    Dim dtStart As Date
    Dim dtEnd As Date
    Dim iCtr As Integer

    Dim rStCell As Range
    Dim row As Integer
    Dim intStCol As Integer
    Dim arr() As Variant
    
    dtStart = InputBox("Please input start date of period (mm/dd/yyyy)", "User Input", "Enter start date HERE")
    dtEnd = InputBox("Please input end date of period (mm/dd/yyyy)", "User Input", "Enter end date HERE")
    
    iCtr = DateDiff("m", dtStart, dtEnd)
    
    Set rStCell = Application.InputBox(Prompt:="Please select a cell", Type:=8)
    
    row = rStCell.row
    intStCol = rStCell.Column
    
    For I = 0 To iCtr
        
        ReDim Preserve arr(0, I)
        arr(0, I) = Format(dtStart, "MMMYY")
        dtStart = DateAdd("m", 1, dtStart)
        
    Next I
    
    rStCell.Resize(1, UBound(arr, 2) + 1) = WorksheetFunction.Transpose(arr)

End Sub

Upvotes: 0

Views: 245

Answers (1)

Excel Hero
Excel Hero

Reputation: 14764

This should do it...

rStCell.Resize(1, I) = arr

Upvotes: 1

Related Questions