Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 621|回复: 1

在2个工作表如何复制格式到另外一个工作表

[复制链接]
发表于 2023-3-3 09:29 | 显示全部楼层 |阅读模式
请问各位大神,如何把工作表1中的第2581114栏,找出单元格为蓝底白字的储存格,然后复制它的格式,在工作表2贴上相对应的单元格,谢谢

活頁簿10.zip

11.48 KB, 下载次数: 2

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2023-3-5 14:36 | 显示全部楼层
请各位大神, 看看这里是错的, 要如何改, 谢谢!
这个vba是把工作表1中第2,5,8,11栏中有颜色的单元格,复制其格式,然后到工作表2第1栏至第5栏找出与工作表1相同的数值, 然后贴上其格式


Sub CopyFormats()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rng1 As Range, rng2 As Range, cell1 As Range, cell2 As Range

    Set ws1 = ThisWorkbook.Worksheets("工作表1")
    Set ws2 = ThisWorkbook.Worksheets("工作表2")

    For Each rng1 In Array(ws1.Range("B1:B" & ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row), _
                           ws1.Range("E1:E" & ws1.Cells(ws1.Rows.Count, "E").End(xlUp).Row), _
                           ws1.Range("H1:H" & ws1.Cells(ws1.Rows.Count, "H").End(xlUp).Row), _
                           ws1.Range("K1:K" & ws1.Cells(ws1.Rows.Count, "K").End(xlUp).Row), _
                           ws1.Range("N1:N" & ws1.Cells(ws1.Rows.Count, "N").End(xlUp).Row))

        For Each cell1 In rng1.Cells
            If cell1.Interior.ColorIndex <> xlNone Then
                Set rng2 = ws2.Range("A1:E" & ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row)
                For Each cell2 In rng2.Cells
                    If cell2.Interior.ColorIndex = cell1.Interior.ColorIndex Then
                        cell2.Copy
                        cell1.PasteSpecial xlPasteFormats
                        Exit For
                    End If
                Next cell2
            End If
        Next cell1

    Next rng1

    Application.CutCopyMode = False

End Sub


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-28 10:02 , Processed in 0.124148 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表