vba help - select case from range

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

Below code helps for deleting of rows. need help in making select case dynamic.

Case "Dhoni", "Sachin", "Sehwag" '<- Keep these , this is hard coded how to pass these criteria from range /or variable here.
like Case arr_player ' not working getting type mismatch.[/CODE]

arr_player = WorksheetFunction.Transpose(Range("D2:d4").Value)

VBA Code:
Sub Del_Except()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long

Dim arr_player As Variant
arr_player = WorksheetFunction.Transpose(Range("D2:d4").Value)

  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    Select Case (a(i, 1))
      Case "Dhoni", "Sachin", "Sehwag"   '<- Keep these  , this is hard coded, how to pass variable here
      Case arr_player  ' not working getting type mismatch.

Case Else '<- Delete everything else
b(i, 1) = 1
k = k + 1
End Select
Next i
If k > 0 Then
Application.ScreenUpdating = False
With Range("A2").Resize(UBound(a), nc)
.Columns(nc).Value = b
.Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
.Resize(k).EntireRow.Delete
End With
Application.ScreenUpdating = True
End If
End Sub[/CODE]


Table with Criteria list.

Book1
ABCD
1NameCriteria
2SehwagSachin
3SehwagDhoni
4KohliSehwag
5sachin
6sachin
7Gayle
8Dhoni
9Gayle
10Sehwag
11Gayle
12Dhoni
13Sehwag
14Ponting
15Dhoni
16Ponting
17sachin
18Sehwag
19Dhoni
20Kohli
21Kohli
22sachin
23Sehwag
24sachin
25Sehwag
26Kohli
27Kohli
28Sehwag
29Sehwag
30Kohli
31Dhoni
Sheet1


Thanks
mg
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Don't use Select Case.

Read the values to keep/delete into an array and then use Application.Match to check the values from the sheet against the values in the array and take the appropriate acion.
 
Upvote 0

Forum statistics

Threads
1,215,048
Messages
6,122,862
Members
449,097
Latest member
dbomb1414

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