これはかなり簡単なコードですが、VBAを書かなかったのかどうかは分かりません。 2つのサブルーチンが必要です。 VBEで "Project - VBAProject"ペインのワークシートをダブルクリックしてください。鉱山は
我々はワークシートのイベントに基づいてサブルーチンを使用しようとしている「シート1」タブで、ブック「Book1.xlsという」用に構築されています。イベントは、VBAのコードを起動するフロントエンド(セルの選択など)で発生するものです。
SelectionChange - このワークシートイベントは、ユーザーがセル/範囲の選択を変更するたびに発生します。ユーザーがセルC5に切り替えたかどうかを検出するために使用します。
変更 - このワークシートのイベントは、誰かがセルの値を変更したときに発生します。私たちは、A5は、以下を置き、あなたのVBEのコードエディタペインで
を変更されたかどうかを確認するためにそれを使用する:
Private Sub Worksheet_Change(ByVal Target As Range)
'This subroutine fires when a cell value changes in this worksheet.
'did someone change something specifically in cell A5?
If Not Intersect(Target, Sheet1.Range("A5")) Is Nothing Then
'Is the value A or C?
If Sheet1.Range("A5").Value = "A" Or Sheet1.Range("A5").Value = "C" Then
'Remove any data validation for this cell:
Sheet1.Range("C5").Validation.Delete
'and change the value of C5 to "Fill in this cell"
Sheet1.Range("C5").Value = "Fill in this cell"
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This subroutine fires when a user selects a different cell or range.
'So... it fires ALL The time so the next line is super important.
'Did someone change selection specifically to cell C5?
If Not Intersect(Target, Sheet1.Range("C5")) Is Nothing Then
'Is the value currently "Fill in this cell"?
If Sheet1.Range("C5").Value = "Fill in this cell" Then
'Empty the cell
Sheet1.Range("C5").Value = ""
'Add data validation to some list somewhere
With Sheet1.Range("C5").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$J$1:$J$4" 'This the range that the list exists in
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
End If
End Sub
私はコメントで重要なものを指摘してきました。あなたのニーズに合うように、おそらくそれを少し猿にする必要があります。最も顕著なのは、セル検証コードです。現在、C5のデータ検証はJ1:J4
に設定されています。あなたのニーズに合わせてそれを変更してください。
私は[excel-vba]タグを追加しました。そうしないと、誰もその質問が表示されません。しかし、人々が助けようとする前に、彼らはあなたが働くと思うコードを見たいと思うでしょう。 – YowE3K
これには*簡単なマクロを書くことはできませんが、VBAを使うことは間違いありません。ユーザーのアクションに応答するには、worksheet_changeおよびworksheet_selectionchangeイベントを使用する必要があります。 –