我想要以下VBA代码:
我有两列完整的值。我想取这些列,并从第一个值开始,检查值是否在另一列中的任何位置。如果找到该值,我想突出显示整行。然后使用第一个范围中的第二个值重新启动,依此类推。这两列是在不同的表格上。
例如,这里是列
Worksheet1 A1: 1234 A2: 2345 A3: 3456 etc... Worksheet2 A1: 1111 A2: 2345 A3: 3333 etc...
我希望vba代码检查工作表2列A中是否有“1234”,如果是,则突出显示黄色的整行。然后为“2345”做同样的事。
答案 0 :(得分:0)
你好TimeX24我只是会帮助你,但你现在可以做的其余部分我可以做的是一个按钮是什么做的是需要sheet1单元格A1而不是在sheet2上搜索它将弹出列A如果找到消息框您可以删除该代码并添加您自己我只是不想完成所有工作但这将帮助您入门。
Sub Button1_Click()
Dim ToBeFound As String
Dim rng As Range
Sheets("Sheet1").Activate
Range("A1").Activate
For x = 1 To 1000
ToBeFound = ActiveCell.Value
If ActiveCell.Value = "" Then
Exit Sub
Else
Sheets("Sheet2").Activate
Range("A1").Activate
Set rng = Cells.Find(What:=ToBeFound, LookIn:=xlValues, lookat:=xlPart)
If IIf(rng Is Nothing, "", rng) = "" Then
Sheets("Sheet1").Activate
ActiveCell.Offset(1, 0).Activate
Else
'This is where you can add some of your
'Own code to get what you want.
MsgBox ("Found " & ToBeFound)
Sheets("Sheet1").Activate
ActiveCell.Offset(1, 0).Activate
End If
End If
Next x
End Sub
答案 1 :(得分:0)
试试这个:
Sub marine()
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("Sheet1"): Set sh2 = Sheets("Sheet2")
Dim c As Range, sh1rng As Range, rngTofill As Range
With sh1
Set sh1rng = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
End With
Dim arr As Variant
With Application
arr = .Transpose(sh2.Range("A1", sh2.Range("A" & _
sh2.Rows.Count).End(xlUp)))
For Each c In sh1rng
If Not IsError(.Match(c,arr,0)) Then
If rngTofill Is Nothing Then
Set rngTofill = c
Else
Set rngTofill = Union(rngTofill, c)
End If
End If
Next
End With
rngTofill.EntireRow.Interior.Color = RGB(255, 255, 0)
End Sub
改进编码并理解代码的大部分内容read this 如果您仍有疑问,请将其评论出来。 HTH。
是的,总是第一次发帖。但下次确保在您的问题中提供相关信息,特别是您尝试过的事情。