What I am doing Wrong In This Code?

  • Thread starter Thread starter Sixthsense
  • Start date Start date
S

Sixthsense

Sub IfMacro()
Set Data = Range("a1:A11")
Set Output = Range("B1:B11")
If Data.Value = "A" Then
Output = 1
ElseIf Data.Value = "B" Then
Output = 2
ElseIf Data.Value = "C" Then
Output = 3
End If
End Sub
 
Sixthsense said:
Sub IfMacro()
Set Data = Range("a1:A11")
Set Output = Range("B1:B11")
If Data.Value = "A" Then
Output = 1
ElseIf Data.Value = "B" Then
Output = 2
ElseIf Data.Value = "C" Then
Output = 3
End If
End Sub


you are trying to use single cell operations on multi-cell ranges.
 
hi,

Sub IfMacro()
Set Data = Range("a1:A11")
Set Output = Range("B1:B11")
For i = 1 To Data.Count
If Data(i).Value = "A" Then
Output(i) = 1
ElseIf Data(i).Value = "B" Then
Output(i) = 2
ElseIf Data(i).Value = "C" Then
Output(i) = 3
End If
Next
End Sub

or

Sub Macro2()
Set Data = Range("a1:A11")
Set Output = Range("B1:B11")
For i = 1 To Data.Count
Select Case Data(i).Value
Case "A": Output(i) = 1
Case "B": Output(i) = 2
Case "C": Output(i) = 3
End Select
Next
End Sub
 
Hi,

Thank you so much It's worked like a charm. I am a newbie to excel
VBA and now I am started to learn it with the help of experts like
you!!!
 
Back
Top