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 08:33 AM. Reason: Issue Solved
Generate the ID within the VBA code
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
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?
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.
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
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.
In VBA, a quick copy/pastevalues example...
Range("B55") = Range("B55").Value
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
orsub snb() If Range("C55").Value <> "" Then Range("B55").Value = 1 + application.max(sheets("Master_Database").Range("B50:B1050")) end sub
sub snb() With Range("C55") If .Value <> "" Then .offset(,-1).Value = 1 + application.max(sheets("Master_Database").Range("B50:B1050")) end with end sub
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
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?![]()
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
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
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...)?
Set myRange = Worksheets("Master_Database").Range("B50:B1050") answer = Application.WorksheetFunction.Max(myRange) If Range("C60") <> "" Then Range("B60") = answer + 1 End If
Try this in the worksheet code module...
Private Sub Worksheet_Change(ByVal Target As Range) Dim rCell As Range On Error GoTo errorhandler For Each rCell In Intersect(Columns(2), UsedRange).SpecialCells(xlCellTypeBlanks) rCell = WorksheetFunction.Max(Columns(2)) + 1 Next rCell errorhandler: End Sub
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:
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".Private Sub Submit_Click() If MsgBox("Send to Database?", vbYesNo + vbQuestion) = vbNo Then Exit Sub Else Sheets("Print").Range("C7") = Answer1.Text Sheets("Print").Range("C8") = Answer2.Text Sheets("Print").Range("C9") = Answer3.Text End If Dim iRow As Long Dim ws As Worksheet Set ws = Worksheets("Master_Database") iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1 ws.Cells(iRow, 3).Value = Me.Answer1.Value ws.Cells(iRow, 4).Value = Me.Answer2.Value ws.Cells(iRow, 5).Value = Me.Answer3.Value Me.Answer1.Value = "" Me.Answer2.Value = "" Me.Answer3.Value = "" Me.Answer1.SetFocus End Sub
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!
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks