Note: The other languages of the website are Google-translated. Back to English
Logáil isteach  \/ 
x
or
x
Cláraigh  \/ 
x

or

Conas sábháil a chosc má tá cill ar leith bán in Excel?

Mar shampla, dhear tú foirm i mbileog oibre agus roinn tú le do chomhghleacaithe. Tá súil agat go líonfaidh do chomhghleacaithe a n-ainmneacha sa chill shonrach le cur in iúl cé a chuir isteach san fhoirm seo, nó iad a chosc ar an bhfoirm a shábháil, conas a d’fhéadfá a dhéanamh? Tabharfaidh mé isteach macra VBA anseo chun cosc ​​a chur ar leabhar oibre a shábháil má tá an chill shonrach bán in Excel.

Cluaisín Oifige Cumasaigh Eagarthóireacht agus Brabhsáil Tabbed in Oifig, agus Déan Do Obair i bhfad Níos Éasca ...
Réitíonn Kutools for Excel an chuid is mó de do chuid Fadhbanna, agus Méadaíonn sé do Tháirgiúlacht 80%
  • Athúsáid Rud ar bith: Cuir na foirmlí, na cairteacha agus aon rud eile is úsáidí nó is casta le do rogha Favorites, agus athúsáid go tapa iad sa todhchaí.
  • Níos mó ná 20 gné téacs: Uimhir Sliocht as Teaghrán Téacs; Sliocht nó Bain Cuid de Théacsanna; Tiontaigh Uimhreacha agus Airgeadraí go Focail Bhéarla.
  • Cumaisc Uirlisí: Illeabhair Oibre agus Bileoga ina hAon; Cumaisc Ilchealla / Sraitheanna / Colúin Gan Sonraí a Chailleadh; Cumaisc Sraithe Dúblacha agus Suim.
  • Uirlisí Scoilt: Roinn Sonraí i Ilbhileoga Bunaithe ar Luach; Leabhar Oibre amháin le Ilchomhaid Excel, PDF nó CSV; Colún amháin go Colúin Iolracha.
  • Greamaigh gan bacadh Sraitheanna Hidden / Scagtha; Líon agus Suim de réir Dath Cúlra; Seol Ríomhphoist Phearsantaithe chuig Il-Faighteoirí i mBulc.
  • Scagaire Sármhaith: Ardscéimeanna scagaire a chruthú agus a chur i bhfeidhm ar aon bhileoga; Sórtáil de réir seachtaine, lae, minicíochta agus níos mó; scagairí le cló trom, foirmlí, trácht ...
  • Níos mó ná 300 gné chumhachtach; Oibríonn sé le hOifig 2007-2019 agus 365; Tacaíochtaí le gach teanga; Imscaradh éasca i d’fhiontar nó d’eagraíocht.


mboilgeog cheart gorm saigheadCosc a chur ar shábháil má tá cill ar leith bán in Excel

Chun cosc ​​a chur ar an leabhar oibre reatha a shábháil má tá an chill shonrach bán in Excel, is féidir leat an macra VBA seo a leanas a chur i bhfeidhm go héasca.

Céim 1: Oscail fuinneog Microsoft Visual Basic for Applications le brúigh an Eile + F11 eochracha idir an dá linn.

Céim 2: Sa Project Explorer, leathnaigh an VBAProject (Ainm do leabhair oibre.xlsm) agus Réada Microsoft Excel, agus ansin cliceáil faoi dhó ar an An Leabhar Oibre seo. Féach an scáileán ar chlé:

Céim 3: I bhfuinneog oscailte ThisWorkbook, greamaigh an macra VBA seo a leanas:

Macra VBA: Cosc a chur ar shábháil má tá cill ar leith bán

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
Nóta: Sa chód VBA, is é an "TÁSTÁIL" ainm sonrach na bileoige oibre, agus an "A1"is é an cill ar leith, agus is féidir leat iad a athrú de réir mar is gá duit.

Anois má tá an chill shonrach bán sa leabhar oibre reatha, nuair a shábhálann tú é, tagann bosca dialóige rabhaidh amach agus insíonn sé duit "Sábháil ar cealFéach an griangraf seo a leanas:


mboilgeog cheart gorm saigheadAirteagail gaolmhara


Na hUirlisí Táirgiúlachta Oifige is Fearr

Réitíonn Kutools for Excel an chuid is mó de do chuid Fadhbanna, agus Méadaíonn sé do Tháirgiúlacht 80%

  • Athúsáid: Cuir isteach go tapa foirmlí casta, cairteacha agus aon rud a d'úsáid tú roimhe seo; Cealla a Chriptiú le pasfhocal; Cruthaigh Liosta Ríomhphoist agus seol ríomhphoist ...
  • Barra Foirmle Super (cuir línte iolracha téacs agus foirmle in eagar go héasca); Leagan Amach Léitheoireachta (líon mór cealla a léamh agus a chur in eagar go héasca); Greamaigh go dtí an Raon Scagtha...
  • Cumaisc Cealla / Sraitheanna / Colúin gan Sonraí a chailleadh; Ábhar Cealla Scoilt; Comhcheangail Sraitheanna / Colúin Dúblacha... Cill Dúblach a Chosc; Déan comparáid idir Ranganna...
  • Roghnaigh Dúblach nó Uathúil Sraitheanna; Roghnaigh Blank Rows (tá na cealla uile folamh); Aimsigh Super agus Fuzzy Aimsigh i go leor Leabhar Oibre; Roghnaigh go randamach ...
  • Cóip Díreach Cealla Il gan tagairt fhoirmle a athrú; Tagairtí Cruthaigh Auto chuig Bileoga Il; Cuir Urchair isteach, Boscaí Seiceála agus go leor eile ...
  • Sliocht Téacs, Cuir Téacs leis, Bain de réir Poist, Bain Spás; Subtotals Paging a chruthú agus a phriontáil; Tiontaigh Idir Ábhar Cealla agus Tráchtanna...
  • Scagaire Super (scéimeanna scagaire a shábháil agus a chur i bhfeidhm ar bhileoga eile); Ard-Sórtáil de réir míosa / seachtaine / lae, minicíocht agus níos mó; Scagaire Speisialta le cló trom, iodálach ...
  • Comhcheangail Leabhair Oibre agus Bileoga Oibre; Cumaisc Táblaí bunaithe ar eochaircholúin; Roinn Sonraí i Ilbhileoga; Baisc Tiontaigh xls, xlsx agus PDF...
  • Níos mó ná 300 gné chumhachtach. Tacaíochtaí Office / Excel 2007-2019 agus 365. Tacaíonn sé le gach teanga. Imscaradh éasca i d’fhiontar nó d’eagraíocht. Gnéithe iomlána triail saor in aisce 30 lá. Ráthaíocht ar ais airgid 60 lá.
cluaisín kte 201905

Tugann Tab Oifige comhéadan Tabbed chuig Office, agus Déan Do Obair i bhfad Níos Éasca

  • Cumasaigh eagarthóireacht agus léamh tabbed i Word, Excel, PowerPoint, Foilsitheoir, Rochtain, Visio agus Tionscadal.
  • Oscail agus cruthaigh cáipéisí iolracha i gcluaisíní nua den fhuinneog chéanna, seachas i bhfuinneoga nua.
  • Méadaíonn do tháirgiúlacht 50%, agus laghdaíonn sé na céadta cad a tharlaíonn nuair luch duit gach lá!
bun 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 · 4 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 · 4 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 · 9 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 · 9 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 · 9 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 · 9 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 · 9 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 · 2 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 · 2 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.
    andrewgonzales048@gmail.com · 1 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 · 1 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!