В Excel VBA как отключить ячейку, если пользователь вводит имя файла без указания формата файла?

#excel #vba

Вопрос:

Я работаю в Excel VBA, и оба столбца «Сведения» и «статус» присутствуют.

Когда статус столбца «Завершено» равен Y, столбец «Имя файла» должен быть обновлен в соответствии с форматом файла abc.doc/abc.pdf/abc.jpeg. Если пользователь просто обновляет имя файла, ячейка не должна позволять пользователю это делать.

Как это должно быть сделано в Excel VBA?

Данные Excel

 Details Status Completed(Y/N) Y or N in Dropdown File Name abc.doc/ abc.pdf/abc.jpg   

Код

 ' If Completed is Y, then File name should be available with file extension (File format - .docx/.xlsx/.pdf/jpeg)  If Application.Sheets("Sheet1").Range("B2").Value = "Y" Then  If Application.Sheets("Sheet1").Range("B3").Value = "" Or Application.Sheets("Sheet1").Range("B3").Value = "" Then    MsgBox "Error: If 'Completed' is filled with Y then the file name must be filled with its file format .docx/.xlsx/.pdf/jpeg"  End If    ElseIf Application.Sheets("Sheet1").Range("B2").Value = "N" Then  If Application.Sheets("Sheet1").Range("B3").Value lt;gt; "" Then  MsgBox "Error : If 'Completed' is N then 'File Name' must be blank"  Range("B3").Value = ""  End If  End If   End If  

Ответ №1:

Я не уверен, чего вы пытаетесь достичь! Если вы просто хотите, чтобы пользователь не выбирал ячейку B3 (имя файла), то события Worksheet_SelectionChange может быть достаточно.

Поместите этот код в модуль Sheet1

 Private Sub Worksheet_SelectionChange(ByVal Target As Range)  ' if the user tries to select B3 then cell B2 will be selected  If Not Intersect(Target, Range("B3")) Is Nothing Then Target.Offset(-1, 0).Select End Sub