Excel이 있습니다.Excel VBA에서 세 열의 계층 구조를 확인하는 방법
첫 번째 시트는 다음과 같습니다 : 두 개의 시트를 가지고있는 '관할 구역'세 개의 열이 :
국가 (열 B), 주 (열 C)와시 (열 D)을
이 시트 각 도시마다 단일 항목이 있습니다.
그러나 각 도시가 별도의 행에 나열되어 있기 때문에 어떤 도시가 속한 주 및 국가의 이름이 여러 행에 반복 될 수 있습니다. 예를 들어
:
U.S. --> New York --> Buffalo
U.S. --> New York --> Manhattan
(이 내 두 행이다)
나는 다른 시트를 가지고 : 시트 1을;
여기도 같은 세 개의 열이 있습니다. (및 다른 일부 20 개의 열)
이 세 개의 열은 '관할 구역'시트의 세 열로 검증됩니다. (만 몇 '관할 구역은'Sheet1의에 나와있는, 그 어떤 순서로 할 수 있으며 모든 국가를 위해 할 수 있습니다) 유효성 검사 규칙은
:
나라를 들어
1) - 나라 이름은 단일 값이어야합니다.
- 관할 구역 시트의 '국가'열에있는 이름과 일치해야합니다.
- 단지 세미콜론 (세미콜론으로 그 값을 구분하기로 구분 된 하나 개 또는 여러 개의 값을 가질 수 나는 다른를 작성했습니다 - 케이스 가
이
(대문자/소문자)2) 주 무시한다 코드가) 잘 작동
-이 셀의 항목도 할 수있다 '전체'
- 모든 국가 이름은 관할 시트의 '상태'열 아래에 나열된 상태와 일치해야합니다. (여러 항목이 나열된 경우 먼저 delimeter - semicolon을 기준으로 구분 한 다음 비교해야합니다.)
- 사례는 무시해야합니다 (대문자/소문자). 상태 이름 앞뒤의 여분의 공백을 제거해야합니다.
3)
도시
- 만세미콜론
로 구분 된 하나 개 또는 여러 개의 값을 가질 수 있습니다 -이 셀의 항목은 '모든'도 될 수 있습니다.- 모든 도시 이름은 관할 구역 시트의 '도시'열에 나열된 도시와 일치해야합니다.(여러 항목이 나열된 경우 먼저 delimeter - semicolon을 기준으로 구분 한 다음 비교해야합니다.)
- 사례는 무시해야합니다 (대문자/소문자). 상태 이름 앞뒤의 여분의 공백을 제거해야합니다.
개별 열의 유효성을 검사하는 코드를 작성했습니다.
하지만 충분하지 않습니다 .. !!!
너무 계층 구조를 검증해야합니다. !!
즉
U.S. --> New York --> Buffalo
U.S. --> New York --> Manhattan; Buffalo
India --> Karnataka;Maharashtra --> All
I 이러한 개별 열 다음과 유효성을 쓴 코드;
'********************************************************
'validate the 'Country' column in Sheet1, such that; It matches with one of the Country names and must exist
'********************************************************
'Get the last row
'Dim lastRow As Integer
LastRow = Sheets("Sheet1").UsedRange.Rows.Count
nLastRowSheet2 = Sheets("Jurisdictions").UsedRange.Rows.Count
Dim c As Range
'Turn screen updating off to speed up macro code.
'User won't be able to see what the macro is doing, but it will run faster.
Application.ScreenUpdating = False
For Each c In Worksheets("Sheet1").Range("B2:B" & LastRow)
Dim rngFnder As Range
On Error Resume Next
Set rngFnder = Sheets("Jurisdictions").Range("B2:B" & nLastRowSheet2).Find(c)
If rngFnder Is Nothing Then
c.Interior.Color = vbRed
End If
On Error GoTo 0
Next
'********************************************************
'validate the 'State(multiples)' column in the Questions sheet, such that:
'- State name matches with one of the state names or
'- State name is set as 'All'
'********************************************************
Dim stString As String
Dim stArray() As String
'Get the last row
'Dim lastRow As Integer
'LastRow = Sheets("Sheet1").UsedRange.Rows.Count
'nLastRowSheet2 = Sheets("Jurisdictions").UsedRange.Rows.Count
'Dim c As Range
Dim d As Range
Dim e As Variant
For Each c In Worksheets("Sheet1").Range("C2:C" & LastRow)
stString = c
stArray() = Split(stString, ";")
For Each e In stArray()
e = Trim(e)
'Dim rngFnder As Range
On Error Resume Next
Set rngFnder = Sheets("Jurisdictions").Range("C2:C" & nLastRowSheet2).Find(e)
If rngFnder Is Nothing And c <> "All" Then
c.Interior.Color = vbRed
End If
On Error GoTo 0
Next
Next
'********************************************************
'validate the City(Multiples) column in the Questions sheet, such that:
'- City name matches with one of the Cities or
'- City name is set as 'All'
'********************************************************
'Dim stString As String
'Dim stArray() As String
'Get the last row
'Dim lastRow As Integer
'LastRow = Sheets("Sheet1").UsedRange.Rows.Count
'nLastRowSheet2 = Sheets("Jurisdictions").UsedRange.Rows.Count
'Dim c As Range
'Dim d As Range
'Dim e As Variant
For Each c In Worksheets("Sheet1").Range("D2:D" & LastRow)
stString = c
stArray() = Split(stString, ";")
For Each e In stArray()
e = Trim(e)
'Dim rngFnder As Range
On Error Resume Next
Set rngFnder = Sheets("Jurisdictions").Range("D2:D" & nLastRowSheet2).Find(e)
If rngFnder Is Nothing And c <> "All" Then
c.Interior.Color = vbRed
End If
On Error GoTo 0
Next
Next
위의 모든 코드를 단일 코드 모듈로 결합하려고 할 때 문제가 발생합니다. Excel vba의 초보자 인 것처럼 인접한 셀을 참조하는 방법을 모르겠습니다. 문자열을 연결하는 방법 ('State'및 'City'열에서는 여러 항목이있을 경우 세미콜론을 기준으로 해당 주/도시를 먼저 구분해야 함)을 세 가지 다른 열에서 비교하고 세 가지 열과 비교하십시오.
적절한 코드를 작성하는 데 도움을 줄 수 있습니까? NEW 정보를 기반으로
방금 코드를 다시 편집했습니다. . 무한 루프를 제거하려면이 새 코드를 시도하십시오. 잘못된 위치에 내 검사 변수가 있습니다. – user3561813