简体   繁体   中英

Insert cell values on variable rows

this is an update on my question yesterday. I figured out how to insert the rows, now I just need to figure out how to give those values names. I need to associate a month name with each column inserted, so where I insert a column I want the top row of the column to say "January". You all have been excellent help so far, thanks!

Sub Months()

Dim y As String
y = "Q1"
If y = "" Then Exit Sub
Dim x As Long
For x = Cells(1, Columns.Count).End(xlUp).Column To 1 Step -1
If Cells(1, x).value = y Then
Columns(x).EntireColumn.Insert
'Cell(1,x).value="January"
Columns(x).EntireColumn.Insert
Columns(x).EntireColumn.Insert
End If
Next x

The commented out section is what I'm looking for, just not quite sure how to get it so that it can replace multiple cells.

EDIT: Solved it, it won't let me post my answer, but the solution is to post the value below all of the column insertions. Hopefully this helps for anyone else trying to figure this out.

Got it! Boy do I feel dumb. It was just the placement I was putting it in.

Dim y As String
y = "Q1"
If y = "" Then Exit Sub
Dim x As Long
For x = Cells(1, Columns.Count).End(xlUp).Column To 1 Step -1
If Cells(1, x).value = y Then
Columns(x).EntireColumn.Insert
Columns(x).EntireColumn.Insert
Columns(x).EntireColumn.Insert
Cells(1, x).value = "January"
Cells(1, x + 1).value = "Febuary"
Cells(1, x + 2).value = "March"
End If
Next x

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