【问题标题】:Copy/Paste Yellow Highlighted Cells in a new WorkSheet VBA在新的 WorkSheet VBA 中复制/粘贴黄色突出显示的单元格
【发布时间】:2019-07-28 08:04:38
【问题描述】:

我正在努力完成这项工作。

这个宏应该打开一个工作簿(工作簿名称总是会改变,并且总是只有一张表要处理)。这行得通。

设置整个工作表的范围;工作正常。

然后在整个工作表中搜索以黄色突出显示的单元格,然后将这些单元格复制到新工作表中......这就是我需要帮助的地方!

我对 VBA 真的很陌生,这就是我目前所拥有的:

Option Explicit

Sub test3()
    Dim data As Variant
    Dim rngTemp As Range
    Dim cell As Range

    '//open Workbook
    data = Application.GetOpenFilename(, , "Open Workbook")
    Workbooks.Open data


    '// set Range ( Whole Sheet)
    Set rngTemp = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    If Not rngTemp Is Nothing Then
        Range(Cells(1, 1), rngTemp).Select
    End If

    '// Search for Yellow highlighted Cells and (if you find one)
    '// Copy Cell B1 + the 3rd Cell in the column (of the highlighted Cell) + the value highlighted Cell
    '// and paste in new Sheet
        For Each cell In rngTemp.Cells
            If rngTemp.Interior.ColorIndex = 6 Then
                cell.Select
                Selection.Copy
                Sheets.Add
                Range("A1").PasteSpecial
                Application.CutCopyMode = False
            End If
        Next
End Sub

【问题讨论】:

  • 您需要将每个突出显示的单元格放在不同的工作表上吗?都在同一张纸上?每行?每列?
  • 您正在循环通过 rngTemp 这只是一个单元格。
  • 不,我需要每列一张表中所有突出显示的单元格

标签: excel vba copy-paste


【解决方案1】:
 Sub test3()
    Dim wbName As string
    Dim rngTemp As Range
    Dim r As Range
    DIM TARGETSHEET AS WORKSHEET
    DIM TARGET AS RANGE
    '//open Workbook
    wbName = Application.GetOpenFilename(, , "Open Workbook")
    if  wbName = "" or wbname = "CANCEL" then exit sub
    Workbooks.Open wbname


    '// set Range ( Whole Sheet)
    Set rngTemp = Activesheet.usedrange
    SET TARGETSHEET = ACTIVEWORKBOOK.WORKSHEETS.ADD()
    SET TARGET = TARGETSHEET.RANGE("A1")  
'// Search for Yellow highlighted Cells and (if you find one)
    '// Copy Cell B1 + the 3rd Cell in the Column (of the highlighted Cell) + the value highlighted Cell
    '// and paste in new Sheet

        For Each r In rngTemp
            If r.Interior.ColorIndex = 6 Then


                TARGET = rngtemp.parent.range("B1")
                TARGET.OFFSET(0,1) = r
                TARGET.OFFSTE(0,2) = rngtemp.parent.cells(3,r.column)
       'I've assumed you want them across the first row
                SET TARGET = TARGET.OFFSET(1,0)
            End If
        Next r
       End Sub

【讨论】:

  • 首先感谢您的回复...此代码有效,但它会生成包含复制数据的表格数量...我想要的是所有复制的数据都在一张表格中...我想我当时解释错了:D
  • 太棒了。谢谢:D
猜你喜欢
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 2018-05-21
  • 2019-07-02
相关资源
最近更新 更多