【问题标题】:Creating a macro to compare cell values in the same worksheet but different columns创建宏以比较同一工作表但不同列中的单元格值
【发布时间】:2015-04-26 20:19:03
【问题描述】:

我正在尝试创建一个宏,它将系统地比较单元格的值。 我有 2 个数据集。我打算创建的宏基本上会将“C3:M25”中的值与“O3:Y25”中的值进行比较。

我的宏应该开始将范围(“C3”)中的值与范围(“O3”)进行比较。 如果C3.value > O3.value,它会改变interior.colourindex.value 和字体颜色

第一次比较完成后,它将向下移动到下一行,即 将 range("C4").value 与 range("O4") 进行比较。该过程一直持续到它到达列中的第一个空白行,在本例中为 Range("C26")。

一旦 range("C26") 是一个空单元格,那么 宏将重复比较过程,但这一次它将基本上将 Range("D3") 中的值与 Range("P3") 进行比较。循环一直持续到整个过程完成。

Sub ilovetocompare()

Dim ross As Long, colss As Long
Dim wb As Workbook, ws1 As Long, ws1row As Integer



Set wb = ActiveWorkbook.Sheets("Pricer")
wb.Range("C3").Activate
With ActiveCell
ws1row = Worksheets("pricer").Range("B3").End(xlDown).Rows.Count
'get the last row count


'macro will stop when it detects that the cells is filled with other colors

Do Until ActiveCell.Interior.Color = 255

'start comparing the prices
For ross = 3 To ws1row
For colss = 15 To 25 ' number of columns will remain unchanged

If ActiveCell.Value > Cells(ross, colss).Value Then

ActiveCell.Font.Bold = True
ActiveCell.Font.colour = vbWhite
'once done with comparison, jump to the next row
ActiveCell.Offset(1, 0).Activate
'the column O likewise also move 1 row down for comparison

Next ross

'when the it hits an empty row, the activecell got readjusted back to the top
ElseIf ActiveCell.Value = "" Then
ActiveCell.Offset(-ws1row, 1).Select
With Selection
Loop
'move the cell up again so that i can resume the comparsion

'create this into a loop




End Sub

【问题讨论】:

  • 我可能会提出愚蠢的问题,但是对于 VLOOKUP 或简单的 Sheet1!b1=Sheet2!c1 样式的公式,然后进行条件格式化,这意味着您必须求助于 VBA,有什么不合适的?

标签: vba excel


【解决方案1】:

这里有一个建议:

Private Sub macrobygiada()
ColumnoneStart = 3 ' C
ColumnoneEnd = 13 'M
ColumntwoStart = 15 'O

Set wb = ActiveWorkbook.Sheets("Pricer")

TotalColumn = ColumnoneEnd - ColumnoneStart 'difference of the columnnumber C to M (3 to 13)
For Column = 1 To TotalColumn 'number of columns
    For Cell = 3 To 25 'go through the Cells
        If (Cells(Cell, ColumnoneStart).Value > Cells(Cell, ColumntwoStart).Value) Then
            wb.Cells(Cell, ColumnoneStart).Font.Bold = True
            wb.Cells(Cell, ColumnoneStart).Font.ColorIndex = 2 'colour white
        End If
    Next
ColumnoneStart = ColumnoneStart + 1
ColumntwoStart = ColumntwoStart + 1
Next
Set wb = Nothing
End Sub

问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多