+ Reply to Thread
Results 1 to 23 of 23

Self deleting cell formulas

  1. #1
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Self deleting cell formulas

    Hello,

    I have a curious issue I wonder if anyone has any thoughts on:

    I have a userform that feeds information into a database. The database has it's own worksheet and the userform is in the same workbook.

    The database is populated by the userform. On the database worksheet, I have an additional column that assigns a unique number every time a new database entry is submitted via the userform. Over time, the database is populated row by row. Each row has it's own unique number using this formula:

    =IF(C55<>"",1+MAX($B$50:B54),"")

    This formula sits in column B and basically says "If any value appears in column C (which happens when someone fills out and submits the userform), then find the biggest number in ALL of column B and +1 to it".

    This works well but the issue is if someone deletes an entry in the database, all the numbers reassign themselves over the remaining entries. I was trying to find a formula that could be added to the above along the lines of: "When a value (generated by the above formula for example) appears in the cell, keep that value but delete the background cell formula (like an automatic copy-paste-special-values)".

    Any thoughts would be much appreciated.
    Last edited by SJJ; 02-13-2012 at 09:33 AM. Reason: Issue Solved

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Self deleting cell formulas

    Generate the ID within the VBA code
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    I am new to VBA and am very much finding my way around it. I understand how to attach code to buttons and objects but I am not sure about adding background instructions to achieve something like a unique identifier. Would you be able to point me in the right direction, perhaps suggest the first line so I can have a go at figuring the rest out?

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Self deleting cell formulas

    You can use Application.WorksheetFunction pretty much the way you have in the sheet.

    Post back if you can't do it using the help files.

  5. #5
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    I'm having trouble applying the formula to a column of cells through VBA. In Excel I was able to drag my formula down my column and the cell references would change automatically. In VBA I am able to set ranges or specific cells but am not sure about translating my Excel thinking into VBA. Thank you for the pointer in the last reply - learnt a lot of other things but feel I'm missing some understanding for this particular question.

  6. #6
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,265

    Re: Self deleting cell formulas

    In VBA, a quick copy/pastevalues example...
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    Thanks for the suggestion (I think I'm slowly getting there!)

    When a value appears in the "C" column, the unique number generated is in the corresponding "B" column. Although the Max function bit doesn't work yet (might need help here), I've got something for when a value appears in one cell but how do I replicate it down so I don't have to write a working version of this code for

    If Range ("C56")...
    If Range ("C56")...
    If Range ("C57")...
    ...and so on (the Database will go up to C1050).

    Set myRange = Worksheets("Master_Database").Range("B50:B1050")
    If Range("C55").Value <> "" Then
    Range("B55").Value = 1 + Max(myRange)
    End If

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Self deleting cell formulas

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.



  9. #9
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    Thanks snb, I'll have a go at your suggestion. Just to clarify my last post, I have managed to make my ID number work for an individual cell but how do I avoid copying the below (now working) code replacing the 3rd line (If Range ("C60...) with

    If Range ("C61...
    If Range ("C62...
    If Range ("C62...
    etc. (up to If Range ("C1050...)?


    Set myRange = Worksheets("Master_Database").Range("B50:B1050")
    answer = Application.WorksheetFunction.Max(myRange)
    If Range("C60") <> "" Then
    Range("B60") = answer + 1
    End If

  10. #10
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    I've understood the latest suggestion from snb but have a different version of the same issue. Is anyone able to put me out of my misery for my previous post?

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Self deleting cell formulas

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

  12. #12
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    Just reposting my reply to comply with rules (apologies):

    Thanks snb, I'll have a go at your suggestion. Just to clarify my last post, I have managed to make my ID number work for an individual cell but how do I avoid copying the below (now working) code replacing the 3rd line (If Range ("C60...) with

    If Range ("C61...
    If Range ("C62...
    If Range ("C62...
    etc. (up to If Range ("C1050...)?


    Please Login or Register  to view this content.

  13. #13
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,265

    Re: Self deleting cell formulas

    Try this in the worksheet code module...
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    Thanks for putting this together but unfortunately, I don't seem to be able to make it work. Do I need to define any parts of the code? I put it in it's own module and also tried adding it to the userform code too but no luck. I'll post a fuller picture of the setup I have:

    My workbook has three worksheets. One is called "Master_Database", the other is "New_Entry" and the last "Print_Copy". The "New_Entry" sheet is just a button that one can click and it makes a userform pop up. All the userform boxes are filled in by the user and then the user clicks on a button at the bottom of the userform that I've called "Submit". This triggers the following code: As you can see, it sends the userform contents to the "Master_Database" worksheet as a row of data (and sends the contents of this row to a "Print_Copy" template (but that's irrelevant for now)), it then clears the userform ready for a new entry:

    Please Login or Register  to view this content.
    The "Master_Database" worksheet is laid out with the headings "Answer1", "Answer2", "Answer3" in cells "C49", "D49", "E49". In "B49" I've called it "UniqueID". In "B50" I have 000000 and in the rest of the row (C50, D50, E50), I have "Example".

    Currently, when the userform is submitted, Columns C, D, E populate successfully but I would like when this happens, for Column B to aswell populate with 000001, 000002,000003 etc.

    I think dangelor's suggestion is almost there but just having problems implementing it.

    Thank you to everyone so far!

  15. #15
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    Helpful thread? Here

    Hello, after much searching, I think the above thread might be the answer. Would anyone be able to make it work with my question? I've tried but I'm struggling.

  16. #16
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,265

    Re: Self deleting cell formulas

    To utilize the code, right click the name tab on the Master_Database worksheet. Select View Code. Paste the code into the window that opens. Every time something on the worksheet changes, such as adding a line of data, the code will execute.

  17. #17
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    Quote Originally Posted by dangelor View Post
    To utilize the code, right click the name tab on the Master_Database worksheet. Select View Code. Paste the code into the window that opens. Every time something on the worksheet changes, such as adding a line of data, the code will execute.
    I've sort of got it to work now (it's filling in the right column but doesn't recognize that the database headings are on row 50 and the data fills in from row 51, then 52, 53 etc.) but I'll explain why I won't be able to use it:

    The background to the issue I had using the spreadsheet formula as shown in my first post was that if an entry in the database was deleted, all the Number IDs would reassign themselves. So where you had 001, 002, 003, 004, 005 for example, if you deleted database row 004, the numbers would change to 001, 002, 003, 004 instead of: 001, 002, 003, 005. This issue could be solved by getting VBA code to assign the number but that's where I got stuck. See my post before last for the full layout of the spreadsheet / database I have.

    Thanks for the suggestion though, I feel I'm getting closer to the solution!

  18. #18
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,265

    Re: Self deleting cell formulas

    Can you post a dummy file?

  19. #19
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Self deleting cell formulas

    If your form is writing to the database file, why not have it simply overwrite the formula with its value after entering the new data?
    Good luck.

  20. #20
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Self deleting cell formulas

    I suggested how to do this, gave you some leads not completed code as you asked. You haven't even used the suggestions.

    You have a userform that adds data to the sheet so you don't need sheet formulas for this.

    If the unique number is in Column A then

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Self deleting cell formulas

    Quote Originally Posted by royUK View Post
    I suggested how to do this, gave you some leads not completed code as you asked. You haven't even used the suggestions.

    You have a userform that adds data to the sheet so you don't need sheet formulas for this.

    If the unique number is in Column A then

    Please Login or Register  to view this content.



    Thank you for your help. I did post an attempt with your initial suggestion (Post #12) but as I am still developing a basic understanding of VBA, I spend a lot of time not getting very far. I don't post all these attempts as I was didn't want to fill the board up with code that might divert from my original issue.

    I will have a go with the above.

  22. #22
    Registered User
    Join Date
    01-15-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    17

    [Solved] Self deleting cell formulas

    Quote Originally Posted by royUK View Post
    I suggested how to do this, gave you some leads not completed code as you asked. You haven't even used the suggestions.

    You have a userform that adds data to the sheet so you don't need sheet formulas for this.

    If the unique number is in Column A then

    Please Login or Register  to view this content.


    Brilliant! It works. I've incorperated this suggestion into what I had and ended up with the below:

    Please Login or Register  to view this content.

    Sorry it took so long for all this to click in my head. I realise how simple the solution is now - particularly as I essentially had all the more complicated code I needed already. Thank you for your patience and for everybody's help. Much appreciated - I have learnt a lot in this short time!
    Last edited by SJJ; 02-13-2012 at 09:45 AM. Reason: Tidying up working solution so that it works for others.

  23. #23
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Self deleting cell formulas

    Glad we got there in the end.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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