2015-01-19 3 views
0

기본적으로 MS Excel로 지뢰밭 게임을 만드는 중입니다. 그리고 지금은 오류가 나는 나의 '새로운 게임'을 실행하려고 '하고 프로그래머에게 각 myCell에서 범위 (strBoardSize)각 셀을 반복합니다.

내 전체 코드에 대한

에 뭔가 말을 한 후 나오고있다 범위 내의 모든 셀을 보도록 만들어지고 셀이 원하는 수만큼 옆에 있으면 주변에있는 항목의 수를 표시합니다. 코드를 작성하는 더 쉬운 방법이있을 수 있음을 이해하지만, 초보자 인 이유를 판단하지 마십시오.

Sub WorkOutMines() 

Dim myCell As Range 
Dim intCountSurroundingCells 

For Each myCell In Range(strBoardSize) 

intCountSurroundingCells = 0 

If myCell.Value = "x" Then 
Else 
    If myCell.Offset(-1, -1).Value = "x" Then 
     intCountSurroundingCells = intCountSurroundingCells + 1 
    End If 

    If myCell.Offset(-1, 0).Value = "x" Then 
     intCountSurroundingCells = intCountSurroundingCells + 1 
    End If 

    If myCell.Offset(-1, 1).Value = "x" Then 
     intCountSurroundingCells = intCountSurroundingCells + 1 
    End If 

    If myCell.Offset(0, -1).Value = "x" Then 
     intCountSurroundingCells = intCountSurroundingCells + 1 
    End If 

    If myCell.Offset(0, 1).Value = "x" Then 
     intCountSurroundingCells = intCountSurroundingCells + 1 
    End If 

    If myCell.Offset(1, -1).Value = "x" Then 
     intCountSurroundingCells = intCountSurroundingCells + 1 
    End If 

    If myCell.Offset(1, 0).Value = "x" Then 
     intCountSurroundingCells = intCountSurroundingCells + 1 
    End If 

    If myCell.Offset(1, 1).Value = "x" Then 
     intCountSurroundingCells = intCountSurroundingCells + 1 
    End If 

    If intCountSurroundingCells = 0 Then 
    Else 
     myCell.Value = intCountSurroundingCells 
    End If 

End If 

Next 

End Sub 

는 strBoardSize를 선언

당신이 변수 "strBoardSize"를 작성하고 당신이되고 싶은 범위를 설정해야 할 것 같습니다
Sub SetBoardSize() 

strBoardSize = "Board9x9" 

End Sub 
+0

'strBoardSize'의 값은 무엇입니까? –

+0

수정되었는데 잘못 되었나요? –

+0

"Board9x9"등과 같은 명명 된 범위가 있습니까? 그렇지 않다면 그것들을 생성하거나 범위를'WorkOutMines' 서브에 어떻게 전달할 것인지를 바꿀 필요가 있습니다. –

답변

0

당신은 "WorkoutMines"하위에서 그 변수를 선언해야한다. 같은 하위 프로 시저에서 변수를 선언하지 않는 경우

Dim strBoardSize as String 

는 그런 다음 변수를 변수

strBoardSize = "Board9x9" 

을 설정할 수 있습니다, 그것은 가치의 유지되지 않습니다.

+0

주석에 코드로 코드를 표시하는 데 문제가있어서 다른 대답을 추가했습니다. – Kyle

+0

위대한, 그것은 일했다! 고맙습니다. –

0

. "strBoardSize은"명명 된 범위가 통합 문서에있는 경우 올바른 구문은 다음과 같습니다

For Each myCell in Range("strBoardSize") 
+0

내 기본 메시지가 편집되었습니다. strBoardSize에 문제가 있습니까? –

+0

"strBoardSize"변수는 내가 볼 수있는 코드의 아무 곳에서나 선언하지 않습니다. "strBoardSize"는 어떻게 설정됩니까? "strBoardSize"가 "Board 9X9"또는 "Board 16X16". – Kyle

+0

메인 메시지가 업데이트되었으므로 처음에는 오해했습니다. –

관련 문제