皆さん、私が作成したリストのセルにユーザーが全く同じデータを入力できるコードを書く方法を考えようとしています。私は、コードがユーザーのエントリを検証し、そのエントリがリストにあるかどうかを確認し、ユーザーが入力したデータを消去してメッセージを表示しないかどうかを確認します。私はほとんどそこにいますが、私はVlookの仕事をしていないようです。どんな助けでも非常に感謝しています。以下は、私が書いたコードです。ユーザーが誤ったデータをセルに入力しないようにするためのVBAコードExcel
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim rng As Range
Dim aCell As Range
Dim LU As Range
Dim Celda As Range
'
On Error GoTo Whoa
Application.EnableEvents = False
'~~> This is the range of cells where I want the user to input the data
Set rng = Range("A1:A15")
'THis is the range where I want the macro to look if the data the user entered is in the list
Set LU = Application.WorksheetFunction.VLookup(aCell.Value, Range("F1:F5"), 1, False)
On Error GoTo MyErrorHandler:
If Not Application.Intersect(Target, rng) Is Nothing Then
'~~> Loop through all cells in the range
For Each aCell In rng
If aCell.Value <> "" Then
If aCell.Value <> LU Then
aCell.ClearContents
MyErrorHandler:
If Err.Number = 1004 Then
aCell.ClearContents
MsgBox "Item Number es Incorrecto - Error en la celda " & aCell.Address
End If
End If
End If
Next
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
データ検証でこれを正確に行うことができるVBAを使用する理由は何ですか? – nbayly
なぜ検証リストを使用していませんか? http://www.contextures.com/xlDataVal01.html – RCaetano
@nbayly 15秒早くxD – RCaetano