如何在列中插入值标题?

时间:2017-05-14 08:17:20

标签: excel vba excel-vba

下面的代码根据输入的数字添加多个列。如何从我创建的列中添加值/文本?

示例:如果我输入数字5,它将创建5列。我想为这些列添加一个值。

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

我想在创建列的同时插入一个值。 enter image description here

1 个答案:

答案 0 :(得分:1)

  

示例如果我输入一个数字5,它将创建5列..我想为这些列添加一个值..

如果我理解得好,你需要为每一列提供不同的价值,这就是它。

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

在代码末尾添加此内容,应该可以正常运行。

相关问题