I hereby call upon the VBA Masters of the Universe and request a favor.

bradenkeith

New Member
Joined
May 4, 2014
Messages
32
I am in need of a simplified code that draws data to a series of cells depending on a particular cell's value. To keep things organized, I will illustrate what I need with the following example and figure out the rest on my own.

Imagine there is a line of infinite persons, each person is wearing a hat of a different color, and the purpose of this exercise is to associate the color of a persons hat with their respective place in line. It seems simple enough; however, I need all information consolidated visibly to two cells (A2:B2).

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Place in line[/TD]
[TD]Color of Hat[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]10[/TD]
[TD]Blue[/TD]
[/TR]
</tbody>[/TABLE]

A spinner will be used to change the value of cell A2 by increments of 1 (from 1 to infinity), and the code I am looking for must draw data to cell B2 upon activating said spinner. All data will be drawn from the following Table ("Table 1"):

[TABLE="width: 500"]
<tbody>[TR]
[TD]Table 1[/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Place in Line[/TD]
[TD]Color of Hat[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]1[/TD]
[TD]Blue[/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]2[/TD]
[TD]Green[/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]3[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD]4[/TD]
[TD]Brown[/TD]
[/TR]
[TR]
[TD]16[/TD]
[TD]5[/TD]
[TD]White[/TD]
[/TR]
[TR]
[TD]17[/TD]
[TD]6[/TD]
[TD]Brown[/TD]
[/TR]
[TR]
[TD]18[/TD]
[TD]7[/TD]
[TD]Brown[/TD]
[/TR]
[TR]
[TD]19[/TD]
[TD]8[/TD]
[TD]Blue[/TD]
[/TR]
[TR]
[TD]20[/TD]
[TD]9[/TD]
[TD]Green[/TD]
[/TR]
[TR]
[TD]21[/TD]
[TD]10[/TD]
[TD]Blue[/TD]
[/TR]
</tbody>[/TABLE]


The tricky part is that I also need to be able to change Cell B2 thereby updating Table 1 with new information. Can it be done?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you put =VLOOKUP(A2, A12:B21, 2, FALSE) in B2, it will return the color as you change the entry in A2
 
Upvote 0
The only problem is that I could not update cell B2 manually because it would then replace the formula. right?
 
Upvote 0
One way:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Oops
    Application.EnableEvents = False
    
    Select Case Target.Address(False, False)
        Case "A2"
            Range("B2").Formula = "=vlookup(A2, A12:B21, 2, false)"
        Case "B2"
            Range("B12:B21").Cells(Range("A2").Value).Value = Target.Value
    End Select

Oops:
    Application.EnableEvents = True
End Sub

It's missing error checking.
 
Upvote 0
Looking at your chart. If you manually put Blue in B2, would you want 1 or 10 to be the value returned?
 
Upvote 0
I would want it to update table 1. Because table 1 already has blue for position 10, no data would change.

If A2= 3, then B2 would equal "Orange." If I updated cell B2 to "Blue" at that point, I would like cell B14 to change to "Blue."
 
Upvote 0
If you have a named range Name: Table1 RefersTo: =Sheet1!$A$12:$B$21 this might work.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    With Target
        If .Cells.Count = 1 And .Address = "$B$2" Then
            Range("Table1").Cells(Range("A2").Value, 2) = .Value
        ElseIf Not Application.Intersect(.Cells, Range("A2")) Is Nothing Then
            Range("B2").FormulaR1C1 = "=VLOOKUP(RC[-1],Table1,2,False)"
        End If
    End With
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,416
Messages
6,178,509
Members
452,853
Latest member
philipnjk64

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