저는 VBA를 처음 사용합니다. 시트에 입력 된 데이터의 길이와 형식을 확인하는 프로젝트를 진행 중입니다. 모든 잠재적 헤더 (전화 번호, 주소, 접미사 등)에 대한 유효성 검사를 포함하도록 코드를 확장하겠습니다. 그러나이 코드 줄에 "Columns (col) .NumberFormat ="0 ""이라는 런타임 오류 1004가 전화 번호 하위에 표시됩니다. 해당 줄을 주석 처리하면 다음 줄에서 동일한 오류가 발생합니다. 이 문제가 발생하는 이유와 해결 방법에 대한 도움말 / 설명을 구합니다.
감사!
Option Explicit
Sub DataVerification()
Dim i As Long
Dim rw As Long
Dim col As Long
Dim rng As Range
'Set cell background color to nothing
ActiveSheet.Cells.Interior.ColorIndex = xlNone
'loop through header row A10:F10 to determine which column needs validation
For i = 1 To 6
With Sheets("Sheet2")
If UCase(.Cells(10, i).Value) = "PHONE NUMBERS" Then
Call PhoneNumbers
ElseIf UCase(.Cells(10, i).Value) = "ADDRESSES" Then
Call Addresses
ElseIf UCase(.Cells(10, i).Value) = "SUFFIXES" Then
Call Suffixes
Else
MsgBox ("No data entered")
Exit For
End If
End With
Next i
'Phone numbers text found?
If col = 0 Then
MsgBox "Phone Numbers Header not found"
Exit Sub
End If
'Set column format to number
Columns(col).NumberFormat = "0"
'set up the start range, loop until we find an empty cell
'tidy up
Set rng = Nothing
End Sub
Sub PhoneNumbers()
Dim i As Long
Dim rw As Long
Dim col As Long
Dim rng As Range
col = i
'Set column format to number
Columns(col).NumberFormat = "0"
Set rng = Sheets("Sheet2").Cells(11, col)
Do Until rng = ""
If Not IsNumeric(rng.Value) Or Len(rng.Value) <> 11 Then
'highlight cell
rng.Interior.ColorIndex = 3 'red
End If
'get next row
Set rng = rng.Offset(1, 0)
Loop
End Sub
귀하의 코드에서 Col은 0입니다. 열은 인덱스 1에서 시작합니다. 시도해보십시오.
Col = 1
'Set column format to number
Columns(col).NumberFormat = "0"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다