我有一个测试要求,它在两个标签中有很多行数据。
Sheet1中的一行是基于Employee Name引入Sheet2并插入到具有匹配的Employee Name的行下面。 例如:
Employee Name Salary Rent Loan
Adler, Jorge $10,000 $2,000 $15,000
Adler, Jorge $10,000 $2,000 $14,000
Byrne, Aleena $12,000 $1,500 $0
Byrne, Aleena $11,000 $1,500 $0
这里第二排必须从另一个标签进入并放置,第四排,第六排等......
2.然后必须根据行 - 员工姓名一次比较两行。 有不同的列,如薪资,租金,贷款等......如果员工的薪水在两行中不同,则两个薪资单元都应突出显示。
示例数据:
Employee Name Salary Rent Loan
Adler, Jorge $10,000 $2,000 $15,000
Adler, Jorge $10,000 $2,000 $14,000
Byrne, Aleena $12,000 $1,500 $0
Byrne, Aleena $11,000 $1,500 $0
Bogie, Douglas $5,000 $1,200 $1,795
Bogie, Douglas $5,000 $1,225 $1,795
此处必须突出显示Adler,Jorge的贷款单元以及Byrne,Aleena的两个薪资单元以及Bogie,Douglas的租赁单元。
答案 0 :(得分:1)
这是另一种可能的解决方案:
如果您始终确切知道两行将彼此相邻,则可以使用此行,这将消除内部循环。
Public Sub HighlightDifferent()
Dim rngMain As Range, rngCompare As Range, rngAfterMain As Range, rng As Range
With ActiveSheet.UsedRange
For Each rngMain In .Rows
If rngMain.Columns(1) = rngMain.Offset(1, 0).Columns(1) Then
For Each rng In rngMain.Columns
If rng <> rngMain.Offset(1, 0).Columns(rng.Column) Then
rng.Interior.Color = vbYellow
rngMain.Offset(1, 0).Columns(rng.Column).Interior.Color = vbYellow
End If
Next
End If
Next
End With
End Sub
这是合并和排序功能:
Public Sub MergeAndSort()
Dim wks As Worksheet, rng As Range
Set wks = ThisWorkbook.Worksheets.Add
Worksheets("Sheet1").UsedRange.Copy wks.Range("A1")
With Worksheets("Sheet2").UsedRange
Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
End With
With wks.UsedRange
rng.Copy .Offset(.Rows.Count, 0).Resize(1, 1)
End With
wks.UsedRange.Sort key1:=wks.Range("A2"), order1:=xlAscending, Header:=xlYes
End Sub
答案 1 :(得分:0)
试试这个:
如果您有两列彼此相邻,并且您想要突出显示哪些行具有不同的值,则可以使用此列:
Public Sub HighlightDifferent()
Dim rng As Range
For Each rng In ActiveSheet.UsedRange.Rows
If rng.Columns(1) <> rng.Columns(2) Then rng.Interior.Color = vbYellow
Next
End Sub
首先尝试在测试工作簿中使用它,然后调整代码以使用您的布局