简体   繁体   English

VBA Excel将表复制并粘贴到新工作簿中,然后选择我要复制的列

[英]VBA Excel Copy and Paste a table into a new Workbook and choice which Columns i want to copy

I want to copy a table into a new Workbook while choosing which range I want to copy and knowing that the first Columns ("A") is automatically copied. 我想将表复制到新的工作簿中,同时选择要复制的范围并知道第一列(“ A”)是自动复制的。 (rows are not a problem, all of them have to be copied) For example, i have a table composed of 28 rows and 10 columns. (行不是问题,所有行都必须复制)例如,我有一个由28行和10列组成的表。 Added to A1:A28 (first columns, all rows),i want just to copy the column 5 and 8 with all its rows. 添加到A1:A28(第一列,所有行)中,我只想复制第5列和第8列及其所有行。 That's what i have until now but it doesn't work. 那是我到目前为止所拥有的,但是它不起作用。

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

Can you help please solving it? 您能帮忙解决吗? Thanks in advance! 提前致谢!

try this (commented) code 试试这个(注释)代码

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

You can't copy a non-continuous range but you can load the data into an array and write it once to the new workbook. 您不能复制非连续范围,但可以将数据加载到数组中并将其一次写入新工作簿。

在此处输入图片说明

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

I think you can copy all column to a temp sheet and then write some code to delete the useless column. 我认为您可以将所有列复制到临时表,然后编写一些代码以删除无用的列。 finally paste the table to your expected area. 最后将表格粘贴到您的预期区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA 添加新工作簿并复制/粘贴 - Excel VBA Add new Workbook and Copy/Paste Excel VBA-将两列从一个工作簿复制并粘贴到另一个工作簿 - Excel VBA - Copy and Paste two columns from one workbook into another Excel VBA:将列从工作簿复制到新工作簿 - Excel VBA: Copy columns from workbook to new workbook Excel,VBA:当1个条件应用于多个范围时,如何将粘贴数据复制到新工作簿中? - Excel, VBA: How can I copy paste data to new workbook when 1 conditional applying to multiple ranges? Excel VBA-将工作簿复制到带有宏的新工作簿中 - Excel VBA - Copy Workbook into a new Workbook with the macros Excel VBA代码,用于在一个工作簿中选择包含特定文本字符串的单元格,然后将这些单元格复制并粘贴到新工作簿中 - Excel VBA code to select cells that contain a certain text string in one workbook, and then copy and paste these cells into a new workbook 使用VBA将选定的列复制并粘贴到Excel中的表末尾 - Copy and Paste Selected Columns to End of Table in Excel with VBA Excel VBA:从另一个工作簿复制行并粘贴到主工作簿 - Excel VBA: Copy Row from another workbook and paste into master workbook 复制工作簿并粘贴到新工作簿中 - Copy workbook & paste into new workbook 我想同时将2个Excel工作表复制到一个新工作簿中 - I want to copy 2 Excel Worksheets at the same time to a new workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM