TOP
VBA-수업 12.4. 제어 요소(연습)
이제 컨트롤(Controls) 사용을 연습할 수 있는 작은 연습을 살펴보겠습니다.
미리 만들어진 양식과 제어 요소가 포함된 파일이 있습니다. 사용자가 양식과의 상호 작용을 통해 즉석 데이터베이스를 채울 수 있도록 모든 요소가 작동하도록 하는 코드를 작성해야 합니다.
다운로드할 Excel 파일은 다음과 같습니다. controls_exercise.xls
이제 여기서의 목표는 양식을 사용하여 테이블을 채우는 것임이 분명해졌습니다.
고려해야 할 몇 가지 사항:
국가 목록은 두 번째 시트의 목록을 기반으로 합니다.
새 연락처를 추가하기 전에 제어 요소의 값을 확인합니다.
값을 입력한 후 폼을 닫지 않고 컨트롤의 초기값을 반환합니다.
문제를 해결하는 방법 중 하나
먼저, 사용하기 쉽도록 양식의 Zoom 속성을 120으로 늘려야 합니다.
우리는 이미 컨트롤에 대한 첫 번째 강의에서 옵션 버튼 확인을 다루었으므로 여기서는 간단한 솔루션을 사용합니다.
"Mrs"는 기본적으로 선택됩니다(속성 Value: True). 즉, 선택할 때 주소를 확인하지 않습니다.
닫기 버튼
- Private Sub CommandButton_Close_Click()
- Unload Me
- End Sub
Private Sub CommandButton_Close_Click()
Unload Me
End Sub
드롭다운 목록의 내용
- Private Sub UserForm_Initialize()
- For i = 1 To 252
- ComboBox_Country.AddItem Sheets("Country").Cells(i, 1)
- Next
- End Sub
Private Sub UserForm_Initialize() '양식이 열려 있을 때 목록 로드
For i = 1 To 252 '국가 워크시트에서 252개 국가 목록 생성
ComboBox_Country.AddItem Sheets("Country").Cells(i, 1)
Next
End Sub
제어 요소 확인
간단한 해결책은 컨트롤 중 하나라도 비어 있으면 대화 상자를 표시하는 것입니다.
- Private Sub CommandButton_Add_Click()
- If TextBox_Last_Name.Value = "" Or TextBox_First_Name.Value = "" Or TextBox_Address.Value = "" Or TextBox_Place.Value = "" Or ComboBox_Country.Value = "" Then
- MsgBox "Form incomplete"
- Else
-
- End If
- End Sub
Private Sub CommandButton_Add_Click()
If TextBox_Last_Name.Value = "" Or TextBox_First_Name.Value = "" Or TextBox_Address.Value = "" Or TextBox_Place.Value = "" Or ComboBox_Country.Value = "" Then
MsgBox "Form incomplete"
Else
'여기에 연락처 입력 지침...
End If
End Sub
하지만 좀 더 복잡하게 하려면 각 요소를 별도로 검사해야 하며, 비어 있는 요소가 있으면 이름()의 색상이 빨간색으로 변경되어야 합니다.
- Private Sub CommandButton_Add_Click()
-
- Label_Last_Name.ForeColor = RGB(0, 0, 0)
- Label_First_Name.ForeColor = RGB(0, 0, 0)
- Label_Address.ForeColor = RGB(0, 0, 0)
- Label_Place.ForeColor = RGB(0, 0, 0)
- Label_Country.ForeColor = RGB(0, 0, 0)
-
-
- If TextBox_Last_Name.Value = "" Then
- Label_Last_Name.ForeColor = RGB(255, 0, 0)
- ElseIf TextBox_First_Name.Value = "" Then
- Label_First_Name.ForeColor = RGB(255, 0, 0)
- ElseIf TextBox_Address.Value = "" Then
- Label_Address.ForeColor = RGB(255, 0, 0)
- ElseIf TextBox_Place.Value = "" Then
- Label_Place.ForeColor = RGB(255, 0, 0)
- ElseIf ComboBox_Country.Value = "" Then
- Label_Country.ForeColor = RGB(255, 0, 0)
- Else
-
- End If
- End Sub
Private Sub CommandButton_Add_Click()
'이름 색상을 검정색으로 설정
Label_Last_Name.ForeColor = RGB(0, 0, 0)
Label_First_Name.ForeColor = RGB(0, 0, 0)
Label_Address.ForeColor = RGB(0, 0, 0)
Label_Place.ForeColor = RGB(0, 0, 0)
Label_Country.ForeColor = RGB(0, 0, 0)
'콘텐츠 제어
If TextBox_Last_Name.Value = "" Then '아무것도 지정하지 않은 경우...
Label_Last_Name.ForeColor = RGB(255, 0, 0) '제목 색상을 빨간색으로 설정
ElseIf TextBox_First_Name.Value = "" Then
Label_First_Name.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Address.Value = "" Then
Label_Address.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Place.Value = "" Then
Label_Place.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_Country.Value = "" Then
Label_Country.ForeColor = RGB(255, 0, 0)
Else
'여기에 연락처 입력 지침...
End If
End Sub
데이터 삽입
위의 코드에 표시된 위치에 다음 코드를 삽입해야 합니다(주석 참조).
- Dim row_number As Integer, salutation As String
-
-
- For Each salutation_button In Frame_Salutation.Controls
- If salutation_button.Value Then
- salutation = salutation_button.Caption
- End If
- Next
-
-
- row_number = Range("A65536").End(xlUp).Row + 1
-
-
- Cells(row_number, 1) = salutation
- Cells(row_number, 2) = TextBox_Last_Name.Value
- Cells(row_number, 3) = TextBox_First_Name.Value
- Cells(row_number, 4) = TextBox_Address.Value
- Cells(row_number, 5) = TextBox_Place.Value
- Cells(row_number, 6) = ComboBox_Country.Value
-
-
- OptionButton1.Value = True
- TextBox_Last_Name.Value = ""
- TextBox_First_Name.Value = ""
- TextBox_Address.Value = ""
- TextBox_Place.Value = ""
- ComboBox_Country.ListIndex = -1
Dim row_number As Integer, salutation As String
'항소의 선택
For Each salutation_button In Frame_Salutation.Controls
If salutation_button.Value Then
salutation = salutation_button.Caption '이의신청이 선택됨
End If
Next
'row_number = 열 +1에서 비어 있지 않은 마지막 셀의 행 번호
row_number = Range("A65536").End(xlUp).Row + 1
'워크시트에 값 삽입
Cells(row_number, 1) = salutation
Cells(row_number, 2) = TextBox_Last_Name.Value
Cells(row_number, 3) = TextBox_First_Name.Value
Cells(row_number, 4) = TextBox_Address.Value
Cells(row_number, 5) = TextBox_Place.Value
Cells(row_number, 6) = ComboBox_Country.Value
'붙여넣은 후 원래 값이 반환됩니다.
OptionButton1.Value = True
TextBox_Last_Name.Value = ""
TextBox_First_Name.Value = ""
TextBox_Address.Value = ""
TextBox_Place.Value = ""
ComboBox_Country.ListIndex = -1
일반적인 모습
이것이 전부입니다. 여기에 연습용으로 완성된 코드와 다운로드할 파일이 있습니다.
- Private Sub CommandButton_Close_Click()
- Unload Me
- End Sub
-
- Private Sub UserForm_Initialize()
- For i = 1 To 252
- ComboBox_Country.AddItem Sheets("Country").Cells(i, 1)
- Next
- End Sub
-
- Private Sub CommandButton_Add_Click()
-
- Label_Last_Name.ForeColor = RGB(0, 0, 0)
- Label_First_Name.ForeColor = RGB(0, 0, 0)
- Label_Address.ForeColor = RGB(0, 0, 0)
- Label_Place.ForeColor = RGB(0, 0, 0)
- Label_Country.ForeColor = RGB(0, 0, 0)
-
-
- If TextBox_Last_Name.Value = "" Then
- Label_Last_Name.ForeColor = RGB(255, 0, 0)
- ElseIf TextBox_First_Name.Value = "" Then
- Label_First_Name.ForeColor = RGB(255, 0, 0)
- ElseIf TextBox_Address.Value = "" Then
- Label_Address.ForeColor = RGB(255, 0, 0)
- ElseIf TextBox_Place.Value = "" Then
- Label_Place.ForeColor = RGB(255, 0, 0)
- ElseIf ComboBox_Country.Value = "" Then
- Label_Country.ForeColor = RGB(255, 0, 0)
- Else
-
- Dim row_number As Integer, salutation As String
-
-
- For Each salutation_button In Frame_Salutation.Controls
- If salutation_button.Value Then
- salutation = salutation_button.Caption
- End If
- Next
-
-
- row_number = Range("A65536").End(xlUp).Row + 1
-
-
- Cells(row_number, 1) = salutation
- Cells(row_number, 2) = TextBox_Last_Name.Value
- Cells(row_number, 3) = TextBox_First_Name.Value
- Cells(row_number, 4) = TextBox_Address.Value
- Cells(row_number, 5) = TextBox_Place.Value
- Cells(row_number, 6) = ComboBox_Country.Value
-
-
- OptionButton1.Value = True
- TextBox_Last_Name.Value = ""
- TextBox_First_Name.Value = ""
- TextBox_Address.Value = ""
- TextBox_Place.Value = ""
- ComboBox_Country.ListIndex = -1
- End If
- End Sub
Private Sub CommandButton_Close_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize() ''국가' 시트의 252개 국가 목록
For i = 1 To 252
ComboBox_Country.AddItem Sheets("Country").Cells(i, 1)
Next
End Sub
Private Sub CommandButton_Add_Click()
'이름 색상을 검정색으로 설정
Label_Last_Name.ForeColor = RGB(0, 0, 0)
Label_First_Name.ForeColor = RGB(0, 0, 0)
Label_Address.ForeColor = RGB(0, 0, 0)
Label_Place.ForeColor = RGB(0, 0, 0)
Label_Country.ForeColor = RGB(0, 0, 0)
'콘텐츠 제어
If TextBox_Last_Name.Value = "" Then '아무것도 지정하지 않은 경우...
Label_Last_Name.ForeColor = RGB(255, 0, 0) '제목 색상을 빨간색으로 설정
ElseIf TextBox_First_Name.Value = "" Then
Label_First_Name.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Address.Value = "" Then
Label_Address.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Place.Value = "" Then
Label_Place.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_Country.Value = "" Then
Label_Country.ForeColor = RGB(255, 0, 0)
Else
'양식이 채워지면 해당 값이 워크시트에 붙여넣어집니다.
Dim row_number As Integer, salutation As String
'항소의 선택
For Each salutation_button In Frame_Salutation.Controls
If salutation_button.Value Then
salutation = salutation_button.Caption
End If
Next
'row_number = 열 +1에서 비어 있지 않은 마지막 셀의 행 번호
row_number = Range("A65536").End(xlUp).Row + 1
'워크시트에 값 삽입
Cells(row_number, 1) = salutation
Cells(row_number, 2) = TextBox_Last_Name.Value
Cells(row_number, 3) = TextBox_First_Name.Value
Cells(row_number, 4) = TextBox_Address.Value
Cells(row_number, 5) = TextBox_Place.Value
Cells(row_number, 6) = ComboBox_Country.Value
'데이터를 삽입한 후 초기값을 반환합니다.
OptionButton1.Value = True
TextBox_Last_Name.Value = ""
TextBox_First_Name.Value = ""
TextBox_Address.Value = ""
TextBox_Place.Value = ""
ComboBox_Country.ListIndex = -1
End If
End Sub
다운로드할 Excel 파일은 다음과 같습니다. controls_exercise2.xls