VBA查找并替换,但为整个列着色
我对VBA相当陌生(因为这是我第一次尝试使用VBA),我正在编写一个宏,以便在名为“AA SERIES”的电子表格中查找大量数字,并将其替换为稍加修改的数字。我将这些存储在一个名为“PartNumber”的电子表格中,现有的存储在第I列中,替换的存储在J中。下面显示的代码适用于此:VBA查找并替换,但为整个列着色,vba,excel,Vba,Excel,我对VBA相当陌生(因为这是我第一次尝试使用VBA),我正在编写一个宏,以便在名为“AA SERIES”的电子表格中查找大量数字,并将其替换为稍加修改的数字。我将这些存储在一个名为“PartNumber”的电子表格中,现有的存储在第I列中,替换的存储在J中。下面显示的代码适用于此: Sub Macro1() Dim i As Integer Dim WS As Worksheet Dim FindStr As String Dim RepStr As String For i
Sub Macro1()
Dim i As Integer
Dim WS As Worksheet
Dim FindStr As String
Dim RepStr As String
For i = 1 To 87
For Each WS In Workbooks("AA SERIES").Worksheets
FindStr = Workbooks("PartNumbers").Sheets("Sheet1").Range("I" & i).Value
RepStr = Workbooks("PartNumbers").Sheets("Sheet1").Range("J" & i).Value
Cells.Replace What:=FindStr, Replacement:=RepStr
Next
Next i
End Sub
但是,如果宏替换了列中的值,我希望它也将整个列格式化为不同的颜色(理想情况下为浅紫色)。目标是让下一个使用此工作表的人能够快速滚动浏览并查看更改的位置
有什么建议吗?我想这就是你想要的:
目前无法验证。。。但我认为它是这样工作的:
Application.ReplaceFormat.Interior.Color = RGB(200, 150, 200)
Cells.Replace What:=FindStr, Replacement:=RepStr, SearchFormat:=False, ReplaceFormat:=True
编辑要为整个列着色,必须搜索浅紫色单元格,并对找到的每个单元格的整个单元格应用颜色更改
Application.FindFormat.Interior.Color = RGB(200, 150, 200)
Cells.Find(SearchFormat:=True).EntireColumn.Interior.Color = RGB(200, 151, 200)
'slightly changed colorcode to avoid endless loop, if you want to loop through all changed cells
要扩展上一个答案,请执行以下操作:
选项显式
第1分段()
将整列常量设置为Byte=0'更改为1以给整列着色
作为整数的Dim i
将ws设置为工作表
作为字符串的Dim findStr
Dim repStr作为字符串
暗淡的波浪和长的波浪一样
作为射程找到的昏暗
先调暗为弦
lPurple=RGB(244233255)
Application.ReplaceFormat.Interior.Color=lPurple
对于工作簿(“AA系列”)中的每个ws。工作表
对于i=1到9
带有工作手册(“零件号”).工作表(“表1”)
findStr=.Range(“I”&I).Value
repStr=.Range(“J”&i).Value
ws.UsedRange.Replace What:=findStr_
替换:=repStr_
ReplaceFormat:=True
如果整列=1,则
使用ws.UsedRange
Set found=.Find(What:=repStr,SearchOrder:=xlByRows)
如果找不到,那就什么都没有了
first=找到。地址
做
如果找到。偏移量(1)。内饰。颜色L曲线,然后
.Columns(found.Column).Interior.Color=lPurple
如果结束
Set found=.FindNext(已找到)
未找到时的循环为Nothing并已找到。地址优先
如果结束
以
如果结束
以
下一个
下一个
端接头
您是要给整列还是整行上色?如果你在行中循环,列就不会有多大用处。学习VBA的最好方法是记录一个宏,它可以做你想做的事情,然后根据你的实际需要调整它。通过这种方式学习将使你成为VBA天才。Pi说的是实话。这就是我建议很多人开始学习的方式。一旦你掌握了诀窍,你会看到/做一些你不喜欢的事情,比如录制功能的工作方式,并会继续改进。我同意,使用Pi的方法会教你需要什么设置以及如何访问它们。它还将教给您可怕的编码实践,因此花一些时间学习更好的编码实践是非常值得的。一个开始阅读的好地方是。
Option Explicit
Sub replace1()
Const ENTIRE_COLUMN As Byte = 0 'Change to 1 to color the entire columns
Dim i As Integer
Dim ws As Worksheet
Dim findStr As String
Dim repStr As String
Dim lPurple As Long
Dim found As Range
Dim first As String
lPurple = RGB(244, 233, 255)
Application.ReplaceFormat.Interior.Color = lPurple
For Each ws In Workbooks("AA SERIES").Worksheets
For i = 1 To 9
With Workbooks("PartNumbers").Sheets("Sheet1")
findStr = .Range("I" & i).Value
repStr = .Range("J" & i).Value
ws.UsedRange.Replace What:=findStr, _
Replacement:=repStr, _
ReplaceFormat:=True
If ENTIRE_COLUMN = 1 Then
With ws.UsedRange
Set found = .Find(What:=repStr, SearchOrder:=xlByRows)
If Not found Is Nothing Then
first = found.Address
Do
If found.Offset(1).Interior.Color <> lPurple Then
.Columns(found.Column).Interior.Color = lPurple
End If
Set found = .FindNext(found)
Loop While Not found Is Nothing And found.Address <> first
End If
End With
End If
End With
Next
Next
End Sub