Note: The other languages of the website are Google-translated. Back to English
ログイン  \/ 
x
or
x
今すぐ登録  \/ 
x

or

Excelで特定のセルが空白の場合に保存を防ぐにはどうすればよいですか?

たとえば、ワークシートでフォームをデザインし、同僚と共有したとします。 同僚が特定のセルに名前を入力して、誰がこのフォームに入力したかを示すことを望みます。そうしないと、同僚がフォームを保存できないようにします。 ここでは、Excelで特定のセルが空白の場合にブックを保存しないようにするためのVBAマクロを紹介します。

OfficeタブOfficeでタブ付きの編集と閲覧を有効にし、作業をはるかに簡単にします...
Kutools for Excelはほとんどの問題を解決し、生産性を80%向上させます
  • 何でも再利用: 最もよく使用される、または複雑な数式、グラフなどをお気に入りに追加して、後ですぐに再利用できます。
  • 20以上のテキスト機能: テキスト文字列から数値を抽出します。 テキストの一部を抽出または削除します。 数字と通貨を英語の単語に変換します。
  • マージツール:複数のワークブックとシートをXNUMXつに。 データを失うことなく複数のセル/行/列をマージします。 重複する行と合計をマージします。
  • 分割ツール:値に基づいてデータを複数のシートに分割します。 XNUMXつのワークブックから複数のExcel、PDF、またはCSVファイル。 XNUMX列から複数列。
  • 貼り付けスキップ 非表示/フィルタリングされた行; カウントと合計 背景色別; パーソナライズされた電子メールを複数の受信者にまとめて送信します。
  • スーパーフィルター: 高度なフィルタースキームを作成し、任意のシートに適用します。 ソート 週、日、頻度など。 フィルタ 太字、数式、コメント...
  • 300以上の強力な機能。 Office2007-2019および365で動作します。 すべての言語をサポートします。 企業や組織に簡単に導入できます。


矢印青い右バブルExcelで特定のセルが空白の場合は保存しない

Excelで特定のセルが空白の場合に現在のブックが保存されないようにするには、次のVBAマクロを簡単に適用できます。

ステップ1:を押してMicrosoft Visual Basic forApplicationsウィンドウを開きます 他の + F11 その間にキー。

ステップ2:プロジェクトエクスプローラーで、 VBAProject(ワークブック名​​.xlsm) Microsoft Excelオブジェクト、次にダブルクリックします このワークブック。 左のスクリーンショットを参照してください。

手順3:ThisWorkbookウィンドウを開いて、次のVBAマクロを貼り付けます。

VBAマクロ:特定のセルが空白の場合に保存を防止する

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.Sheets("TEST").Range("A1").Value = "" Then
Cancel = True
MsgBox "Save cancelled"
End If
End Sub
注:VBAコードでは、「TEST」は特定のワークシート名であり、「A1"は特定のセルであり、必要に応じて変更できます。

現在のブックで特定のセルが空白の場合、それを保存すると、警告ダイアログボックスが表示され、「保存キャンセル"。次の​​スクリーンショットを参照してください。


矢印青い右バブル関連記事


最高のオフィス生産性ツール

Kutools for Excelはほとんどの問題を解決し、生産性を80%向上させます

  • 再利用: すばやく挿入 複雑な数式、チャート および以前に使用したものすべて。 セルを暗号化する パスワード付き。 メーリングリストを作成する そしてメールを送る...
  • スーパーフォーミュラバー (複数行のテキストと数式を簡単に編集できます); 読書レイアウト (多数のセルを簡単に読み取って編集する); フィルター範囲に貼り付け...
  • セル/行/列をマージする データを失うことなく; 分割セルコンテンツ; 重複する行/列を組み合わせる...重複セルを防止します。 範囲を比較する...
  • [複製]または[一意]を選択します 行; 空白行を選択 (すべてのセルは空です); スーパーファインドとファジーファインド 多くのワークブックで; ランダム選択...
  • 正確なコピー 数式参照を変更せずに複数のセル。 参照の自動作成 複数のシートに; 箇条書きを挿入、チェックボックスなど...
  • テキストを抽出、テキストの追加、位置による削除、 スペースを削除する; ページング小計の作成と印刷。 セルの内容とコメントを変換する...
  • スーパーフィルター (フィルタースキームを保存して他のシートに適用します); 高度な並べ替え 月/週/日、頻度など。 特殊フィルター 太字、斜体...
  • ワークブックとワークシートを組み合わせる; キー列に基づいてテーブルをマージします。 データを複数のシートに分割; xls、xlsx、PDFをバッチ変換...
  • 300以上の強力な機能。 Office / Excel2007-2019および365をサポートします。すべての言語をサポートします。 企業や組織に簡単に導入できます。 全機能30日間の無料トライアル。 60日間の返金保証。
kteタブ201905

Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性が50%向上し、毎日何百ものマウスクリックが減ります。
officetab下部
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Aditya · 5 months ago
    This is not working, it states save cancelled but still ends up saving the workbook
    • To post as a guest, your comment is unpublished.
      kellytte · 5 months ago
      Note: In the VBA code, the "TEST" is the specific worksheet name, and the "A1" is the specific cell, and you can change them as you need.

      For example, your sheet is named as "Sheet1", and the specified cell is B2, you need to change the sheet name and cell address in the VBA code before running it
  • To post as a guest, your comment is unpublished.
    Happy · 10 months ago
    I tried above formula which works. May i know is there any formula can force user to fill in before they can save? As i set the pull down menu "Please select", "Yes" or "No" for them to select. But they always forgot to select that field and remain "Please select". If i add this VBA code only apply cell is blank. Much appreciate you can advise. Thank you
    • To post as a guest, your comment is unpublished.
      kellytte · 10 months ago
      Hi Happy,
      Just replace the empty value “Sheets("TEST").Range("A1").Value = ""” to the specified text “Sheets("TEST").Range("A1").Value = "Please select"
      And the whole code will be changed as below:

      Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) If Application.Sheets("TEST").Range("A1").Value = "Please select" Then Cancel = True MsgBox "Save cancelled" End If End Sub


  • To post as a guest, your comment is unpublished.
    Happy · 10 months ago
    Hi, I tried above formula which works. May i know is there any formula can force user to fill in before they can save? As i set the pull down menu "Please select", "Yes" or "No" for them to select. But they always forgot to select that field and remain "Please select". If i add this VBA code only apply cell is blank. Much appreciate you can advise. Thank you
  • To post as a guest, your comment is unpublished.
    Am1n · 10 months ago
    Hi
    I have a VBA code that sorts and filters data from one excel table and save 48 different reports on my desktop. but based on those filters, some generated reports have only 1 row (headers) and no data. How can I add some VBA code to my file that prevents to save files that has just one row (header) and no data?
    Thank you
  • To post as a guest, your comment is unpublished.
    Amin · 10 months ago
    Hi
    I have a VBA code that sorts and filters data from one excel table and save 48 different reports on my desktop. but based on those filters, some generated reports have only 1 row (headers) and no data. How can I add some VBA code to my file that prevents to save files that has just one row (header) and no data?
    Thank you
  • To post as a guest, your comment is unpublished.
    Benjamin · 1 years ago
    good afternoon, I used the code above and it worked perfectly. my question is what should the code look like if I want to test on 2 cells? I am quite desperate. thanking you I advance for your assistance
  • To post as a guest, your comment is unpublished.
    Yzelle · 1 years ago
    I have a very big spreadsheet that contains a lot of info.
    Can someone please help me with a code to copy into VBA - I want it to be that if Cell C2-C1000+ have any info in them then cell O2-O1000+ and P2-P1000+ requires user input - however if a cell in Column C is empty then the cell in Column O & P can be empty as well. (for example) if cell C3 doesn't have any data input then cell O3-P3 can be empty.

    Thank you :)
    • To post as a guest, your comment is unpublished.
      kellytte · 1 years ago
      Hi Yzelle,
      Please remember to place below code into “ThisWorkbook” script window, and rename the worksheet name “Test” in the below code based on your condition.

      Dim xIRg As Range
      Dim xSRg As Range
      Dim xBol As Boolean
      Dim xInt As Integer
      Dim xStr As String
      If ActiveSheet.Name = "Test" Then
      Set xRg = Range("C:C")
      Set xRRg = Intersect(xRg.Worksheet.UsedRange, xRg)
      xBol = False
      On Error Resume Next
      For xInt = 1 To xRRg.Count
      Set xIRg = xRRg.Item(xInt)
      If xIRg.Value2 <> "" Then
      Set xSRg = Nothing
      If (Range("O" & xIRg.Row) = "") Or (Range("P" & xIRg.Row) = "") Then
      xBol = True
      Exit For
      End If
      End If
      Next
      If xBol Then
      Cancel = True
      MsgBox "Save cancelled"
      End If
      End If
      End Sub
      • To post as a guest, your comment is unpublished.
        Fatos Gaxha · 3 months ago
        Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

        With Sheets("Sheet1")
        If WorksheetFunction.CountA(.Range("A1:A4")) <> WorksheetFunction.CountA(.Range("B1:C4")) / 2 Then
        Cancel = True
        MsgBox "Please enter a values in columns B and C", vbCritical, "Error!"
        End If
        End With

        End Sub


        Just change the range from a to c, and from b to o and p
        hope it will help
      • To post as a guest, your comment is unpublished.
        Fatos Gaxha · 3 months ago
        Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

        With Sheets("Sheet1")
        If WorksheetFunction.CountA(.Range("A1:A4")) <> WorksheetFunction.CountA(.Range("B1:C4")) / 2 Then
        Cancel = True
        MsgBox "Please enter a values in columns B and C", vbCritical, "Error!"
        End If
        End With

        End Sub


        Just change the range from a to c, and from b to o and p
        hope it will help
        • To post as a guest, your comment is unpublished.
          Reece · 10 days ago
          Do you have a way that this can be coded so that either B or C need to be populated but its not required to populate both?
  • To post as a guest, your comment is unpublished.
    andrewgonzales048@gmail.com · 2 years ago
    This is really great. Do you know what I can do to make this work for a range of sheets and a number of cells? Also, these cells cannot always be the same, as there are sheets generated in this specific workbook which may not have the same cell needing to be filled each time. The cells will always be in the same column, just above the page border which is also generated. Thanks!
  • To post as a guest, your comment is unpublished.
    mhoferica@gmail.com · 2 years ago
    Hi, very useful. BUT there is a problem when I use it for files on the sharepoint. The changes are not saved but a new version is created that is displayed when reopening which is quite confusing. Is it possible to disable these new versions ?
  • To post as a guest, your comment is unpublished.
    Wkai · 2 years ago
    Hi i want to ask if it is from A2 to U2. what should i write?
    • To post as a guest, your comment is unpublished.
      kellytte · 2 years ago
      Hi Wkai,
      Try this VBA code:
      (This VBA code will detect Range A2:E5 in the Sheet “Test”, and cancel saving if there are blank cells existing in the range.)

      Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      Dim xWSName As String
      Dim xRgAddress As String
      Dim xRg As Range
      Dim xWs As Worksheet
      Dim xFNRg As Range
      xWSName = "TEST"
      xRgAddress = "A2:E5"
      Set xWs = Application.ActiveWorkbook.Worksheets.Item(xWSName)
      Set xRg = xWs.Range(xRgAddress)
      Set xFNRg = Nothing
      On Error Resume Next
      Set xFNRg = xRg.SpecialCells(xlCellTypeBlanks, 23)
      If Not TypeName(xFNRg.count) = "Nothing" Then
      Cancel = True
      MsgBox "Save cancelled"
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    edusuro · 3 years ago
    hi - this was super helpful... Just had one question, how do I save the file without a value in that field? As I try to save, the VBA code will pop the "Save Cancelled" message which is the intended response, however, need to save once without a value to create the form to be reused.

    Thanks!
    • To post as a guest, your comment is unpublished.
      kelly.extendoffice@gmail.com · 3 years ago
      Hi Eduardo,
      What about typing a space in the specified cell to pretend to a blank cell? Please remind to remove the space in future!