Note: The other languages of the website are Google-translated. Back to English
Log på  \/ 
x
or
x
Tilmeld  \/ 
x

or

Hvordan undgår man at gemme, hvis en bestemt celle er tom i Excel?

For eksempel designede du en formular i regneark og delte med dine kolleger. Du håber, at dine kolleger udfylder deres navne i den specifikke celle for at angive, hvem der har indtastet denne formular, ellers forhindrer de dem i at gemme formularen. Hvordan kunne du gøre det? Her vil jeg introducere en VBA-makro for at forhindre at gemme en projektmappe, hvis den specifikke celle er tom i Excel.

Fanen Office Aktiver redigering og browsing med faner i Office, og gør dit arbejde meget lettere ...
Kutools til Excel løser de fleste af dine problemer og øger din produktivitet med 80%
  • Genbrug alt: Tilføj de mest anvendte eller komplekse formler, diagrammer og alt andet til dine favoritter, og genbrug dem hurtigt i fremtiden.
  • Mere end 20 tekstfunktioner: Uddrag nummer fra tekststreng; Uddrag eller fjern en del af teksterne; Konverter tal og valutaer til engelske ord.
  • Flet værktøjer: Flere arbejdsbøger og ark til én; Flet flere celler / rækker / kolonner uden at miste data; Flet duplikatrækker og sum.
  • Opdel værktøjer: Opdel data i flere ark baseret på værdi; Én projektmappe til flere Excel-, PDF- eller CSV-filer; En kolonne til flere kolonner.
  • Indsæt spring Skjulte / filtrerede rækker; Tæl og sum af baggrundsfarve; Send personaliserede e-mails til flere modtagere i bulk.
  • Superfilter: Opret avancerede filterskemaer og anvend dem på alle ark; Sort efter uge, dag, hyppighed og mere; filtre med fed, formler, kommentar ...
  • Mere end 300 kraftfulde funktioner; Arbejder med Office 2007-2019 og 365; Understøtter alle sprog Nem implementering i din virksomhed eller organisation.


pil blå højre bobleUndgå at gemme, hvis en bestemt celle er tom i Excel

For at forhindre at gemme den aktuelle projektmappe, hvis den specifikke celle er tom i Excel, kan du nemt anvende følgende VBA-makro.

Trin 1: Åbn vinduet Microsoft Visual Basic til applikationer ved at trykke på andre + F11 nøgler i mellemtiden.

Trin 2: I Project Explorer skal du udvide VBAProject (dit projektbogsnavn.xlsm) og Microsoft Excel-objekter, og dobbeltklik derefter på ThisWorkbook. Se skærmbillede til venstre:

Trin 3: Indsæt følgende VBA-makro i det åbne ThisWorkbook-vindue:

VBA-makro: Undgå at gemme, hvis en bestemt celle er tom

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
Bemærk: I VBA-koden er "TEST" det specifikke regnearknavn og "A1"er den specifikke celle, og du kan ændre dem efter behov.

Hvis den specifikke celle nu er tom i den aktuelle projektmappe, når du gemmer den, kommer en advarselsdialogboks ud og fortæller dig "Gem annulleret". Se følgende skærmbillede:


pil blå højre bobleRelaterede artikler


De bedste Office-produktivitetsværktøjer

Kutools til Excel løser de fleste af dine problemer og øger din produktivitet med 80%

  • Genbruge: Indsæt hurtigt komplekse formler, diagrammer og alt, hvad du har brugt før; Krypter celler med adgangskode Opret postliste og send e-mails ...
  • Super formel bar (let redigere flere linjer med tekst og formel); Læsning Layout (let at læse og redigere et stort antal celler); Indsæt til filtreret rækkevidde...
  • Flet celler / rækker / kolonner uden at miste data; Split celler indhold; Kombiner duplikerede rækker / kolonner... Forhindre duplikerede celler; Sammenlign områder...
  • Vælg Duplicate eller Unique Rækker; Vælg tomme rækker (alle celler er tomme); Super Find og Fuzzy Find i mange arbejdsbøger; Tilfældig valg ...
  • Præcis kopi Flere celler uden at ændre formelreference; Auto Opret referencer til flere ark; Indsæt kugler, Afkrydsningsfelter og mere ...
  • Uddrag tekst, Tilføj tekst, Fjern efter position, Fjern mellemrum; Opret og udskriv personsøgningssubtotaler; Konverter mellem celler indhold og kommentarer...
  • Superfilter (gem og anvend filterskemaer på andre ark); Avanceret sortering efter måned / uge / dag, hyppighed og mere; Specielt filter af fed, kursiv ...
  • Kombiner arbejdsbøger og arbejdsark; Fletabeller baseret på nøglekolonner; Opdel data i flere ark; Batch Konverter xls, xlsx og PDF...
  • Mere end 300 kraftfulde funktioner. Understøtter Office / Excel 2007-2019 og 365. Understøtter alle sprog. Nem implementering i din virksomhed eller organisation. Fuld funktioner 30-dages gratis prøveperiode. 60-dages pengene tilbage garanti.
kte-fane 201905

Fanen Office bringer en grænseflade til et kontor med Office, og gør dit arbejde meget lettere

  • Aktiver redigering og læsning af faner i Word, Excel, PowerPoint, Publisher, Access, Visio og Project.
  • Åbn og opret flere dokumenter i nye faner i det samme vindue snarere end i nye vinduer.
  • Øger din produktivitet med 50% og reducerer hundredvis af museklik for dig hver dag!
officetab bund
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 · 6 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 · 11 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 · 11 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 · 11 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 · 11 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 · 2 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 · 2 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 · 4 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 · 4 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 · 20 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!