简体   繁体   English

在Excel VBA上将水平单元格与垂直单元格进行比较

[英]Comparing horizental cells with vertical cells on excel VBA

i have a excel sheet 我有一个excel表 在此处输入图片说明

and i want to compare the value on the cells in vertical with the cells in horizental and put an "X" on the interaction 我想比较垂直单元格和水平单元格上的值,并在交互上加上“ X”

this is what i did : 这就是我所做的:

Set c = ThisWorkbook.Worksheets("harnwire")
For j = 2 To 100 Step 1
For i = 2 To 900 Step 1
If c.Cells(i, 1).Value = c.Cells(1, j).Value Then

c.Cells(i, j).Value = "X"
Else
c.Cells(i, j).Value = " "

Just to be clear - and sorry but my setup does not allow me to see pictures uploaded to this site and consequently I can't see your photo- What your code suggests is that you want to check 900 rows in column "A" against 100 columns in row 1. Is that correct. 请明确说明-很抱歉,但是我的设置不允许我查看上传到该网站的图片,因此我看不到您的照片-您的代码建议您要针对100列检查“ A”列中的900行第1行中的列。是否正确。 If there is a match between cell in column "A" and cell in row 1, then you want the junction cell between the two to show "X". 如果列“ A”中的单元格与行1中的单元格之间存在匹配项,则您希望两者之间的结点单元格显示“ X”。 Is that correct? 那是对的吗?

If so, then your logic is correct. 如果是这样,那么您的逻辑是正确的。 Perhaps your syntax is off. 也许您的语法已关闭。 Try this. 尝试这个。

Sub sdftgyhj()

    Set c = ThisWorkbook.Worksheets("harnwire")
    For j = 2 To 100 Step 1
        For i = 2 To 900 Step 1
            If c.Cells(i, 1).Value = c.Cells(1, j).Value Then
                    c.Cells(i, j).Value = "X"
            Else
                c.Cells(i, j).Value = " "
            End If
        Next i
    Next j

End Sub

The line 线

If c.Cells(i, 1).Value = c.Cells(1, j).Value Then

seems t be incorrect. 似乎不正确。 Because this compares column A to the headers in row 1. This line is essentially the same as 因为这会将A列与第1行中的标题进行比较,所以此行基本上与

If c.Cells(i, "A").Value = c.Cells(1, j).Value Then

Yet, based on the picture, it seems that you are more interested to compare another column with the header. 但是,基于图片,您似乎更感兴趣将另一列与标题进行比较。 So, maybe try something like this instead: 因此,也许尝试这样的事情:

If c.Cells(i, "G").Value = c.Cells(1, j).Value Then

(or whichever the correct column is) . (或正确的列为准)

So, bringing it all together that would be: 因此,将所有内容放在一起将是:

Option Explicit

Public Sub tmpSO()

Dim ws As Worksheet
Dim rowNumber As Long, columnNumber As Long

Set ws = ThisWorkbook.Worksheets("harnwire")

For columnNumber = 2 To 100
    For rowNumber = 2 To 900
        If c.Cells(rowNumber, "G").Value = c.Cells(1, columnNumber).Value Then
            c.Cells(rowNumber, columnNumber).Value = "X"
        Else
            c.Cells(rowNumber, columnNumber).Value = vbNullString
        End If
    Next rowNumber
Next columnNumber

End Sub

Update: 更新:

The following code addresses the comments provided: (1) Start comparing with the headers in column H and (2) error handling if the two cells cannot be compared because either cell contains an error. 以下代码解决了提供的注释:(1)开始与H列中的标题进行比较,以及(2)如果由于两个单元格中的任何一个包含错误而无法比较两个单元格,则进行错误处理。

Option Explicit

Public Sub tmpSO()

Dim ws As Worksheet
Dim rowNumber As Long, columnNumber As Long

Set ws = ThisWorkbook.Worksheets("harnwire")

For columnNumber = 8 To 100
    For rowNumber = 2 To 900
        If IsError(c.Cells(rowNumber, "G").Value) Or IsError(c.Cells(1, columnNumber).Value) Then
            c.Cells(rowNumber, columnNumber).Value = "err"
        Else
            If c.Cells(rowNumber, "G").Value = c.Cells(1, columnNumber).Value Then
                c.Cells(rowNumber, columnNumber).Value = "X"
            Else
                c.Cells(rowNumber, columnNumber).Value = vbNullString
            End If
        End If
    Next rowNumber
Next columnNumber

End Sub

Let me know if this works as expected. 让我知道这是否按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM