Help with VBA to return values to given cells

Gregm66

Board Regular
Joined
Jan 23, 2016
Messages
170
Hi everyone,

I have a worksheet called "Fees Paid" that holds members names and other values, In cells "A through to J"

Then i use another worksheet called "MembersReport", on the worksheet i use Vlookup formulas to return matching values from "Fees Paid" worksheet to certain cells in the "MembersReport" worksheet.

Cell "B9" on the "MembersReport worksheet has a in cell dropdown list of members names, this is what triggers the vlookup to return matching values, the the following cells in the "MembersReport" worksheet.

Cell "B11" gets its value from Column "E" on the "Fees Paid" worksheet
Cell "B13" from column "A" on the "Fees Paid" Worksheet
Cell "F9" from column "B" "Fees Paid" worksheet
Cell "F11" from column "F" fees Paid worksheet
Cell "F13" from Cell 'K2" on the Fees Paid worksheet, this one works on a date value. That being the date value that appears in Cell "K2"...

What i would like to do is to use VBA to acheive this instead on using formulas in each cell.

Thanks in advance for any help.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this

Change "C:C" for the column with the names.
Put the code in sheet events.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Target.Address(0, 0) = "B9" Then
    Dim sh2 As Worksheet, f As Range
    Set sh2 = Sheets("Fees Paid")
    Set f = sh2.Range("[COLOR=#ff0000]C:C[/COLOR]").Find(Range("B9"), , xlValues, xlWhole)
    If Not f Is Nothing Then
      Range("B11") = sh2.Range("E" & f.Row)
      Range("B13") = sh2.Range("A" & f.Row)
      Range("F9") = sh2.Range("B" & f.Row)
      Range("F11") = sh2.Range("F" & f.Row)
      Range("F13") = sh2.Range("K2")
    Else
      MsgBox "name does not exist"
    End If
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0
Thankyou for your reply DanteAmor,

The code you supplied works perfectly.

Thankyou once again for your help, greatly appreciated..
 
Upvote 0
Try this

Change "C:C" for the column with the names.
Put the code in sheet events.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Target.Address(0, 0) = "B9" Then
    Dim sh2 As Worksheet, f As Range
    Set sh2 = Sheets("Fees Paid")
    Set f = sh2.Range("[COLOR=#ff0000]C:C[/COLOR]").Find(Range("B9"), , xlValues, xlWhole)
    If Not f Is Nothing Then
      Range("B11") = sh2.Range("E" & f.Row)
      Range("B13") = sh2.Range("A" & f.Row)
      Range("F9") = sh2.Range("B" & f.Row)
      Range("F11") = sh2.Range("F" & f.Row)
      Range("F13") = sh2.Range("K2")
    Else
      MsgBox "name does not exist"
    End If
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.

Not to hijack this thread, but what would go into the section of code where you state "Change "C:C" for the column with the names"

Could you also explain what these two lines of code mean:
Code:
If Target.Count > 1 Then Exit Sub
If Target.Address(0, 0) = "B9" Then

Also, say A2 on down is what triggers matching values for B2,C2,D2 and the number of rows varies. Would the above code work if I tweaked it a little? Thanks for any info, I basically would like to try to add this onto some code I already have.
 
Upvote 0
I put my comments

Not to hijack this thread,
Do not worry

but what would go into the section of code where you state "Change "C:C" for the column with the names"
I do not understand what you mean.


Could you also explain what these two lines of code mean:
Code:
If Target.Count > 1 Then Exit Sub
[/COLOR][COLOR=#ff0000]If you modify more than one cell then it exits the program.[/COLOR][COLOR=#333333]

If Target.Address(0, 0) = "B9" Then
If the modified cell is cell B9 then the instructions are executed.

Also, say A2 on down is what triggers matching values for B2,C2,D2 and the number of rows varies. Would the above code work if I tweaked it a little? Thanks for any info, I basically would like to try to add this onto some code I already have.

I could gladly modify the code for your needs. But I am not sure what you require.


I recommend you create a new thread where you explain in detail how your data is, if you have more than one sheet, what data you have on each sheet, what data you have in each column. What data do you want to capture, in which cell and what do you expect?
 
Upvote 0

Forum statistics

Threads
1,224,749
Messages
6,180,725
Members
452,995
Latest member
isldboy

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