Locating the last cell thats in a coulmn then entering a value based on the cell above

JohnnyBQue

New Member
Joined
Apr 25, 2016
Messages
8
I have a database with a "worksheet" (sheet5) named "Route Sheet"
In order to retrieve data from (sheet5) I have a column that acts like a "Primary Key" in access called "ID"
The ID column contain numbers in symmetrical order (0001, 0002, 0003, etc) for every row that contains data
I use the ID in a "form" that generates a "Route Sheet document" for material process based on the ID entered and corresponding data in the row

Now...I also use the "form" to enter information in the control fields then with a command button I then store the entered data to the "worksheet" (sheet5) named "Route Sheet" in the next empty row that's available.

in-lies the issue...when using the form to enter information i have no idea what the next available ID number is (so this control field is left blank) so when the data is stored in (sheet5) in the next empty row... the cell in the column named "ID" that's adjacent to the row of data just stored is now BLANK

upon the "store command" I need the cell in the column named "ID" that adjacent to the row of data just stored to have a new ID number based on incrementing the cell that is above it.

Below is the code I use to store the data and bring up the data in the "form" when a known ID is entered.


//
Private Sub cmdSend_Click()
'Dim the variables
Dim cNum As Integer
Dim X As Integer
Dim nextrow As Range

'change the number for the number of controls/text boxes on the userform

cNum = 17
Set nextrow = Sheet3.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
For X = 1 To cNum
nextrow = Me.Controls("Reg" & X).Value
Set nextrow = nextrow.Offset(0, 1)
Next
MsgBox "The data has been sent"
'Clear the controls boxes
cNum = 17
For X = 1 To cNum
Me.Controls("Reg" & X).Value = ""
Set nextrow = nextrow.Offset(0, 1)
Next

End Sub

Private Sub Reg1_AfterUpdate()
'Check to see if value exists
If WorksheetFunction.CountIf(Sheet3.Range("C:C"), Me.Reg1.Value) = 0 Then
MsgBox "This ID does not exist or has not been used"
Me.Reg1.Value = ""
Exit Sub
End If
'Lookup values based on first ID entered and place row/cell values into control boxes
With Me
.Reg2 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 2, 0)
.Reg3 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 3, 0)
.Reg4 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 4, 0)
.Reg5 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 5, 0)
.Reg6 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 6, 0)
.Reg7 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 7, 0)
.Reg8 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 8, 0)
.Reg9 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 9, 0)
.Reg10 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 10, 0)
.Reg11 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 11, 0)
.Reg12 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 12, 0)
.Reg13 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 13, 0)
.Reg14 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 14, 0)
.Reg15 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 15, 0)
.Reg16 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 16, 0)
.Reg17 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet3.Range("Lookup"), 17, 0)

End With
Sheet5.SReg2 = Reg2.Text
Sheet5.SReg3 = Reg3.Text
Sheet5.SReg4 = Reg4.Text
Sheet5.SReg5 = Reg5.Text
Sheet5.SReg6 = Reg6.Text
Sheet5.SReg7 = Reg7.Text
Sheet5.SReg8 = Reg8.Text
Sheet5.SReg9 = Reg9.Text
Sheet5.SReg10 = Reg10.Text
Sheet5.SReg11 = Reg11.Text
Sheet5.SReg12 = Reg12.Text
Sheet5.SReg13 = Reg13.Text
Sheet5.SReg14 = Reg14.Text
Sheet5.SReg15 = Reg15.Text
Sheet5.SReg16 = Reg16.Text
Sheet5.SReg17 = Reg17.Text
Sheet5.TSReg4 = Reg4.Text
Sheet5.TSReg12 = Reg12.Text

End Sub
//
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I made a form i used for Inventory counting at a job i had, and i had a section that showed the "Last Entry". Not the prettiest way to do it, but you could make a text box, lock it, color it the same background as your form and have it display that last "ID" used. So when creating ID "0009" the text box would read "0008". You could take that a step further and turn the "Last ID text box to "visible = False", then in the "New" ID text box enter a code like
Code:
txtNewID.value = txtLastID.value +1.

I'm very amateur but i think it would work.

Code:
Private Sub UserForm_Initialize()
 Dim ws As Worksheet
    Dim rFound As Range
Dim i As Long
     
        
              
Set ws = ThisWorkbook.Sheets("INVENTORY") ' change sheet name accordingly.
 
With ws
    i = .Rows.Count
    lstdt = .Range("b" & i).End(xlUp).Value
    frmINV.txtTYPE.Value = lstdt
      
    i = .Rows.Count
    lstdt = .Range("a" & i).End(xlUp).Value
    frmINV.txtLOC.Value = lstdt
    
    i = .Rows.Count
    lstdt = .Range("C" & i).End(xlUp).Value
    frmINV.txtLAST.Value = lstdt
    
    i = .Rows.Count
    lstdt = .Range("d" & i).End(xlUp).Value
    frmINV.txtLAST_QTY.Value = lstdt
    
   i = .Rows.Count
    lstdt = .Range("e" & i).End(xlUp).Value
    frmINV.txtLast_Descr.Value = lstdt
    
   
     
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top