简体   繁体   中英

How do I auto-fill excel Table data range to right dynamically using vba code

I want to write VBA code for auto-filling excel Table range data dynamically to Right side column wise as per given date. For eg. If I give Date for "Sep-19" in Worksheet it should automatically auto-fill the table range to right containing Sept-19 data. I am using Excel 2016 version. Below is the table data:

在此处输入图像描述

I have tried some code, below is the vba code for it:

Sub Macro4()

Range("Table1[[#Headers],[Month]]").Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FillRight
Range("Table1[[#All],[Aug-19]:[Sep-19]]").Select

End Sub

I want the Range("Table1[[#All],[Aug-19]:[Sep-19]]").Select in last of code to be set up dynamic as per given month, how can I do it?

Below code will select the dynamic range of the column and first 6 rows starting from A1

Sub rg()
Dim sht As Worksheet
Set sht = Worksheets("Data")

Dim LastRow As Long
Dim LastCol As Long

LastRow = Cells(Rows.Count, "A").End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Columns(LastCol).Copy
Columns(LastCol + 1).Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

End Sub

Thank you all, finally I got the solution for my problem, below is the code for it.

Sub Macro4()

    Range("Table1[[#Headers],[Month]]").Select
    Selection.End(xlToRight).Select
    Range(Selection, Selection.End(xlDown)).Copy

    ActiveCell.Offset(0, 1).PasteSpecial xlPasteAll
    ActiveCell.FormulaR1C1 = Range("N3").Value


End Sub

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