microsoft_excel:macros:compare_rows
Differences
This shows you the differences between two versions of the page.
microsoft_excel:macros:compare_rows [2021/08/04 14:05] – created peter | microsoft_excel:macros:compare_rows [2021/08/04 15:03] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Microsoft Excel - Macros - Compare Rows ====== | ||
- | |||
- | <code excel> | ||
- | Sub CompareRows() | ||
- | Dim Row1 As Range | ||
- | Dim Row2 As Range | ||
- | |||
- | ' | ||
- | ' | ||
- | Set Row1 = Application.InputBox(" | ||
- | |||
- | 'Check that the range they have provided consists of only 1 row... | ||
- | If Row1.Rows.Count > 1 Then | ||
- | |||
- | Do Until Row1.Rows.Count = 1 | ||
- | |||
- | MsgBox "You can only select 1 row" | ||
- | Set Row1 = Application.InputBox(" | ||
- | |||
- | Loop | ||
- | |||
- | End If | ||
- | | ||
- | ' | ||
- | ' | ||
- | Set Row2 = Application.InputBox(" | ||
- | |||
- | 'Check that the range they have provided consists of only 1 row... | ||
- | If Row2.Rows.Count > 1 Then | ||
- | |||
- | Do Until Row2.Rows.Count = 1 | ||
- | |||
- | MsgBox "You can only select 1 row" | ||
- | Set Row2 = Application.InputBox(" | ||
- | |||
- | Loop | ||
- | |||
- | End If | ||
- | |||
- | |||
- | 'Check both row ranges are the same size... | ||
- | ' | ||
- | If Row2.Columns.Count <> Row1.Columns.Count Then | ||
- | |||
- | Do Until Row2.Columns.Count = Row1.Columns.Count | ||
- | |||
- | MsgBox "The second row must be the same size as the first" | ||
- | Set Row2 = Application.InputBox(" | ||
- | |||
- | Loop | ||
- | |||
- | End If | ||
- | |||
- | 'If entire rows have been selected (e.g. $AEmbarrassedA), | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | If Row1.Columns.Count = 16384 Then | ||
- | |||
- | Set Row1 = Range(Row1.Cells(1), | ||
- | Set Row2 = Range(Row2.Cells(1), | ||
- | |||
- | End If | ||
- | |||
- | |||
- | ' | ||
- | ' | ||
- | Dim intCell As Long | ||
- | |||
- | For intCell = 1 To Row1.Columns.Count | ||
- | |||
- | If Row1.Cells(intCell) <> Row2.Cells(intCell) Then | ||
- | |||
- | Row1.Cells(intCell).Interior.Color = vbYellow | ||
- | Row2.Cells(intCell).Interior.Color = vbYellow | ||
- | |||
- | Else | ||
- | | ||
- | Row1.Cells(intCell).Interior.Color = vbWhite | ||
- | Row2.Cells(intCell).Interior.Color = vbWhite | ||
- | |||
- | End If | ||
- | |||
- | Next | ||
- | | ||
- | End Sub | ||
- | |||
- | Sub CompareRows2(Row1 As Range, Row2 As Range) | ||
- | |||
- | Dim intCell As Long | ||
- | |||
- | For intCell = 1 To Row1.Columns.Count | ||
- | |||
- | On Error Resume Next | ||
- | If Row1.Cells(intCell) <> Row2.Cells(intCell) Then | ||
- | |||
- | ' | ||
- | Row2.Cells(intCell).Interior.Color = vbYellow | ||
- | |||
- | Else | ||
- | | ||
- | ' | ||
- | Row2.Cells(intCell).Interior.Color = vbWhite | ||
- | |||
- | End If | ||
- | |||
- | Next | ||
- | | ||
- | End Sub | ||
- | |||
- | Sub CheckRowSame() | ||
- | ' | ||
- | ' CheckRowSame Macro | ||
- | ' | ||
- | Dim Row1 As Range | ||
- | Dim Row2 As Range | ||
- | |||
- | Dim Resource1 As String | ||
- | Dim Resource2 As String | ||
- | | ||
- | Dim Columns As Integer | ||
- | Dim ColumnLetter As String | ||
- | | ||
- | Application.Goto Range(" | ||
- | ActiveCell.Offset(1, | ||
- | Resource1 = ActiveCell.Text | ||
- | |||
- | Columns = ActiveSheet.UsedRange.Columns.Count | ||
- | If Columns = 3 Then ColumnLetter = " | ||
- | If Columns = 4 Then ColumnLetter = " | ||
- | If Columns = 5 Then ColumnLetter = " | ||
- | If Columns = 6 Then ColumnLetter = " | ||
- | If Columns = 7 Then ColumnLetter = " | ||
- | If Columns = 8 Then ColumnLetter = " | ||
- | If Columns = 9 Then ColumnLetter = " | ||
- | If Columns = 10 Then ColumnLetter = " | ||
- | If Columns = 11 Then ColumnLetter = " | ||
- | If Columns = 12 Then ColumnLetter = " | ||
- | If Columns = 13 Then ColumnLetter = " | ||
- | If Columns = 14 Then ColumnLetter = " | ||
- | If Columns = 15 Then ColumnLetter = " | ||
- | If Columns = 16 Then ColumnLetter = " | ||
- | If Columns = 17 Then ColumnLetter = " | ||
- | If Columns = 18 Then ColumnLetter = " | ||
- | If Columns = 19 Then ColumnLetter = " | ||
- | If Columns = 20 Then ColumnLetter = " | ||
- | If Columns = 21 Then ColumnLetter = " | ||
- | If Columns = 22 Then ColumnLetter = " | ||
- | If Columns = 23 Then ColumnLetter = " | ||
- | If Columns = 24 Then ColumnLetter = " | ||
- | If Columns = 25 Then ColumnLetter = " | ||
- | If Columns = 26 Then ColumnLetter = " | ||
- | |||
- | Do While Resource1 <> "" | ||
- | Set Row1 = Range(" | ||
- | ' | ||
- | | ||
- | ActiveCell.Offset(1, | ||
- | Resource2 = ActiveCell.Text | ||
- | Set Row2 = Range(" | ||
- | | ||
- | If Resource1 = Resource2 Then | ||
- | Call CompareRows2(Row1, | ||
- | ActiveCell.Offset(1, | ||
- | If Resource1 = ActiveCell.Text Then | ||
- | ActiveCell.Offset(-1, | ||
- | Else | ||
- | Resource1 = ActiveCell.Text | ||
- | End If | ||
- | Else | ||
- | ActiveCell.Offset(1, | ||
- | If Resource2 = ActiveCell.Text Then | ||
- | Resource1 = ActiveCell.Text | ||
- | ActiveCell.Offset(-1, | ||
- | Else | ||
- | Resource1 = ActiveCell.Text | ||
- | End If | ||
- | End If | ||
- | Loop | ||
- | | ||
- | Application.Goto Range(" | ||
- | | ||
- | End Sub | ||
- | </ | ||
microsoft_excel/macros/compare_rows.1628085901.txt.gz · Last modified: 2021/08/04 14:05 by peter