VBA Scipt to Input Value Next To A Value In A Database

timpickup

New Member
Joined
May 14, 2015
Messages
14
Hi All,

I am trying to create a VBA code to do the following task:

1. Look up a value on active sheet.
2. With that value, look in coloum "B" of sheet "Database".
3. Change the Value of a Cell to the Time in the same row but a different column on that sheet.

I have this so far, but it goes to the other sheet and then just inputs the Time into the original sheet instead of the updating the cells in the Database.

Private Sub CommandButton2_Click()

ref_number = Range("B9")

Sheets("Database").Activate

Last = Cells(Rows.Count, "B").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "A").Value) = ref_number Then
Cells(i, "G") = Time
End If
Next i

End Sub

Can anyone offer any guidance regarding where I am going wrong? Would be much appreciated.

Thankyou
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Perhaps
Code:
Private Sub CommandButton2_Click()
Dim Res As Variant

    ref_number = Range("B9")
  
    With Sheets("Database")
        Res = Application.Match(ref_number, .Range("B:B"), 0)
        If Not IsError(Res) Then 
            Cells(Res, "G") = Time
        End If
    End With

End Sub
 
Upvote 0
Is it definitely column B on 'Database' you want to look in?

I ask because in the original post you said column B but in the code you were looping through column A.

Also, does the data type of ref_number match the data type of the values in column B (or is A.:eek:).
 
Upvote 0
Yeah it is B, I made an error in my original post. I set both data types as Number and this still has not worked
 
Upvote 0
Try this:
Code:
Sub New_One()
Dim ref_number As Integer
ref_number = Range("B9").Value
Sheets("Database").Activate
Last = Cells(Rows.Count, "A").End(xlUp).Row
 
    For i = 1 To Last
        If Cells(i, "A").Value = ref_number Then
            Cells(i, "G") = Time
        End If
    Next i

 End Sub

it's good to see your trying to learn Vb. You had several errors in your code.
 
Upvote 0
There's a very small typo - a missing . in front of Cells(Res, "G").

Here's the fixed code with a bit of tidying up as well.
Code:
Private Sub CommandButton2_Click()
Dim Res As Variant

    ref_number = Range("B9").Value
  
    With Sheets("Database")
        Res = Application.Match(ref_number, .Range("B:B"), 0)
        If Not IsError(Res) Then
            .Cells(Res, "G").Value = Time
        End If
    End With

End Sub
 
Upvote 0
Thank You so much Norie!

Works perfectly, would it be possible to have the same formula for a different button to delete the whole row if it is a match... Or will I have to post a new thread?
 
Upvote 0
This will delete the row if found.
Code:
Private Sub CommandButton3_Click()
Dim Res As Variant

    ref_number = Range("B9").Value
  
    With Sheets("Database")
        Res = Application.Match(ref_number, .Range("B:B"), 0)
        If Not IsError(Res) Then
            .Rows(Res).Delete
        End If
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,539
Members
449,169
Latest member
mm424

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