【问题标题】:Insert User Input into a new column at end of Excel worksheet将用户输入插入 Excel 工作表末尾的新列
【发布时间】:2018-02-15 00:06:32
【问题描述】:

作为更大宏的一部分,我想在工作表的末尾创建一个新列,然后用输入框的输入填充单元格。

用户输入称为 CourseName。我想在工作表末尾添加一个名为“Course”的新列,然后用 CourseName 中的值填充每行具有活动数据的单元格。

我已经有了 inputBox 代码,而且这个:

With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With

非常感谢您的帮助!

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您可以使用此代码(cmets 中的解释):

    With ActiveSheet.UsedRange ' reference active sheet "used" range 
        With .Columns(.Columns.Count + 1) ' reference the column one column right of the referenced (i.e.: "used" range) range last one
            .Value = CourseName ' fill all referenced columns cell with inputbox return value (adjust "CourseName" to fit the variable name that stores InputBox return value)
            .Cells(1, 1).Value = "Course" 'write referenced column header
        End With
    End With
    

    【讨论】:

      【解决方案2】:
      Sub Macro1()
      Dim LastCol, LastRow As Long
      
      'code of your inputbox, whatever it does. Mine is just to run the macro
      Dim MyInputbox As String
      
      MyInputbox = InputBox("Type something")
      
      
      With ActiveSheet
          LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
          LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
      End With
      
      Range(Cells(1, LastCol + 1), Cells(LastRow, LastCol + 1)).Value = MyInputbox
      
      
      End Sub
      

      根据您的需要进行调整。我专注于始终获取最后一列和最后一行,并在该范围内输入任何输入框的值。

      我第一次运行这个宏时输入了“hello”,第二次输入了“Hello again”。如您所见,宏总是添加一个包含数据的新列。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2013-10-30
        • 1970-01-01
        • 2021-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-03
        • 1970-01-01
        相关资源
        最近更新 更多