Newbie question!

V

Vestergaard

Hi people at Excel forum

I have just begun using VBA for Excel, so I am a newbie :) I hop
somebody can help me with the following problem:

Each of the cells from A1 until A10 contains different kind of text.
Cell B1 contains an integer between 1 and 10.
In cell B2 I want the same text as in the cell, which is pointed out b
the integer value in cell B1. For example: If B1 contains the intege
value 5, then B2 should automatically obtain the text from cell A5.

What VBA code is appropriate for cell B2?

Best regards,

Eri
 
G

Guest

This best way IMHO is to use a worksheet function instead of VBA. Place the
following in cell B2:

=INDIRECT("A" & B1)

If you want to use VBA then I would suggest pasting this to the worksheet's
code module:

Private Sub Worksheet_Change(ByVal Target As Range)
Range("B2").Value = Range("A" & Range("B1").Value)
End Sub

Note that worksheet functions execute faster than VBA and so IMHO should be
used preferentially. There are of course plenty of situations where this is
not feasible.

Regards,
Greg
 
G

Guest

If you want to dabble...

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address = "$B$1" Then
If IsNumeric(Target.Value) Then
Range("B2").Value = Cells(Target.Value, 1)
Range("B2").Font.Color = RGB(0, 0, 0)
Range("B2").Font.Bold = False
Else
Range("B2").Value = "Error"
Range("B2").Font.Color = RGB(255, 0, 0)
Range("B2").Font.Bold = True
End If
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top