在Excel宏中,我如何接受用户选择的列?

时间:2014-02-02 11:45:56

标签: excel excel-vba vba

在Excel宏中,如何接受用户选择的列,然后将该列复制到新的Excel工作表,然后重复多个列的过程(比如7)?

编辑:

使用brettdj的答案结合牧师提供的链接我得到了这个,它完成了这项工作:

Dim rng1 As Range
Dim NewBook As Workbook


Set rng1 = Application.InputBox("Please select a column", "User selection - entire column will be copied", Selection.Address, , , , , 8)
If Not rng1 Is Nothing Then
Set NewBook = Workbooks.Add

rng1.EntireColumn.Copy NewBook.Worksheets("Sheet1").[a1]


End If

2 个答案:

答案 0 :(得分:1)

使用Application.InputBox,类似这样

Sub TrySomethingNextTime()
Dim rng1 As Range
Dim ws As Worksheet

On Error Resume Next
Set rng1 = Application.InputBox("Please select a column", "User selection - entire column will be copied", Selection.Address, , , , , 8)
On Error Goto 0

If Not rng1 Is Nothing Then
    Set ws = Sheets.Add
    rng1.EntireColumn.Copy ws.[a1]
End If

End Sub

答案 1 :(得分:1)

那是如此的轻松和美丽!谢谢...但你怎么做行?!您选择的行。我自己试一试。那将是我的圣杯!这太棒了!!适用于行:

    Sub TrySomethingNextTime2()
    Dim rng1 As Range
    Dim ws As Worksheet

    On Error Resume Next
    Set rng1 = Application.InputBox("Please select a row", "User selection -    entire row will be copied", Selection.Address, , , , , 8)
    On Error GoTo 0

    If Not rng1 Is Nothing Then
       Set ws = Sheets.Add
        rng1.EntireRow.Copy ws.[a1]
   End If

   End Sub