【问题标题】:VBA Excel Copy and Paste a table into a new Workbook and choice which Columns i want to copyVBA Excel将表格复制并粘贴到新工作簿中,然后选择我要复制的列
【发布时间】:2016-09-25 09:04:26
【问题描述】:

我想将一个表复制到一个新的工作簿中,同时选择我要复制的范围并且知道第一列(“A”)会自动复制。 (行不是问题,都得复制) 例如,我有一个由 28 行和 10 列组成的表。添加到 A1:A28 (第一列,所有行),我只想复制第 5 列和第 8 列及其所有行。 这就是我到目前为止所拥有的,但它不起作用。

Sub CommandButton1_Click()
  Dim newWB As Workbook, currentWB As Workbook
  Dim newS As Worksheet, currentS As Worksheet
  Dim CurrCols As Variant
  Dim rng As rang
  'Copy the data you need
    Set currentWB = ThisWorkbook
    Set currentS = currentWB.Sheets("Feuil1")
    'select which columns you want to copy
    CurrCols = InputBox("Select which column you want to copy from        table (up to 10)")
    If Not IsNumeric(CurrCols) Then
    MsgBox "Please select a valid Numeric value !", vbCritical
    End
    Else
    CurrCols = CLng(CurrCols)
    End If
    'Set rng = currentWB.currentS.Range(Cells(1, A), Cells(27, CurrCols)).Select
    currentS.Range("A1:A27").Select
    Selection.copy
    Set rng = currentWB.currentS.Range(Cells(1, CurrCols), Cells(28, CurrCols)).Select
    rng.copy
    'Create a new file that will receive the data
    Set newWB = Workbooks.Add
    With newWB
    Set newS = newWB.Sheets("Feuil1")
    newS.Range("A1").PasteSpecial Paste:=xlPasteValues,    Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
End With
End Sub

你能帮忙解决一下吗?提前致谢!

【问题讨论】:

  • “复制第 5 列和第 8 列及其所有行” 究竟是什么意思?发布输入和输出数据的示例
  • 例如,我有一个表格作为输入,该表格由从 A 到 G 的 28 行和列组成。作为输出,我想从我的表格中获得一个 28 行和列 A、C 和 F 的表格输入(所以它由我输入的三列组成)
  • 我在你的代码中哪里得到 1)“输入”表 2)要选择的列?
  • 在 currentWB.currentS 我已经有了我的表格,我将选择我的列。就在评论之后:'选择要复制的列;我开始选择列,CurrCols 是我要复制的列 Set rng = currentWB.currentS.Range(Cells(1, CurrCols), Cells(28, CurrCols)).Select rng.copy 是复制此列并粘贴但它不起作用! currentS.Range("A1:A27").Select Selection.copy 只是复制所有行的第一列,因为我总是需要它

标签: vba excel


【解决方案1】:

您不能复制非连续范围,但可以将数据加载到数组中,然后将其写入新工作簿一次。

Private Sub CommandButton1_Click()
    Dim arData
    Dim MyColumns As Range, Column As Range
    Dim x As Long, y As Long

    On Error Resume Next
    Set MyColumns = Application.InputBox(Prompt:="Hold down [Ctrl] and click the columns to copy", Title:="Copy Columns to new Workbook", Type:=8)
    On Error GoTo 0

    If MyColumns Is Nothing Then Exit Sub

    Set MyColumns = Union(Columns("A"), MyColumns.EntireColumn)

    Set MyColumns = Intersect(MyColumns, ActiveSheet.UsedRange)

    ReDim arData(1 To MyColumns.Rows.Count, 1 To 1)

    For Each Column In MyColumns.Columns
        y = y + 1
        If y > 1 Then ReDim Preserve arData(1 To MyColumns.Rows.Count, 1 To y)
        For x = 1 To Column.Rows.Count
            arData(x, y) = Column.Rows(x)
        Next
    Next

    With Workbooks.Add().Worksheets(1)
        .Range("A1").Resize(UBound(arData, 1), UBound(arData, 2)) = arData
        .Columns.AutoFit
    End With
End Sub

【讨论】:

    【解决方案2】:

    试试这个(注释的)代码

    Option Explicit
    
    Sub CommandButton1_Click()
        Dim newSht As Worksheet
        Dim currCols As String
        Dim area As Range
        Dim iArea As Long
    
        Set newSht = Workbooks.add.Worksheets("Feuil1") '<--| add a new workbook and set its "Feuil1" worksheet as 'newSht'
        currCols = Replace(Application.InputBox("Select which column you want to copy from table (up to 10)", "Copy Columns", "A,B,F", , , , , 2), " ", "") '<--| get columns list
    
        With ThisWorkbook.Worksheets("Feuil1") '<--| reference worksheet "Feuil1" in the workbook this macro resides in
            For Each area In Intersect(.Range(ColumnsAddress(currCols)), .Range("A1:G28")).Areas ' loop through referenced worksheet areas of the range obtained by crossing its listed columns with its range "A1:G28"
                With area '<--| reference current area
                    newSht.Range("A1").Offset(, iArea).Resize(.Rows.Count, .Columns.Count).value = .value '<--| copy its values in 'newSht' current column offset from "A1" cell
                    iArea = iArea + .Columns.Count '<--| update current column offset from 'newSht' worksheet "A1" cell
                End With
            Next area
        End With
    End Sub
    
    Function ColumnsAddress(strng As String) As String
        Dim elem As Variant
    
        For Each elem In Split(strng, ",")
            ColumnsAddress = ColumnsAddress & elem & ":" & elem & ","
        Next
        ColumnsAddress = Left(ColumnsAddress, Len(ColumnsAddress) - 1)
    End Function
    

    【讨论】:

      【解决方案3】:

      我认为您可以将所有列复制到临时表,然后编写一些代码来删除无用的列。最后将表格粘贴到您预期的区域。

      【讨论】:

      • 没有解决方案只选择我想要复制的列并像他们一样成为联合而不是粘贴?
      • 如果你联合他们。假设您要复制第 5 列和第 8 列。您粘贴的结果可能在第 5 列和第 6 列,因为粘贴的区域必须是连续的。
      • 如果您认为可以,请为我的回答投票。谢谢。
      • 对不起,我正在旅行,无法访问互联网!我现在才明白!!我会尝试并给你反馈
      • 感谢陈,它运行良好!我只想保持每列和每行的格式和颜色相同!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      相关资源
      最近更新 更多