简体   繁体   English

Excel VBA:将列从工作簿复制到新工作簿

[英]Excel VBA: Copy columns from workbook to new workbook

I'm not really great at coding so as much help as possible would be incredible. 我不是很擅长编码,所以尽可能多的帮助将是不可思议的。 Basically here's what I want to do. 基本上,这就是我想要做的。

  1. Export CSV from Website (No code required) 从网站导出CSV(无需代码)
  2. Open CSV in Excel (No code required) 在Excel中打开CSV(无需代码)
  3. Automatically remove rows that have a blank cell in certain column (Already coded) 自动删除某些列中具有空白单元格的行(已编码)
  4. Copy specific columns (ignoring header rows) to another workbook in specific order. 按特定顺序将特定列(忽略标题行)复制到另一个工作簿。

Column order is as follows: (S1 = Open CSV || S2 = New Workbook) 列顺序如下:(S1 =打开CSV || S2 =新工作簿)

  • S1.V = S2.A S1.V = S2.A
  • S1.B = S2.D S1.B = S2.D
  • S1.F = S2.V S1.F = S2.V
  • S1.H = S2.X S1.H = S2.X
  • S1.I = S2.J S1.I = S2.J
  • S1.L = S2.B S1.L = S2.B

Step 3's code: 步骤3的代码:

Columns("V:V").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete

There is a lot to consider when doing what you require, I have made some assumptions that you will need to code for if they are incorrect: - 在做您需要做的事情时,有很多要考虑的地方,我做了一些假设,如果它们不正确,则需要进行编码:-

  • The destination already exists 目标已经存在
  • The destination has headers on row 1 but no content 目标行1上有标头,但没有内容
  • The destination is simply the first sheet in the destination workbook 目标只是目标工作簿中的第一张表
  • The source header row is row 1 源头行是第1行

Sample code: - 示例代码:-

Public Sub Sample()
Dim StrDestPath As String
Dim WkBk_Dest   As Workbook
Dim WkBk_Src    As Workbook
Dim WkSht_Dest  As Worksheet
Dim WkSht_Src   As Worksheet

'A reference to the destination
StrDestPath = "C:\Users\Gary\Desktop\Destination.xlsx"

'Connect to the source
Set WkBk_Src = ThisWorkbook
Set WkSht_Src = WkBk_Src.Worksheets(1)

'See if the destination is open already
For Each WkBk_Dest In Application.Workbooks
    If WkBk_Dest.FullName = StrDestPath Then Exit For
Next

'If it wasn't then open it
If WkBk_Dest Is Nothing Then
    Set WkBk_Dest = Application.Workbooks.Open(StrDestPath)
End If

'Connect to the destination
Set WkSht_Dest = WkBk_Dest.Worksheets(1)

'Per column mapping - Copy everythng from row 2 (assuming headers are on row 1 down to the last populated cell in that column
'and paste it into the required column in the destination
WkSht_Src.Range("V2:" & WkSht_Src.Range("V2").End(xlDown).Address).Copy WkSht_Dest.Range("A2")
WkSht_Src.Range("B2:" & WkSht_Src.Range("B2").End(xlDown).Address).Copy WkSht_Dest.Range("D2")
WkSht_Src.Range("F2:" & WkSht_Src.Range("F2").End(xlDown).Address).Copy WkSht_Dest.Range("V2")
WkSht_Src.Range("H2:" & WkSht_Src.Range("H2").End(xlDown).Address).Copy WkSht_Dest.Range("X2")
WkSht_Src.Range("I2:" & WkSht_Src.Range("I2").End(xlDown).Address).Copy WkSht_Dest.Range("J2")
WkSht_Src.Range("L2:" & WkSht_Src.Range("L2").End(xlDown).Address).Copy WkSht_Dest.Range("B2")

'Disconnect from destination worksheet
Set WkSht_Dest = Nothing

'save changes
WkBk_Dest.Save

'disconnect from destination workbook
Set WkBk_Dest = Nothing

'Disconnect from source
Set WkSht_Src = Nothing
Set WkBk_Src = Nothing

End Sub

I have also assumed the source to be the workbook I was coding in, this won't be possible in a CSV file so you might want to open it in the same way the destination is checked for and then opened, you also may want to add a flag to close them when done if they were not opened to begin with. 我还假设源是我正在编码的工作簿,因此在CSV文件中将无法实现,因此您可能希望以检查目标然后打开目标的方式打开它,您可能还想如果开始时未打开它们,请添加一个标志以在完成后将其关闭。

Finally, if the destination already has data use the .end function as shown in the sample to get the the last row. 最后,如果目的地已经有数据,请使用如示例中所示的.end函数来获取最后一行。

since you're working from CSV file, you don't have formats to carry along 由于您使用的是CSV文件,因此没有可携带的格式

therefore simple values pasting is what you need 因此,您需要简单的值粘贴

try this 尝试这个

Option Explicit
Sub CopyColumnsToAnotherWB(sourceWS As Worksheet, targetWs As Worksheet, sourceCols As String, targetCols As String)
    Dim sourceColsArr As Variant, targetColsArr As Variant
    Dim iCol As Long, nCols As Long

    sourceColsArr = Split(Application.WorksheetFunction.Trim(sourceCols), ",") '<--| make array out of string with delimiter
    targetColsArr = Split(Application.WorksheetFunction.Trim(targetCols), ",") '<--| make array out of string with delimiter

    nCols = UBound(sourceColsArr) '<--| count columns number to copy/paste
    If nCols <> UBound(targetColsArr) Then Exit Sub  '<--| exit if the two columns list haven't the same number of columns
    With sourceWS
        For iCol = 0 To nCols '<--|loop through source sheet columns
            With .Cells(1, sourceColsArr(iCol)).Resize(.Cells(.Rows.Count, sourceColsArr(iCol)).End(xlUp).Row)
                targetWs.Columns(targetColsArr(iCol)).Resize(.Rows.Count).value = .value  '<--|paste values to corresponding target sheet column
            End With
        Next iCol
    End With
End Sub

which you can exploit as follows 您可以如下利用

Option Explicit 显式期权

Sub main()

    Dim sourceCols As String, targetCols As String

    sourceCols = "V,B,F,H,I,L"
    targetCols = "A,D,V,X,J,B"
    CopyColumnsToAnotherWB ActiveWorkbook.ActiveSheet, Workbooks("columntest").Worksheets("test"), sourceCols, targetCols

End Sub

just change ActiveWorkbook.ActiveSheet and Workbooks("columntest").Worksheets("test") to your actual source and target workbooks and worksheets 只需将ActiveWorkbook.ActiveSheetWorkbooks("columntest").Worksheets("test")更改为实际的源和目标工作簿和工作表

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM