-
1 Attachment(s)
VBA - Select Case Error
HTML Code:
Hi Team
I wanted to pass dynamically array values into select Case.Instead of 'Case "Dhoni", "Sachin" , Case ar
Getting error at :=> Case arBelow are my attempted Code. Thanks in advance
Option ExplicitSub test() Dim sh1 As Worksheet Dim sh2 As Worksheet Set sh1 = ThisWorkbook.Worksheets("Sheet1") Set sh2 = ThisWorkbook.Worksheets("Mapping") Dim lr As Long lr = sh1.Cells(sh1.Rows.Count, "A").End(xlUp).Row Dim ar As Variant ar = Application.WorksheetFunction.Transpose(sh2.Range("a2:a3")) Dim i As Long For i = 2 To lr Select Case sh1.Cells(i, 1).Value 'Case "Dhoni", "Sachin" Case ar 'Getting error here want to make this line Dynamic sh1.Cells(i, 1).Font.Bold = True End Select Next i
End Sub
Regards,
mg
-
HTML Code:
Hi Team
I wanted to pass dynamically array values into select Case.Instead of 'Case "Dhoni", "Sachin" , Case ar
Getting error at :=> Case arBelow are my attempted Code.
Option ExplicitSub test() Dim sh1 As Worksheet Dim sh2 As Worksheet Set sh1 = ThisWorkbook.Worksheets("Sheet1") Set sh2 = ThisWorkbook.Worksheets("Mapping") Dim lr As Long lr = sh1.Cells(sh1.Rows.Count, "A").End(xlUp).Row Dim ar As Variant ar = Application.WorksheetFunction.Transpose(sh2.Range("a2:a3")) Dim i As Long For i = 2 To lr Select Case sh1.Cells(i, 1).Value 'Case "Dhoni", "Sachin" Case ar sh1.Cells(i, 1).Font.Bold = True End Select Next i
End Sub
-
You cannot use Select case like that, try
Code:
For i = 2 To lr
If UBound(Filter(ar, sh1.Cells(i, 1).Value, True, vbTextCompare)) >= 0 Then
sh1.Cells(i, 1).Font.Bold = True
End If
Next i
-
Hi Fluff,
Thanks a lot , it worked perfectly,
Can you please explain this line UBound(Filter(ar, finding it hard to understand what it is does. Thanks once again.
If UBound(Filter(ar, sh1.Cells(i, 1).Value, True, vbTextCompare)) >= 0 Then
Regards,
Mallesh
-
You're welcome & thanks for the feedback.
For more info on the Filter function, have a look here https://www.excelfunctions.net/vba-filter-function.html
-
Hi Fluff.
Thanks once again for your help:thumb in this and also suggesting website.
Regards,
mg
-