jhovyn
jhovyn

Reputation: 265

How to insert value headers in columns?

The code below adds multiple columns based on the input number. How can I add a value/text from the columns that I created?

Example: If I input the number 5 it will create 5 columns. I want to add a value for that columns.

Option Explicit

Sub Button2_Click()

    Dim iCountCols
    Dim colRef As String
    Dim colRef2 As String
    Dim colAdd As Worksheet

    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    Set colAdd = ThisWorkbook.Worksheets("RDBMergeSheet")
    Sheets("RDBMergeSheet").Select
    Range("M1").Select

    iCountCols = Application.InputBox(Prompt:="Insert number of columns" _
    & "?", Type:=1)

    If iCountCols <= 0 Then End

    Selection.Resize(, iCountCols).EntireColumn.Insert Shift:=xlRight

    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

I want to insert a value at the same time as creating the column. enter image description here

Upvotes: 0

Views: 97

Answers (1)

Dusan Gligoric
Dusan Gligoric

Reputation: 584

Example if I input a number 5 it will create 5 columns..I want to add a value for that columns..

If I understood good you need different value for each column, here it goes.

dim col_values_input as string
dim col_values() as string

col_value=InputBox("Type your column/s value bellow, delimit them with coma")
col_values=Split(col_value, ",")
if col_values.Lenght <> 0 And Not col_values.Lenght > iCountCols Then
   count = 0
   for each val in col_values
      colAdd.range("M1").offset(0,count).value=val
      count = count + 1
   next val
end if

add this at the end of the code, should work well.

Upvotes: 1

Related Questions