【问题标题】:Compare two columns in excel and seeing if values are the same比较excel中的两列并查看值是否相同
【发布时间】:2017-05-23 16:55:31
【问题描述】:

这张图片显示了之前的情况。 https://drive.google.com/open?id=0B8BmxxuBoGYnVkhDaEF2b1J6ejA

代码的目标是查看第 1 列的第一个单元格的值,然后通过向下的列在第 4 列中查找相同的值。对于第 1 列中的第一个单元格,它将是蜂蜜,第 4 列中的相应行是 6。然后它将复制第 5 列和第 6 列中与第 4 列中的蜂蜜对应的值并将其放入第 2 列和与第 1 列的蜂蜜对应的行中的第 3 列。每次第 2 列或第 3 列中的单元格被填充时,它将被涂成蓝色。我不知道如何获得正确的语法以将一个单元格中的字符串设置为等于另一个单元格中的字符串,并首先确定该单元格是否为空白。

这张图片显示了之后。 https://drive.google.com/open?id=0B8BmxxuBoGYnX1VXWllaQTAxWFE

Sub checkcolumns()
'j determines the row for Column 1. n determines the row for Column 4'
Dim j As Integer
Dim n As Integer
j = 1
n = 1

 'The first part is a Do While loop and is intended to check if the first 
 'cell is filled with something. If it's not then the code won't run.'

Do While Cells(j,1).Value <> Not vbNullString

 'The next part determines whether the first cell from Column 1 and 
 'first cell from Column 4 are the same. If they aren't then it will 
 'search for the cell in Column 4 that has the same value. n denotes the row 
 'for column 4 and the Do Until loop will determine which row in column 4 
 'has the exact value as the cell we're looking at from Column 1

     if Cell(j,1) NotEqual Cell(n, 4)
         Do Until cell(j, 1) Equalto Cell(n, 4)
             n = n + 1
         End

  'The next if statements first determine whether Column 2 of the row we're 
  'looking at has a value already. If it does not then that cell is 
  'populated with whatever value is in Column 5 of the corresponding row for  
  'Column 4 which is found with n. This is repeated for Column 3 using 
  'Column 6.

        if Cells(j, 2).Value <> vbNullString Then
            Cells(j, 2) = Cells(n, 5)
            Cells(j, 2).Interior.ColorIndex = 5
        End if
        if Cells(j, 3).Value <> vbNullString Then
            Cells(j, 3) = Cells(n, 6)
            Cells(j, 3).Interior.ColorIndex = 5
        End if
   'This else statement below is for the case where the cell value from 
   'Column 1 on that row is equal to the cell value of Column 4 on that
   'same row, so j and n would be equal.
    Else
        if Cells(j, 2).Value <> vbNullString Else
            Cells(j, 2) = Cells(n, 5)
            Cells(j, 2).Interior.ColorIndex = 5
        End If
        if Cells(j, 3).Value <> vbNullString Else
            Cells(j, 3) = Cells(n, 6)
            Cells(j, 3).Interior.ColorIndex = 5
        End If
    End If
    'Once it has checked the first row in Column 1. It will then look at the 
    'second row.
    j = j + 1
End

End Sub

【问题讨论】:

  • 使用 VLOOKUP????
  • 颜色的条件格式?

标签: excel vba


【解决方案1】:

把这个公式放在B2:

=VLOOKUP(A2,$D$2:$F$7,2,FALSE)

然后把这个公式放到C2中:

=VLOOKUP(A2,$D$2:$F$7,3,FALSE)

A2 是您要在 D 列中搜索的值 $D$2:$F$7 创建一个静态表进行搜索 2 或 3 是您要返回的表中的列(来自表的第一个列) False 要求搜索完全匹配

将它们放入单元格后,将它们向下拖动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 2013-09-08
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多