简体   繁体   English

使用 VBA 将列复制并粘贴到新工作表

[英]Using VBA to Copy and Paste Columns to New sheet

I'm automating a few things for my job to try and create tables from data faster but we have to first take columns from our raw data and paste them to a new sheet as there is extraneous stuff that comes off the instrument.我正在为我的工作自动化一些事情,以尝试更快地从数据创建表格,但我们必须首先从原始数据中取出列并将它们粘贴到新工作表中,因为仪器会产生无关的东西。 Currently I do it manually as shown below but I want to run a script that automatically pulls columns with: um2,mm2,#, measurement, or treatment in the header.目前我手动执行如下所示,但我想运行一个脚本,自动拉列:um2,mm2,#,测量,或标题中的处理。 These would be part of a string and the desired column titles would vary minus containing one of the things I just listed.这些将是字符串的一部分,所需的列标题会有所不同,减去我刚刚列出的内容之一。 I was thinking of using either InStr to automatically sort through and select the columns or just prompting userinput to select the appropriate ones.我正在考虑使用 InStr 来自动排序和选择列,或者只是提示用户输入选择适当的列。 I prefer the automated if possible.如果可能的话,我更喜欢自动化。

Sub CopyHeaders()
    Dim header As Range, headers As Range
    Set headers = Worksheets("Data").Range("A1:Z1")

    For Each header In headers
        If GetHeaderColumn(header.Value) > 0 Then
            Range(header.offset(1, 0), header.End(xlDown)).Copy Destination:=Worksheets("Raw Data").Cells(2, GetHeaderColumn(header.Value))
        End If
    Next
End Sub

Function GetHeaderColumn(header As String) As Integer
    Dim headers As Range
    Set headers = Worksheets("Raw Data").Range("A1:Z1")
    GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function

Was thinking of maybe using this to try and automate it but was unsure how to use the range I want.正在考虑使用它来尝试自动化,但不确定如何使用我想要的范围。

Sub Test()
    Worksheets.FillAcrossSheets Sheet1.[A1:M1]
End Sub

I have attached an example of the headers, but the number of columns and titles will vary for each data set.我附上了一个标题示例,但每个数据集的列数和标题数会有所不同。 Thanks!谢谢!

Header Examples标题示例

credit for above code: https://www.mrexcel.com/board/threads/vba-to-merge-data-from-multiple-sheets-along-with-sheet-name.1171178/以上代码的功劳: https : //www.mrexcel.com/board/threads/vba-to-merge-data-from-multiple-sheets-along-with-sheet-name.1171178/

Try something like this:尝试这样的事情:

Sub CopyHeaders()
    Dim header As Range, headers As Range, ws As Worksheet, cDest As Range
    
    Set cDest = Worksheets("Copied data").Range("a1") 'first paste destination
    Set ws = Worksheets("Data")
    Set headers = ws.Range("A1:Z1").Cells
    
    For Each header In headers
        If Len(header.Value) > 0 Then
            If CopyThis(header.Value) Then
                ws.Range(header, ws.Cells(ws.Rows.Count, header.Column).End(xlUp)).Copy _
                    cDest
                Set cDest = cDest.Offset(0, 1) 'move one column over
            End If
        End If
    Next
End Sub

'copy this header and data?
Function CopyThis(hdr As String) As Boolean
    Dim v
    For Each v In Array("treatment", "measurement", "um2", "mm2", "#")
        If InStr(1, hdr, v, vbTextCompare) > 0 Then
            CopyThis = True
            Exit For
        End If
    Next v
End Function

Are you going to be appending data?你要追加数据吗? If not, then this should work and it's pretty close to your code.如果没有,那么这应该可以工作,并且与您的代码非常接近。

Sub CopyHeaders()
    Dim wsData As Worksheet, wsPaste As Worksheet
    Set wsData = Worksheets("Data")
    Set wsPaste = Worksheets("Raw Data")
    
    Dim header As Range, headers As Range
    Set headers = wsData.Range("A1:Z1")
    
    For Each header In headers.Cells
        If IsEmpty(header) Then
        
        'do nothing
        ElseIf testHeaderForMatch(header.Value) Then
            
            If GetHeaderColumn(header.Value) = 0 Then
                Dim lastColumn As Long
                lastColumn = wsPaste.Cells(1, Columns.Count).End(xlToLeft).Column
                wsPaste.Cells(1, lastColumn + 1).Value = header.Value
            End If
                
        wsData.Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=wsPaste.Cells(2, GetHeaderColumn(header.Value))
   
        End If
    Next
End Sub

Function GetHeaderColumn(header As String) As Integer
    Dim headers As Range
    Set headers = Worksheets("Raw Data").Range("A1:Z1")
    GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function

Function testHeaderForMatch(theText As String) As Boolean
Dim theValidMembers As String
    theValidMembers = "um2, mm2, measurement,treatment"
Dim myTextArray() As String
    myTextArray = Split(theValidMembers, ",")

Dim i As Long
For i = LBound(myTextArray) To UBound(myTextArray)
    If theText = myTextArray(i) Then
        testHeaderForMatch = True
        Exit Function
    End If
Next i

End Function

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

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