2013-03-14 8 views
0

4 장이 포함 된 Microsoft Excel 문서가 있습니다. 각 시트에는 21 개의 행과 약 500 개의 열이 있습니다. 특정 값을 가지고 이러한 시트의 모든 셀을 채우기 위해 가장 가까운 이웃 함수를 작성하려고합니다.Excel 매크로 가장 가까운 이웃

예 행 데이터 레이아웃 :

  1. 25 41 54 54 XX 41 54 XX XX XX 54 14
  2. 23 88 33 XX 41 54 XX 87 48 65 77 14

모든 데이터를 검토하고 XX를 가장 가까운 행의 이웃으로 바꿔야합니다. 이것은 각각의 값 (각 행의 각 열)을 거치며 그 현재 셀이 XX인지를 보면서 중첩 된 for 루프로 수행 할 수 있다고 생각합니다. 그렇다면 XX 값없이 가장 가까운 이웃을 움켜 잡아야합니다.

+1

당신이 "가까운 행 이웃"을 말할 때, 당신은 같은 행에서 가장 가까운 값을 의미 명확히하려면? 나는. 잠재적으로 더 가까운 수직 이웃을 무시합니까? 또한 예제에서 첫 번째'XX'와 같이 가장 가까운 이웃이 여러 곳에서있는 경우를 어떻게 해결하고 싶습니까? – ikh

+0

멋진 질문이지만 "가장 가까운 이웃"이 의미하는 바를 좀 더 자세하게 설명해야합니다 - 왼쪽 끝에있는 XX (왼쪽/오른쪽/위쪽/아래 - 가장 가까운 이웃을 다시 정의하는 방법에 따라 다름) –

+0

사과, 나는 가장 가까운 이웃을 의미했다. 시작과 끝에 XXXX가있는 경우가 있습니다.이 경우 행의 가장 가까운 비 X 값으로 채워지려고합니다. – user1574832

답변

0

나는 이것을 시도 할 것이다. 그러나 당신이 설명을위한 요청에 응답하지 않았기 때문에, 이것은 당신이 염두에 두었던 것과 정확히 같지 않을 수도 있음을 명심하라. 또한 VBA를 실행하는 시스템에 액세스하지 않고도이 작업을 수행하므로 사소한 버그 또는 두 가지가있을 수 있습니다. 코드 아래

Option Explicit 
sub fillNN() 
' we know there are five rows; number of columns is "approximate". 
dim thisRow as Integer 
dim s, c 
dim r, rLast as range 

for each s in WorkBook.WorkSheets 
    s.Activate 
    set r = Range("A1") 

    For thisRow = 1 To 5 
    set r = Range("A1").Offset(thisRow-1,0) 
    set rLast = r.End(xlToRight) ' find the last cell in the row 

    for each c in Range(r, rLast).cells 
     if c.Value = "XX" Then 
     c.Value = nearestNeighbor(c) 
     end if 
    next c 
    Next thisRow 

    ' the nearestNeighbor() function left the "XX" on the value 
    ' now we have to strip it: 
    For thisRow = 1 To 5 
    set r = Range("A1").Offset(thisRow-1,0) 
    set rLast = r.End(xlToRight) ' find the last cell in the row 

    for each c in Range(r, rLast).cells 
     if Left(c.Value, 2) = "XX" Then 
     c.Value = MID(c.Value, 3, len(c.Value)-2) 
     end if 
    next c 
    Next thisRow 

Next s 
End Sub 

Function nearestNeighbor(c as Range) 
' find the nearest "valid" cell: 
' look to the left and to the right; if nothing found, extend the range by 1 and repeat 
Dim rc, cc , dr, cs, s as Integer 
Dim lastCol as Integer 
Dim flag as Boolean 
flag = true 
s = 1 ' size of step 

lastCol = c.End(xlToRight).column 

' if c is the last cell, then the above will go to the end of the spreadsheet 
' since we know there are "about 500" columns, we can catch that easily: 
if lastCol > 1000 Then lastCol = c.column 

' make sure there is always a return value: 
nearestNeighbor = "XX" 
While (flag) 
    For dr = -1 To 1 Step 2 
    cs = c.column + dr * s 
    If Not(cs < 1 Or cs > lastCol) Then 
     If Not c.offset(dr * s, 0).Value = "XX" Then 
     flag = false 
     ' keep the "XX" in front so it won't become a "valid nearest neighbor" on next pass 
     nearestNeighbor = "XX" + c.offset(dr * s, 0).Value 
     Exit For 
     End If 
    End If 
    Next dr 
    s = s + 1 
    if s > lastCol Then flag = false 
End While 

End Function 
0

봅니다 :

데이터를 가정하면 아래 이미지와 같다.

enter image description here

코드 :

Sub Sample() 
    Dim rng As Range 
    Set rng = Cells.Find("XX") 

    Do Until rng Is Nothing 
     rng.Value = rng.Offset(0, -1) 'Offset(0, -1) for left neighbour , Offset(0, 1) for right 
     Set rng = Cells.Find("XX") 
    Loop 
End Sub 
관련 문제