【问题标题】:Excel VBA Macro--Search For Column names and then copy into defined columns on another template worksheet in same workbook Excel 2010Excel VBA 宏 - 搜索列名,然后复制到同一工作簿 Excel 2010 中另一个模板工作表上的已定义列
【发布时间】:2012-09-18 17:11:40
【问题描述】:

我似乎无法让它工作,我看不出哪里有问题。

它编译得很好,但它在我的工作表上什么也没做。我正在尝试编写一个宏,该宏将按列标题复制数据并粘贴到具有相同标题的同一工作簿中的另一个模板表中。

例如,复制导入表“开始时间”列下的数据,复制新数据,粘贴到主表“开始时间”列。

Sub CopyByHeader()

Dim shtImport As Worksheet, shtMain As Worksheet
Dim c As Range, f As Range
Dim rngCopy As Range, rngCopyTo

Set shtImport = ActiveSheet ' "import" - could be different workbook
Set shtMain = ThisWorkbook.Sheets("Main")

For Each c In Application.Intersect(shtImport.UsedRange, shtImport.Rows(1))
    'only copy if >1 value in this column (ie. not just the header)
    If Len(c.Value) > 0 And Application.CountA(c.EntireColumn) > 1 Then
        Set f = shtMain.Rows(1).Find(what:=c.Value, LookIn:=xlValues, _
        LookAt:=xlWhole)
        If Not f Is Nothing Then
            Set rngCopy = shtImport.Range(c.Offset(1, 0), _
                shtImport.Cells(Rows.Count, c.Column).End(xlUp))
            Set rngCopyTo = shtMain.Cells(Rows.Count, _
                f.Column).End(xlUp).Offset(1, 0)
            'copy values
            rngCopyTo.Resize(rngCopy.Rows.Count, 1).Value = rngCopy.Value
        End If
    End If
 Next c

 End Sub

我改成这个了,超级慢……有什么想法吗??:

Sub ImportTimeStudy()
Dim myHeaders, e, x, wsImport As Worksheet, wsMain As Worksheet
Dim r As Range, c As Range

myHeaders = Array(Array("Time Started", "Time Started"), Array("Description of the task", "Description of the task"), Array("Level", "Level"), Array("Location", "Location"), Array("Targeted", "Targeted"), Array("System", "System"), Array("Process Code", "Process Code"), _
            Array("Value Stream", "Value Stream"), Array("Subject", "Subject"), Array("BU", "BU"), Array("Task Duration", "Task Duration"), Array("Activity Code", "Activity Code"))

Set wsImport = Sheets("Import")
Set wsMain = Sheets("Main")

For Each e In myHeaders

    Set r = wsImport.Cells.Find(e(0), , , xlWhole)

    If Not r Is Nothing Then
        Set c = wsMain.Cells.Find(e(1), , , xlWhole)

        If Not c Is Nothing Then
            wsImport.Range(r.Offset(1), wsImport.Cells(Rows.Count, r.Column).End(xlUp)).Copy _
            wsMain.Cells(Rows.Count, c.Column).End(xlUp)(2)
        Else
            msg = msg & vbLf & e(1) & " " & wsMain.Name
        End If
    Else
        msg = msg & vbLf & e(0) & " " & wsImport.Name
    End If

Next

If Len(msg) Then
    MsgBox "Header not found" & msg

End If

Application.ScreenUpdating = False

End Sub

【问题讨论】:

  • 请注意,可变尺寸似乎不正确。当您键入 Dim rngCopy As Range, rngCopyTo 时,只有第一个 rngCopy 被声明为范围,您将第二个 rngCopyTo 声明为变体类型变量。这不会直接影响您的问题,但应该注意,因为这是一个坏习惯,会导致您在编码过程中遇到麻烦。
  • 您是否尝试过单步执行代码?如果它没有复制任何数据,那么您的某些测试没有按照您的预期进行。

标签: vba excel


【解决方案1】:

我将您的循环重写为 2 个for 循环,试试这个: (cmets in-line)

Sub CopyByHeader()


Dim shtImport As Worksheet
Dim shtMain As Worksheet
Set shtImport = ActiveSheet ' "import" - could be different workbook
Set shtMain = ThisWorkbook.Sheets("Main")

Dim lCopyColumn As Long
Dim lCopyRow As Long
Dim lLastRowOfColumn As Long

'- for each column in row 1 of import sheet
For lCopyColumn = 1 To shtImport.Cells(1, shtImport.Columns.Count).End(xlToLeft).Column
    '- check what the last row is with data in column
    lLastRowOfColumn = shtImport.Cells(shtImport.Rows.Count, lCopyColumn).End(xlUp).Row

    'if last row was larger than one then we will loop through rows and copy
    If lLastRowOfColumn > 1 Then
        For lCopyRow = 1 To lLastRowOfColumn
            '- note we are copying to the corresponding cell address, this can be modified.
            shtMain.Cells(lCopyRow, lCopyColumn).Value = shtImport.Cells(lCopyRow, lCopyColumn).Value
        Next lCopyRow
    End If
Next lCopyColumn

End Sub

【讨论】:

  • 这很好用,唯一的问题是我在“Main”上的标题被覆盖了。有没有办法规定信息的粘贴位置?该模板适用于第 6 行以下的任何内容,因此需要粘贴到第 7 行。如果您能提供帮助,谢谢!
  • 在粘贴行中添加一个加号 6,使其看起来像这样 shtMain.Cells(lCopyRow+6, lCopyColumn).Value = shtImport.Cells(lCopyRow, lCopyColumn).Value 我不是 100% 了解电子表格标题的布局方式,但如果您需要抵消一些东西要去,在那里做。
  • 我现在遇到的问题是,如果工作表上的“导入”选项卡的列与模板的顺序不同,它只会导入到错误的列中。 (例如,如果将数据粘贴到主工作表中,但“开始时间列”移动到另一列,则会将“开始时间”粘贴到错误的列下)...有什么方法可以将数据与列标题匹配两张纸之间? vlookup 是最佳选择吗?
  • 现在lLastRowOfColumn 循环遍历输入列并将它们直接粘贴到相同位置的输出。如果您需要动态查找粘贴位置,我建议您在目标工作表的标题行中使用 for 循环来查找哪个标题与您要将行粘贴到的列匹配。
  • @RCoy1978 需要查看您的新代码以评论如何优化它以提高速度。也许发布一个新问题?
猜你喜欢
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
相关资源
最近更新 更多