I have created a VBA form that has Asset, User and two buttons for Check in and Check out. I have these set up to create a table when entries are made and you hit one of the two buttons using the following VBA code (unique code for each button to changed the row 3 value).
Private Sub chkout_Click()
Dim emptyRow As Long
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 1).Value = user.Value
Cells(emptyRow, 2).Value = asset.Value
Cells(emptyRow, 3).Value = "Checked out"
Cells(emptyRow, 4).Value = Now()
End Sub
What i want though is to have the code first look to see if the asset.value exists already in the table and replace certain fields with the new inputed ones but if not then to add new rows just as the code would. I have tried using a few different codes found online with no sucesss such as the following:
Private Sub chkin_Click()
Dim tbl As ListObject
Dim newrow As ListRow
Dim Fnd As Range
Set tbl = Sheets("Sheet1").ListObjects("Table1")
With Sheets("Sheet1")
Set Fnd = tbl.ListColumns(1).DataBodyRange.Find(.Range("asset.Value").Value)
If Fnd Is Nothing Then
Set newrow = tbl.ListRows.Add
With newrow
.Range(1) = Worksheets("Sheet1").Range(asset.Value)
.Range(2) = Worksheets("Sheet1").Range(user.Value)
.Range(3) = Worksheets("Sheet1").Range("Checked In")
.Range(4) = Worksheets("Sheet1").Range(Now())
End With
Else
Fnd.Offset(, 1) = user.Value
Fnd.Offset(, 2).Value = "Checked in"
Fnd.Offset(, 3).Value = Now()
End If
End With
End Sub
Any help would be appreciated
Private Sub chkout_Click()
Dim emptyRow As Long
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 1).Value = user.Value
Cells(emptyRow, 2).Value = asset.Value
Cells(emptyRow, 3).Value = "Checked out"
Cells(emptyRow, 4).Value = Now()
End Sub
What i want though is to have the code first look to see if the asset.value exists already in the table and replace certain fields with the new inputed ones but if not then to add new rows just as the code would. I have tried using a few different codes found online with no sucesss such as the following:
Private Sub chkin_Click()
Dim tbl As ListObject
Dim newrow As ListRow
Dim Fnd As Range
Set tbl = Sheets("Sheet1").ListObjects("Table1")
With Sheets("Sheet1")
Set Fnd = tbl.ListColumns(1).DataBodyRange.Find(.Range("asset.Value").Value)
If Fnd Is Nothing Then
Set newrow = tbl.ListRows.Add
With newrow
.Range(1) = Worksheets("Sheet1").Range(asset.Value)
.Range(2) = Worksheets("Sheet1").Range(user.Value)
.Range(3) = Worksheets("Sheet1").Range("Checked In")
.Range(4) = Worksheets("Sheet1").Range(Now())
End With
Else
Fnd.Offset(, 1) = user.Value
Fnd.Offset(, 2).Value = "Checked in"
Fnd.Offset(, 3).Value = Now()
End If
End With
End Sub
Any help would be appreciated