VBA - Naming Columns according to Specified Range

salman94

New Member
Joined
Jun 7, 2018
Messages
21
Hi,

I have a range of dates in column A from A18 to end (the number of rows will vary) and I've named the range "TradeDates". For each date in that range, I'd like to have a column starting from column J displaying the date on the top and then 3 columns to the right, I'd like the next date to be displayed, then next trade date 3 columns to the right of that and so on. How can this be done in VBA?
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Are the dates in column A repeated?
 
Upvote 0
Perhaps.
Code:
Dim rngTradeDates As Range
Dim rngCol As Range
Dim cl As Range

   Set rngTradeDates = Range("TradeDates")

   Set rngCol = Range("J1")

   For Each cl In rngTradeDates.Cells

       rngCol.Value = cl.Value
       Set rngCol = rngCol.Offset(,3)

   Next cl
 
Upvote 0
How about
Code:
Sub addheaders()
   Dim ary As Variant, Nary As Variant
   Dim i As Long, j As Long
   
   ary = Range("TradeDates").Value2
   ReDim Nary(0 To UBound(ary) * 3)
   For i = 1 To UBound(ary)
      Nary(j) = ary(i, 1)
       j = j + 3
   Next i
   Range("J1").Resize(, UBound(Nary)).Value2 = Nary
End Sub
This will put the dates in row 1
 
Upvote 0
Perhaps.
Code:
Dim rngTradeDates As Range
Dim rngCol As Range
Dim cl As Range

   Set rngTradeDates = Range("TradeDates")

   Set rngCol = Range("J1")

   For Each cl In rngTradeDates.Cells

       rngCol.Value = cl.Value
       Set rngCol = rngCol.Offset(,3)

   Next cl

This worked thanks!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top