2017-09-24 2 views
0

Image열 위치가 고정되어 있지 않은 것으로 가정하고 두 열 모두 자동으로 검색 한 후 아래 작업을 수행해야합니다. 사례 : -두 개의 서로 다른 열의 값을 고유 한 조건으로 비교

'C'열에는 'F'열의 7 자리 값을 나타내는 두 자리 값이 있습니다. 열 'C'에는 열 'F'의 8 자리 값으로 연결되는 3 자리 값이 있습니다. 'F2'와 'F3'이 21로 시작하고 Msgbox 'Error in this row'가 유효한지 확인하고 싶습니다. 'F6'과 'F7'이 함께 시작하는지 228, '이 행에 오류가 없습니다'라는 메시지가 표시됩니다.

감사합니다. 은 ++ 코드

 Sub CompareColumns() 
    Dim lr As Long, n As Long 
    Dim rng As Range, cell As Range 
    Dim strA As String, strB As String, str As String 
    Dim NotMatched As Boolean 

    lr = Cells(Rows.Count, 1).End(xlUp).Row 

    'Assuming your data starts from Row2 
    Set rng = Range("B2:B" & lr) 
    str = "The following cells don't match." & vbNewLine & vbNewLine 
    For Each cell In rng 
    If cell <> "" Then 
     n = Len(cell.Offset(0, -1)) 
     If n > 0 Then 
      strA = cell.Offset(0, -1).Text 
      strB = Left(cell, n) 
      If strA <> strB Then 
       NotMatched = True 
       str = str & cell.Offset(0, -1).Address(0, 0) & " : " &  
cell.Offset(0, -1).Value & vbTab & cell.Address(0, 0) & " : " & cell.Value & 
    vbNewLine 
      End If 
     Else 
      str = str & cell.Offset(0, -1).Address(0, 0) & " : " & cell.Offset(0, -1).Value & vbTab & cell.Address(0, 0) & " : " & cell.Value & vbNewLine 
     End If 
    End If 
    n = 0 
    strA = "" 
    strB = "" 
    Next cell 
    If NotMatched 
    MsgBox str, vbInformation 
Else 
    MsgBox "Both columns match.", vbInformation 
End If 
End Sub 
+0

지금까지 작성한 코드를 게시하십시오. –

+0

"sktneer"에서 가져온 코드를 게시했습니다. –

답변

0

가 제공되는 코드 열 A 및 B가 아니라 C 및 비교 F. 코드 열 A의 각 행에서 값이 열에있는 값의 처음 숫자와 동일한지를 비교 B를 누른 다음 모든 다른 행을 인쇄하십시오.

코드는 약간의 오류가 나는 그것을 아래처럼 고정 :

Sub CompareColumns() 
Dim lr As Long, n As Long 
Dim rng As Range, cell As Range 
Dim strA As String, strB As String, str As String 
Dim NotMatched As Boolean 

lr = Cells(Rows.Count, 1).End(xlUp).Row 

'Assuming your data starts from Row2 
Set rng = Range("B2:B" & lr) 
str = "The following cells don't match." & vbNewLine & vbNewLine 
For Each cell In rng 
If cell <> "" Then 
    n = Len(cell.Offset(0, -1)) 
    If n > 0 Then 
     strA = cell.Offset(0, -1).Text 
     strB = Left(cell, n) 
     If strA <> strB Then 
      NotMatched = True 
      str = str & cell.Offset(0, -1).Address(0, 0) & " : " & cell.Offset(0, -1).Value & vbTab & cell.Address(0, 0) & " : " & cell.Value & vbNewLine 
     End If 
    Else 
     str = str & cell.Offset(0, -1).Address(0, 0) & " : " & cell.Offset(0, -1).Value & vbTab & cell.Address(0, 0) & " : " & cell.Value & vbNewLine 
    End If 
End If 
n = 0 
strA = "" 
strB = "" 
Next cell 
If NotMatched Then 
    MsgBox str, vbInformation 

Else 
    MsgBox "Both columns match.", vbInformation 
End If 
End Sub 

을이 당신이, 알려 주시기 바랍니다 찾고 있습니다하지 것입니다 경우.

+0

감사! 열 위치가 고정되어 있지 않기 때문에 머리글을 기반으로 열을 검색하는 코드를 찾고 있습니다. 너 나 좀 도와 줄 수있어? –

관련 문제