Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
use Select Case with 'external' list
I am trying to write a macro that will delete all of the rows in a
report that contain names that are not on a list. Part of the problem is that the macro can't be in the report and I want to store the list of acceptable names in the workbook that contains the macro so they can be easily updated. I have been trying to use Select Case but I can't figure out how to access the list. I have tried using a Range To Range but that just selected all the names from Andy to Walter. I was thinking about using an array but I couldn't get my mind wrapped around that. The report workbook and the sheet the report is on could be named anything so I'm going to have to rely on giving that sheet the focus. The column with the tech's names could be anywhere but I have coded for that thanks to snippets found in this group. The number of techs could change and the length of the report itself will change every time it is generated. Again, coded for thanks to the group. Here is the code as it stands. I don't have anything in the Case at the moment because that's where my mental block is: The list on Sheet1, starting at A1, would be something like; Tech Names (Header) Andy Fred Penny Tina Walter (A row with any other name should get deleted.) Option Explicit Sub Delete_other_names() Dim LastRow As Long, r As Long, c1 As Long, LastName As Long Dim Rng As Range, Tx As String, Ts As String Dim header As String, n As Long Dim src As Worksheet Set src = ThisWorkbook.Worksheets(1) 'MyTechs.xls/Sheet1 ' Find last name on Tech list. LastName = src.Cells(Rows.Count, "A").End(xlUp).Row ' Find last row on the actual report. LastRow = Cells(Rows.Count, "A").End(xlUp).Row ' Find which column of the report has the Tech's name. header = "Assigned To" Set Rng = ActiveSheet.Range("1:1").Find(What:=header, _ After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlWhole, _ SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) If Not Rng Is Nothing Then c1 = Rng.Column 'Loop through entire report to find your techs. For r = LastRow To 2 Step -1 'starts at the bottom to avoid skipping lines. Tx = Cells(r, c1).Value 'match this string. Select Case Tx Case "Help me, please" Cells(r, 1).Interior.ColorIndex = 0 'essentially, do nothing. Case Else ActiveSheet.Rows(r).EntireRow.Delete End Select Next r End Sub The other thought I have is that I am doing the checking and deleting backwards, meaning if it's on the list do nothing otherwise delete the row. Let me know what a more logical way to do that is. Thanks in advance. Bill |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Case without Select Case error problem | Excel Discussion (Misc queries) | |||
import external data sql case statement | Excel Discussion (Misc queries) | |||
End Select without Select Case, Block If without End If errors | Excel Programming | |||
Pivot Tables, can I use an external list to select data items? | Excel Worksheet Functions |