vba学习系列(4)-- index()提取指定单元格并保留字体格式

16 篇文章 6 订阅
文章详细描述了如何通过VBA编写程序,在Excel中当工作表1C列与工作表2B列中的姓名匹配时,从工作表2AK列提取对应数据,并保持源数据的字体颜色和格式一致。
摘要由CSDN通过智能技术生成

系列文章目录

一、目标需求

工作表2 B列中姓名,在工作表1 C列中存在相同姓名时,提取工作表2 AK列的对应单元格内容;
工作表2名称:OQC
工作表1名称:汇总

二、使用步骤

1.VBA程序

代码如下(示例):


Sub ExtractData()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rng1 As Range, rng2, rng3, rng4, sourceCell, destinationCell As Range, cell As Range
    Dim i, j As Integer
    Dim charColor As Long
    Dim sourceValue As String
    Dim charIndex As Integer
    Dim formattedText As String
    Dim fontColors() As Long
    Dim colorIndex As Integer
    Dim color As Integer

    
    '设置工作表1和工作表2
    Set ws1 = ThisWorkbook.Sheets("汇总")
    Set ws2 = ThisWorkbook.Sheets("OQC")
    '设置范围对象
    Set rng1 = ws1.Range("C:C")
    Set rng2 = ws2.Range("B:B")
    Set rng3 = ws1.Range("F3:F100")
    Set rng4 = ws2.Range("AK2:AK100")
    
    rng3.Interior.colorIndex = xlNone
    rng3.Font.colorIndex = xlNone
    rng3.Font.colorIndex = xlAutomatic
    
    '遍历工作表1的每个单元格
    For Each cell In rng1
        If cell.value <> "" Then
            '在工作表2的B列查找匹配项
            Set matchcell = rng2.Find(What:=cell.value, LookIn:=xlValues, LookAt:=xlWhole)
            If Not matchcell Is Nothing Then
            '如果找到匹配项,则提取工作表2对应的AK列数据
                a = matchcell.Row
                Set sourceCell = ws2.Cells(matchcell.Row, "AK")
                Set destinationCell = ws1.Cells(cell.Row, "F")
                Set targetCell = ws1.Cells(cell.Row, "F")
                destinationCell.value = ""
                
                'Text = sourceCell.value
                Set rngSource = ws2.Range("AK" & a)
                
                ReDim fontColors(0 To rngSource.Characters.Count)
                For j = 1 To sourceCell.Characters.Count
                    charColor = sourceCell.Characters(j, 1).Font.color
                    fontColors(j) = charColor
                Next j
                
                For j = 1 To sourceCell.Characters.Count
                    With destinationCell.Characters(j, 1).Font
                        color = fontColors(j)
                    End With
                Next j
                
                ws2.Range("AK" & a).Copy Destination:=ws1.Cells(cell.Row, "F")                    
                                               
                '定义要使用的颜色数组
                'Colors = Array(vbRed, vbBlue, vbGreen)
                
                'For i = 1 To sourceCell.Characters.Count
                    'Set charFormat = sourceCell.Characters(Start:=i, Length:=1).Font
                    'targetCell.Characters(Start:=i, Length:=1).Font.color = charFormat.color
                    'targetCell.Characters(Start:=i, Length:=1).Font.Bold = charFormat.Bold
                     ' 如果需要复制其他字体属性,比如Italic或Underline,可以继续添加代码行
                'Next i                                    
                
                'ReDim fontColors(1 To sourceCell.Characters.Count)
                'ReDim fontColors(2 To rngSource.Characters.Count)
                
                'destinationCell.value = sourceCell.value
                'sourceCell.Copy Destination:=destinationCell
                
                'destinationCell.Interior.color = sourceCell.Interior.color
                'destinationCell.Font.color = sourceCell.Font.color                                
            
               'ws1.Cells(cell.Row, "F").value = ws2.Cells(matchcell.Row, "AK").value
               'ws1.Cells(Cell.Row, "F").Font.Color = ws2.Cells(matchCell.Row, "AK").Font.Color
               'ws1.Cells(Cell.Row, "F").Font.ColorIndex = ws2.Cells(matchCell.Row, "AK").Font.ColorIndex
               'ws1.Cells(Cell.Row, "F").Interior.Color = ws2.Cells(matchCell.Row, "AK").Interior.Color
               'ws1.Cells(Cell.Row, "F").Interior.ColorIndex = ws2.Cells(matchCell.Row, "AK").Interior.ColorIndex
               
               'ws1.Cells(Cell.Row, "F").HorizontalAlignment = ws2.Cells(matchCell.Row, "AK").HorizontalAlignment
               'ws1.Cells(Cell.Row, "F").VerticalAlignment = ws2.Cells(matchCell.Row, "AK").VerticalAlignment
               'ws1.Cells(Cell.Row, "F").Borders.LineStyle = ws2.Cells(matchCell.Row, "AK").Borders.LineStyle
               'ws1.Cells(Cell.Row, "F").Borders.Color = ws2.Cells(matchCell.Row, "AK").Borders.Color
                                         
               'ws1.Cells(Cell.Row, "F").Font.Name = ws2.Cells(matchCell.Row, "AK").Font.Name
               'ws1.Cells(Cell.Row, "F").Font.Size = ws2.Cells(matchCell.Row, "AK").Font.Size
               'ws1.Cells(Cell.Row, "F").Font.Bold = ws2.Cells(matchCell.Row, "AK").Font.Bold
               'ws1.Cells(Cell.Row, "F").Font.Italic = ws2.Cells(matchCell.Row, "AK").Font.Italic                        
          
            End If
        End If
    Next cell
End Sub

2.VBA简要程序


Sub ExtractData()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rng1 As Range, rng2, rng3, rng4, sourceCell, destinationCell As Range, cell As Range
    Dim i, j As Integer
    Dim charColor As Long
    Dim sourceValue As String
    Dim charIndex As Integer
    Dim formattedText As String
    Dim fontColors() As Long
    Dim colorIndex As Integer
    Dim color As Integer

    
    '设置工作表1和工作表2
    Set ws1 = ThisWorkbook.Sheets("汇总")
    Set ws2 = ThisWorkbook.Sheets("OQC3")
    '设置范围对象
    Set rng1 = ws1.Range("C:C")
    Set rng2 = ws2.Range("B:B")
    Set rng3 = ws1.Range("F3:F100")
    Set rng4 = ws2.Range("AK2:AK100")
    
    rng3.Interior.colorIndex = xlNone
    rng3.Font.colorIndex = xlNone
    rng3.Font.colorIndex = xlAutomatic
    
    '遍历工作表1的每个单元格
    For Each cell In rng1
        If cell.value <> "" Then
            '在工作表2的B列查找匹配项
            Set matchcell = rng2.Find(What:=cell.value, LookIn:=xlValues, LookAt:=xlWhole)
            If Not matchcell Is Nothing Then
            '如果找到匹配项,则提取工作表2对应的AK列数据
                a = matchcell.Row
                Set sourceCell = ws2.Cells(matchcell.Row, "AK")
                Set destinationCell = ws1.Cells(cell.Row, "F")
                Set targetCell = ws1.Cells(cell.Row, "F")
                destinationCell.value = ""
                
                Set rngSource = ws2.Range("AK" & a)
                
                ReDim fontColors(0 To rngSource.Characters.Count)
                For j = 1 To sourceCell.Characters.Count
                    charColor = sourceCell.Characters(j, 1).Font.color
                    fontColors(j) = charColor
                Next j
                
                For j = 1 To sourceCell.Characters.Count
                    With destinationCell.Characters(j, 1).Font
                        color = fontColors(j)
                    End With
                Next j
                
                ws2.Range("AK" & a).Copy Destination:=ws1.Cells(cell.Row, "F")
                                                
            End If
        End If
    Next cell
End Sub


总结

分享:
接受可以让我面对所有的问题,当我感到焦虑的时候,通常是因为我发现自己不能接受生活中的一些人、地方、事情,直到我完全接受了它们,我才能获得心灵上的安宁。除非我完全的接受生活,否则我将无法获得快乐。我不需要再纠结这个世界上有什么需要改变而是关注我自己的态度需要发生怎样的改变;

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若竹之心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值