excel - 在工作簿的所有工作表中查找特定的列标题

标签 excel vba

我正在尝试创建一个宏,它将浏览工作簿中的所有工作表并找到名为“ID”的列。大多数工作表中都会有一个“ID”列,但标题不一定位于第 1 行。找到该列后,我想将该列中的所有数据复制到新工作表中。将数据复制到新工作表时,我希望将数据全部复制到新工作表的 A 列中 - 因此希望将数据复制到下一个空白单元格中。到目前为止,这就是我所得到的

Sub Test()

Dim ws As Worksheet
Dim sString As String
Dim sCell As Variant
Dim cfind As Range
Dim j As Integer

  For Each ws In Worksheets
    If ws.Name = "Archive" Then GoTo nextws
    ws.Activate
    j = ActiveSheet.Index
    'MsgBox j
    On Error Resume Next
    Set cfind = Cells.Find(what:="ID", lookat:=xlWhole)
    If Not cfind Is Nothing Then
      cfind.EntireColumn.Copy
      Worksheets("Archive").Range("A1").Offset(0, j - 1).PasteSpecial
    End If
  nextws:
  Next ws

End Sub

我似乎无法正确粘贴数据的最后一点。目前,它只是将其粘贴到下一个可用列中。

最佳答案

那么,您希望将所有内容都放在 A 列中,对吧?

更改为

With Worksheets("Archive")
  If .Range("A1") = "" Then
    .Range("A1").PasteSpecial
  Else
    .Range("A1").Offset(.UsedRange.Rows.Count).PasteSpecial
  End If
End With

来自

Worksheets("Archive").Range("A1").Offset(0, j - 1).PasteSpecial

关于excel - 在工作簿的所有工作表中查找特定的列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6074182/

相关文章:

excel - 如何在行数不等的水平和垂直数据中进行 COUNTIFS?

excel - 如何删除单元格右下角的这个小 "right angle"?

performance - VBA 循环效率

Excel 应用程序事件 : How to automatically reset WithEvents variable after a runtime error?

Excel在单元格中显示无限重复的值

javascript - 如何仅提取下面示例代码中的数字 1780

vba - 由于(可能)数据验证导致 Excel 崩溃

sql - 在VBA过程中使用ADODB查询Excel文件时,多个JOIN不可用吗?

vba - Excel-VBA : Getting the values from Form Controls

excel - 使用 Delphi 的 OleObject 在 Office 365 和 Office 2013 中的工作方式不同