Note: The other languages of the website are Google-translated. Back to English
Vpiši se  \/ 
x
or
x
Registracija  \/ 
x

or

Kako preprečiti shranjevanje, če je določena celica v Excelu prazna?

Na primer, oblikovali ste obrazec na delovnem listu in ga delili s svojimi kolegi. Upate, da bodo vaši kolegi v posamezno celico vpisali svoja imena, da bodo označili, kdo je vstopil v ta obrazec, sicer jim preprečujejo, da bi shranili obrazec. Tukaj bom predstavil makro VBA, da preprečim shranjevanje delovnega zvezka, če je določena celica prazna v Excelu.

Kartica Office Omogočite urejanje in brskanje z zavihki v storitvi Office ter olajšajte svoje delo ...
Kutools za Excel rešuje večino vaših težav in poveča produktivnost za 80%
  • Ponovno uporabi vse: Med priljubljene dodajte najbolj uporabljene ali zapletene formule, grafikone in kar koli drugega ter jih v prihodnosti hitro ponovno uporabite.
  • Več kot 20 besedilnih funkcij: Izvleček številke iz besedilnega niza; Izvleči ali odstrani del besedil; Pretvorite številke in valute v angleške besede.
  • Združi orodja: Več delovnih zvezkov in listov v eno; Združi več celic / vrstic / stolpcev brez izgube podatkov; Združi podvojene vrstice in vsoto.
  • Razdeljena orodja: Razdeli podatke na več listov na podlagi vrednosti; En delovni zvezek v več datotek Excel, PDF ali CSV; En stolpec na več stolpcev.
  • Prilepi preskoči Skrite / filtrirane vrstice; Štetje in vsota glede na Barva ozadja; V večjem obsegu pošiljajte osebna e-poštna sporočila več prejemnikom.
  • Super filter: Ustvarite napredne filtrirne sheme in uporabite za vse liste; Razvrščanje po tednih, dnevih, pogostosti in še več; filter s krepkim tiskom, formule, komentar ...
  • Več kot 300 zmogljivih funkcij; Deluje s sistemoma Office 2007-2019 in 365; Podpira vse jezike; Preprosta namestitev v vašem podjetju ali organizaciji.


puščica modri desni mehurčekPreprečite shranjevanje, če je določena celica v Excelu prazna

Če želite preprečiti shranjevanje trenutnega delovnega zvezka, če je določena celica prazna v Excelu, lahko preprosto uporabite naslednji makro VBA.

1. korak: Odprite okno Microsoft Visual Basic for Applications s pritiskom na druga + F11 ključi v vmesnem času.

2. korak: V Raziskovalec projektov razširite VBAProject (ime vašega delovnega zvezka.xlsm) in Microsoft Excelovi predmetiin nato dvokliknite ThisWorkbook. Oglejte si posnetek zaslona levo:

3. korak: V začetno okno ThisWorkbook prilepite naslednji makro VBA:

VBA Macro: preprečite shranjevanje, če je določena celica prazna

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
Opomba: V kodi VBA je "TEST" določeno ime delovnega lista in "A1"je določena celica in jih lahko spremenite po potrebi.

Če je določena celica v trenutnem delovnem zvezku prazna, se pri shranjevanju odpre opozorilno pogovorno okno z napisom "Shranjevanje preklicano". Oglejte si naslednji posnetek zaslona:


puščica modri desni mehurčekPovezani članki


Najboljša orodja za pisarniško produktivnost

Kutools za Excel rešuje večino vaših težav in poveča produktivnost za 80%

  • Ponovna uporaba: Hitro vstavite zapletene formule, grafikoni in vse, kar ste že uporabljali; Šifriraj celice z geslom; Ustvari poštni seznam in pošiljanje e-pošte ...
  • Vrstica Super Formula (enostavno urejanje več vrstic besedila in formule); Bralna postavitev (enostavno branje in urejanje velikega števila celic); Prilepite v filtrirani obseg...
  • Združi celice / vrstice / stolpce brez izgube podatkov; Vsebina razdeljenih celic; Združi podvojene vrstice / stolpce... prepreči podvojene celice; Primerjaj obsege...
  • Izberite Duplicate ali Unique Vrstice; Izberite prazne vrstice (vse celice so prazne); Super Find in Fuzzy Find v mnogih delovnih zvezkih; Naključna izbira ...
  • Natančna kopija Več celic brez spreminjanja sklica formule; Samodejno ustvarjanje referenc na več listov; Vstavi oznake, Potrditvena polja in še več ...
  • Izvleček besedila, Dodaj besedilo, Odstrani po položaju, Odstrani presledek; Ustvari in natisni vmesne seštevke strani Pretvarjanje med vsebino celic in komentarji...
  • Super filter (shranite in uporabite sheme filtrov za druge liste); Napredno razvrščanje glede na mesec / teden / dan, pogostost in drugo; Poseben filter s krepko, ležeče ...
  • Združite delovne zvezke in delovne liste; Spoji tabele na podlagi ključnih stolpcev; Razdelite podatke na več listov; Paketna pretvorba xls, xlsx in PDF...
  • Več kot 300 zmogljivih funkcij. Podpira Office / Excel 2007-2019 in 365. Podpira vse jezike. Preprosta namestitev v vašem podjetju ali organizaciji. Vse funkcije 30-dnevnega brezplačnega preskusa. 60-dnevno jamstvo za vračilo denarja.
zavihek kte 201905

Kartica Office prinaša vmesnik z zavihki v Office in poenostavi vaše delo

  • Omogočite urejanje in branje z zavihki v Wordu, Excelu, PowerPointu, Publisher, Access, Visio in Project.
  • Odprite in ustvarite več dokumentov v novih zavihkih istega okna in ne v novih oknih.
  • Poveča vašo produktivnost za 50% in vsak dan zmanjša na stotine klikov z miško!
dno pisarniške mize
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 · 11 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!