2016-07-09 1 views
-6

행과 열의 텍스트가 만나는 경우에만 사용자가 값을 입력 할 수 있습니다. 그렇지 않으면 사용자가 값을 입력 할 수 없습니다. 나머지 셀은 0 또는 null 값을 표시 할 수 있습니다. 나는 VBA에 그것을하고 싶은 이미지는 참조 용으로 만 워크 시트 코드 창에서이 코드행과 열 머리글이 일치 할 때만 셀 값을 입력하십시오.

enter image description here

+4

이 사이트는 "무료 코드"소스가 아닌 특정 코드 관련 질문 용입니다. [How to ask] (http://stackoverflow.com/help/how-to-ask)를보고 질문에 따라 변경하십시오. –

+0

시트의 모든 셀을 잠그고 열 레이블이 행 레이블과 같은 셀을 잠금 해제 한 다음 시트를 보호하십시오. 코드를 시도했다면 코드를보기 좋을 것입니다. –

답변

1

장소 :

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target.Value <> "" Then '<--| bother only when user inputs values, not when he/she deletes them 
     If Cells(2, Target.Column) <> Cells(Target.Row, 1) Then '<--| if edited cell row (row 2) and column (column 1) headers don't match then ... 
      MsgBox "Sorry you must input a cell whose row and column headers match !" '<--|... inform the user... 
      Application.EnableEvents = False ' <--|... disable events not to have this event handler be called in an infinite loop,,, 
      Target.ClearContents '<--| clear the invalid userinput 
      Application.EnableEvents = True '<--| enable back events and have them run for subsequent user inputs 
     End If 
    End If 
End Sub 
0

대체 방법 경우 사용자가 모든 셀을 잠금으로써 항목을 방지 데이터를 입력 할 수 없습니다.

Sub StopEntries() 

Dim ws As Worksheet 
Dim wb As Workbook 
Dim SheetRow As Integer 
Dim SheetColumn As Integer 
Const ColumnHeaderRow As Integer = 2 
Const RowHeaderColumn As Integer = 1 
Const NumberOfRows As Integer = 21 
Const NumberOfColumns As Integer = 21 

Set wb = Workbooks("TestBook.xlsx") 
Set ws = wb.Worksheets("LockedSheet") 

With ws 
' lock all cells 
ws.Cells.Locked = True 

' unlock cells that meet the condition 
For SheetRow = 1 To NumberOfRows 
    For SheetColumn = 1 To NumberOfColumns 
     If .Cells(SheetRow + ColumnHeaderRow, RowHeaderColumn) = .Cells(ColumnHeaderRow, SheetColumn + RowHeaderColumn) Then .Cells(SheetRow + ColumnHeaderRow, SheetColumn + RowHeaderColumn).Locked = False 
    Next SheetColumn 
Next SheetRow 

' protect the sheet 
.Protect UserInterfaceOnly:=True 
End With 

End Sub 
관련 문제