VBA macro: insert string value in the same row (number) after finding column header

TommasoP11

New Member
Joined
Jul 24, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi I was wondering if someone can help me with a macro. I am creating a schedule in excel and I need to insert a specific string value (timing) to a column in the same sheet that match a specific person that comes from a drop down list. Basically I would like to create a system like this with a macro:
ABCDEFGHI
1​
Date8.00-17.009.00-13.0014.00-17.00SpidermanHulkBatmanRobin
2​
01/08/2021​
SpidermanHulk8.00-17.009.00-13.00
3​
02/08/2021​
HulkRobin8.00-17.0014.00-17.00
4​
03/08/2021​
Batman8.00-17.00
5​
04/08/2021​
RobinSpiderman14.00-17.008.00-17.00

I would like to assign to every member his time in the Range F:I by inserting their name in range A:D (source range). The two ranges are in the same row. Thanks a lot for your help very appreciated. If not clear please feel free to ask me.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Upvote 0
@TommasoP11 Try this.

MRXLMAY21.xlsm
ABCDEFGHI
1Date8.00-17.0009.00-13.0014.00-17.00SpidermanHulkBatmanRobin
201/08/2021SpidermanHulk8.00-17.0009.00-13.00  
302/08/2021HulkRobin 8.00-17.00 14.00-17.00
403/08/2021Batman  8.00-17.00 
504/08/2021RobinSpiderman14.00-17.00  8.00-17.00
CASH SHEET
Cell Formulas
RangeFormula
F2:I5F2=IFERROR(INDEX($B$1:$D$1,1,MATCH(F$1,$B2:$D2,0)),"")


Hope that helps.

EDIT: Sorry, just realised you were asking for a macro. Is a macro still required?
Thanks a lot for your promt reply, really appreciated. If it possible to have in macro that would be great, if not don't worry. Many thanks
 
Upvote 0
Maybe this, pasted into the code pane for that workshhet.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub
If Not Target.Column > 1 And Target.Column < 5 Then Exit Sub
If Not (Target.row > 1 And Target.row < Range("A" & Rows.Count).End(xlUp).row + 1) Then Exit Sub

Application.EnableEvents = False
Range("F" & Target.row & ":I" & Target.row).FormulaR1C1 = "=IFERROR(INDEX(R1C2:R1C4,1,MATCH(R1C,RC2:RC4,0)),"""")"
Range("F" & Target.row & ":I" & Target.row).Value = Range("F" & Target.row & ":I" & Target.row).Value
Application.EnableEvents = True

End Sub
 
Upvote 0
Solution
Maybe this, pasted into the code pane for that workshhet.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub
If Not Target.Column > 1 And Target.Column < 5 Then Exit Sub
If Not (Target.row > 1 And Target.row < Range("A" & Rows.Count).End(xlUp).row + 1) Then Exit Sub

Application.EnableEvents = False
Range("F" & Target.row & ":I" & Target.row).FormulaR1C1 = "=IFERROR(INDEX(R1C2:R1C4,1,MATCH(R1C,RC2:RC4,0)),"""")"
Range("F" & Target.row & ":I" & Target.row).Value = Range("F" & Target.row & ":I" & Target.row).Value
Application.EnableEvents = True

End Sub
Thanks a lot, it work smashing! thanks for your time really appreciated and apologies if my request was not so clear. Have a good evening.
 
Upvote 0
Hi, it's me again. Please I would like to ask you another advice. If I want to start this code from Row n.2 what settings do I need to change in your code? Sorry for the silly question.
 
Upvote 0
Hi, it's me again. Please I would like to ask you another advice. If I want to start this code from Row n.2 what settings do I need to change in your code? Sorry for the silly question.
Hi, If you are still inputting in columns B,C,D but want output to N,O,P,Q rather than F,G,H,I then try

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub
If Not Target.Column > 1 And Target.Column < 5 Then Exit Sub
If Not (Target.row > 1 And Target.row < Range("A" & Rows.Count).End(xlUp).row + 1) Then Exit Sub

Application.EnableEvents = False
Range("N" & Target.row & ":Q" & Target.row).FormulaR1C1 = "=IFERROR(INDEX(R1C2:R1C4,1,MATCH(R1C,RC2:RC4,0)),"""")"
Range("N" & Target.row & ":Q" & Target.row).Value = Range("N" & Target.row & ":Q" & Target.row).Value
Application.EnableEvents = True

End Sub
 
Upvote 0
VBA Code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub
If Not Target.Column > 1 And Target.Column < 5 Then Exit Sub
If Not (Target.row > 1 And Target.row < Range("A" & Rows.Count).End(xlUp).row + 1) Then Exit Sub

Application.EnableEvents = False
Range("N" & Target.row & ":Q" & Target.row).FormulaR1C1 = "=IFERROR(INDEX(R1C2:R1C4,1,MATCH(R1C,RC2:RC4,0)),"""")"
Range("N" & Target.row & ":Q" & Target.row).Value = Range("N" & Target.row & ":Q" & Target.row).Value
Application.EnableEvents = True

End Sub
Thank you so much for your reply Snakehips. I would like to put either the input or the output in Row. n. 2. Basically having all my headers in Row. n. 2 rather than Row. 1. Thanks a lot for your assistance really much appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,704
Members
452,938
Latest member
babeneker

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