Fill Adjacent Columns based on cell value

AriffChowdhury

New Member
Joined
Jul 28, 2013
Messages
10
Hello,
I have string in column A.
I input some numerical values (null -15) in column B .
I want vba / function to look for the value entered in B and based on this value fill the adjcaent columns with string in column A by adding "_C" & "number" with the string in column A

For example;
I have string in cell A1 "volume1".
I input value 3 into cell B1.
I want concatenate the string of column A with "_C" & "number", and fill the adjcent no. of column based on the value.
I want "volume1_C1" in column C1, "volume1_C2" in D1 and "volume1_C3 in E1

ABCDEFGH
HeadersValuesHeader1 Header2 Header3 Header4 Header5 Header6
Volume13Volume1_C1Volume1_C2Volume1_C3
Volume2
Volume35Volume3_C1Volume3_C2Volume3_C3Volume3_C4Volume3_C5
Volume4
Volume5
Volume61Volume6_C1
Volume7

<colgroup><col style="text-align: center;"><col style="text-align: center;"><col span="5" style="text-align: center;"><col style="text-align: center;"></colgroup><tbody>
</tbody>




Does this make sense ? and is this can be achieved through vba/excel function ? i would prefer vba..

Thank you!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You function worked perfectly.Thank you Sir!
But this is it what I am afraid of with the excel function actually this section is to be copied in another sheet when i ran macro to copy, it copied the formula as well (and due to reference of cell formula results an #REF! error).
 
Upvote 0
Since you said you would prefer a VBA solution (not sure why as, personally, I would use Andrew's formula solution), so here is event code that will do what you asked...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  With Target
    If .Count > 1 Then Exit Sub
    If .Column = 2 Then
      If Not .Value Like "*[!0-9]*" Then
        Range(.Offset(, 1), Cells(.Row, Columns.Count)).Clear
        .Offset(, 1).Resize(, .Value) = Evaluate("A" & .Row & "&""_C""&COLUMN(1:" & .Value & ")")
      Else
        MsgBox "Only whole number entries are allowed!"
        .Select
      End If
    End If
  End With
End Sub

HOW TO INSTALL Event Code
------------------------------------
If you are new to event code procedures, they are easy to install. To install it, right-click the name tab at the bottom of the worksheet that is to have the functionality to be provided by the event code and select "View Code" from the popup menu that appears. This will open up the code window for that worksheet. Copy/Paste the event code into that code window. That's it... the code will now operate automatically when its particular event procedure is raised by an action you take on the worksheet itself.
 
Upvote 0
At the very first i would like to thank you to both of you Sir!...Not only i got the vba code but also got the excel function! by the dint of your supports!

And Sir Rick....Yes its worked as it is, as you described, but the area is filled with background color "pink".
When i enter any value it creates the headers with number but removed all my formatting. i wanted to paste the formatting as well into my other worksheet.


Thanks again for your cooperation!
 
Upvote 0
And Sir Rick....Yes its worked as it is, as you described, but the area is filled with background color "pink".
When i enter any value it creates the headers with number but removed all my formatting. i wanted to paste the formatting as well into my other worksheet.
Sorry, I used Clear instead of ClearContents. Try this code instead...
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
  With Target
    If .Count > 1 Then Exit Sub
    If .Column = 2 Then
      If Not .Value Like "*[!0-9]*" Then
        Range(.Offset(, 1), Cells(.Row, Columns.Count)).ClearContents
        .Offset(, 1).Resize(, .Value) = Evaluate("A" & .Row & "&""_C""&COLUMN(1:" & .Value & ")")
      Else
        MsgBox "Only whole number entries are allowed!"
        .Select
      End If
    End If
  End With
End Sub
 
Upvote 0
And when I delete any value in column B it gives me "Run Time 1004" error.
Sorry, I did not test for that when developing the code. Try this modified version of my code instead...
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
  With Target
    If .Count > 1 Then Exit Sub
    If .Column = 2 Then
      If Not .Value Like "*[!0-9]*" Then
        Range(.Offset(, 1), Cells(.Row, Columns.Count)).ClearContents
        If Len(.Value) Then .Offset(, 1).Resize(, .Value) = Evaluate("A" & .Row & "&""_C""&COLUMN(1:" & .Value & ")")
      Else
        MsgBox "Only whole number entries are allowed!"
        .Select
      End If
    End If
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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