+ Reply to Thread
Results 1 to 15 of 15

Run a VBA Code before printing in excel

  1. #1
    Registered User
    Join Date
    06-08-2022
    Location
    Hengelo
    MS-Off Ver
    2203
    Posts
    6

    Run a VBA Code before printing in excel

    Hi all,

    I would like to make users mandatory to fill-up some specific fields of an excel Form. The form consists within an excel file including different fields where the user must fill-in some of them as mandatory.
    Since the document is a template document (excel), I'd like to avoid be printed in case one of the mandatory fields are not filled-in.

    Form.jpg

    In order to perform that task, I am running a VBA using 'Beforeprint' declaration. However, I have no clue how to develop the rest of the code function. Would you mind please provide help? Thank you in advance.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Run a VBA Code before printing in excel

    A recent, similar, thread ...https://www.excelforum.com/excel-pro...ml#post5675374

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Run a VBA Code before printing in excel

    If you change 2 cells to be consistent with the others (*Tracking info required (Yes/No) & *Ship today (Yes/No))

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Run a VBA Code before printing in excel

    If you want it a little fancier.
    Please Login or Register  to view this content.

    Attachment 783351

  5. #5
    Registered User
    Join Date
    06-08-2022
    Location
    Hengelo
    MS-Off Ver
    2203
    Posts
    6

    Re: Run a VBA Code before printing in excel

    Thank you for your fast reply. However I am completely new in coding and I have many doubts.

    Please Login or Register  to view this content.
    //Does this function add the BeforePrinter task?

    Please Login or Register  to view this content.
    // This line means to check the entire Column 'A'? I need to check column 'B' instead, if
    // that's the case.

    Please Login or Register  to view this content.
    // Do you mean...personalize the comment?

    I do not manage to run it properly. The file still prints even though the B column cells are empty.

    Thank you in advance
    Last edited by Tonibco; 06-09-2022 at 09:39 AM.

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Run a VBA Code before printing in excel

    Attach a sanitized copy of your workbook.

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Run a VBA Code before printing in excel

    I made a representative workbook, I think, while waiting on a sanitized copy .
    Try it out and let us know.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    06-08-2022
    Location
    Hengelo
    MS-Off Ver
    2203
    Posts
    6

    Re: Run a VBA Code before printing in excel

    Woow! That previous example is so great! It is exactly what I expected. I only need the table Border lines when printing.

    Hereby a copy of my file.
    Attachment 783471

    Thank you very much!
    Last edited by Tonibco; 06-10-2022 at 06:19 AM.

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Run a VBA Code before printing in excel

    To attach a workbook, you have to use "Go Advanced" at the bottom right.
    The yellow marker at the top explains it.

    Do you want to add borders with code or manually set them?

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Run a VBA Code before printing in excel

    BTW, if you put this in the ThisWorkbook Module, you don't need a button on the sheet.
    You can use your normal print button on the toolbar as you did previously.
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-08-2022
    Location
    Hengelo
    MS-Off Ver
    2203
    Posts
    6

    Re: Run a VBA Code before printing in excel

    Hi Jolivanes,

    Thank you very much for your last update. It works now as expected. The Template will be only printed out when the mandatory fields are filled out.

    May I have some small idea what the code exactly does line by line, please? Would you mind to provide a short comment after the lines? For example I am aware that Chr(42 belongs to the Ascii table *) but not what the rest of the code does.

    I am very curious to get a more detailed idea.

    Thank you in advance.

  12. #12
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Run a VBA Code before printing in excel

    Based on the code in Post #10.
    Line #1:
    Is invoked when printer button is clicked.
    Line #2:
    Declaring variables as needed.
    Line #3:
    Loop through each cell from the first cell in Column A to the last used cell in Column A
    One should have a header row and start from the 2nd cell.
    Line #4:
    Check if the left most character in the cell is an asterisk (Chr 42) and if the cell beside it to the right in empty.
    Line #5:
    cels is an array that holds data.
    Check if it has anything in it.
    Line #6:
    If Line #5 is True (array is empty), it will enter the needing attention string, put in a couple linefeeds and the address of the cell that needs attention.
    Line #7:
    If Line #5 is False, it will do what Line #8 says.
    Line #8:
    Add a linefeed to the holding array (cels) and add the address of the cell to the right of the cell that is being checked for the asterisk.
    Line #9:
    End of the middle block of If.... Then statements.
    Line #10:
    End of the outside block of If.... Then statements.
    Line #11:
    Go to the next cell in Column A and go through Line #4 to Line #10 again. This is repeated until all cells in Column A and B have been checked.
    Line #12:
    Checks if the holding array has data in it.
    Line #13:
    If Line 12 is True, it will show the data in the holding array (cels)
    Line #14:
    Cancels the execution of events.
    Line #15:
    End of the block of If.... Then statements.
    Line #16:
    That is it for today folks. End of the show.
    Experience trumps academics every day of the week and twice on Sunday.

  13. #13
    Registered User
    Join Date
    06-08-2022
    Location
    Hengelo
    MS-Off Ver
    2203
    Posts
    6

    Re: Run a VBA Code before printing in excel

    hahhaha Last line made me laugh in a 'for' loop manner.

    Line #16:
    That is it for today folks. End of the show.


    Thank you so much for your help. Awesome!

  14. #14
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Run a VBA Code before printing in excel

    Good to hear that all is well in Twente.
    Good Luck

  15. #15
    Registered User
    Join Date
    06-08-2022
    Location
    Hengelo
    MS-Off Ver
    2203
    Posts
    6

    Re: Run a VBA Code before printing in excel

    Dear Colleagues,

    I have actually a problem when I upload the excel file into my company cloud. Somehow the 'Developer' functionality does not take effect. Does it occur due to the lack of the 'Developer' functionality in the cloud (excel in the cloud)?
    My expectation would be to be able to use that funcionality in the cloud as well, not only when someone download the file in his/her own computer. Do I have to use any script in order to solve it? Would you mind please give me a hand?

    Thank you very much in advance.

    Toni

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Code Printing list of hyperlinks in excel file
    By reimar_rem in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-31-2018, 11:34 AM
  2. Replies: 1
    Last Post: 06-11-2014, 09:23 AM
  3. Cancel printing VBA code doesn't works on excel 2010
    By saesaria in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-28-2013, 06:08 AM
  4. Replies: 0
    Last Post: 07-18-2012, 03:57 AM
  5. Printing a Word doc from Excel VBA code
    By BruceInMiami in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2005, 09:05 PM
  6. [SOLVED] Printing a Pivot Table from code - Excel 2003
    By Newbie in forum Excel General
    Replies: 4
    Last Post: 01-05-2005, 02:06 PM
  7. Printing a Pivot Table from code - Excel 2003
    By Newbie in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-05-2005, 02:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1