Results 1 to 4 of 4

Thread: [RESOLVED] Easy question...probably has to do with arrays

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    67

    Resolved [RESOLVED] Easy question...probably has to do with arrays

    Hey guys, I always code this the hard way...want to do it the easy way this time so I was hoping someone could let me in on the secret. Without knowing the right verbiage its always hard to find what I need doing a search.

    I've got a spreadsheet in excel, and one of the columns has branch codes in it. For every row that has a branch code of AI, FI, FJ, etc etc (18 total branch codes) I need to delete it. The way I know how to do that is like this:

    Code:
    ROW = 2
    WHILE NOT .CELLS(ROW,2).VALUE = ""
         BR_CODE = .CELLS(ROW,2).VALUE
           IF BR_CODE = "AI" THEN
              .ROWS(ROW).DELETE
           END IF
           IF BR_CODE = "FI" THEN
              .ROWS(ROW).DELETE
           END IF
           IF BR_CODE = "FJ" THEN
              .ROWS(ROW).DELETE
              ROW = ROW - 1
           END IF
    'ETC. FOR ALL 18 BRANCHES.
    ROW = ROW + 1
    WEND
    I normally just do it that way, because its quicker to copy & paste than it is to find the right answer. But I don't want my code to look sloppy anymore, so whats the right way? People talk about arrays all the time...I've never used one. Can someone show me an example as it would apply to this scenario?

    *EDIT* that code wouldn't actually work quite right because it would skip a row after each one it deletes...but you get the idea of what I'm trying to do...
    Last edited by Bill-E-BoB; Mar 28th, 2007 at 07:30 PM.

  2. #2
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Easy question...probably has to do with arrays

    Either of these should work.
    vb Code:
    1. Dim BR_CODE As String
    2. Dim Row As Integer
    3.  
    4. Row = 2
    5. BR_CODE = Cells(Row, 2).Value
    6. Do Until BR_CODE = ""
    7.   Select Case BR_CODE
    8.   Case "AI", "FI", "FJ"
    9.     Rows(Row).Delete
    10.   Case Else
    11.     Row = Row + 1
    12.   End Select
    13.   BR_CODE = Cells(Row, 2).Value
    14. Loop
    vb Code:
    1. Dim BR_CODE As String
    2. Dim Row As Integer
    3. Dim ALL_CODES As String
    4.  
    5. ALL_CODES = "AI,FI,FJ"
    6. Row = 2
    7. BR_CODE = Cells(Row, 2).Value
    8. Do Until BR_CODE = ""
    9.   If InStr(1, ALL_CODES, BR_CODE) > 0 Then
    10.     Rows(Row).Delete
    11.   Else
    12.     Row = Row + 1
    13.   End If
    14.   BR_CODE = Cells(Row, 2).Value
    15. Loop

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    67

    Re: Easy question...probably has to do with arrays

    Awsome, thanks! Just to make sure I'm understanding what goes on correctly (so that hopefully someday I can start answering questions on here instead of always just asking them...) tell me if I've got this down.

    In your first example, the Case Code on line 8 basically says "if its blah, or blah, or blah, or blah, then do whatever's on line 9 (for this example, delete the row). Then, the Case Else code on line 10 just says "otherwise, don't touch line 9 & do what I tell you on row 11 (in this case move to the next row of the spreadsheet).

    In your 2nd example, ALL_CODES is just a plain ol' ordinary string like I'm used to. No weird variant or array or anything that I should know about by now but don't. Its just a single string that contains all the branch codes I need to eliminate. Then BR_CODE is a normal string too. Then lines 9-13 are a good ol' fashioned if statement (again like what I'm used to) but line 9 has the piece thats new to me. InStr(1, ALL_CODES, BR_CODE) I'm guessing that counts how many occurences of the BR_CODE string exist within the ALL_CODES string and returns it as an integer...then if that integer is greater than 0 it deletes the row & if its not then it leaves that row and checks the next one.

    Am I understanding both of those correctly? I feel like if I really understand the "why" instead of just the "how" I'll find myself becoming a much better programmer. Sorry for all the dumb questions in the meantime.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Easy question...probably has to do with arrays

    Very close.. the only part you got wrong was InStr - it returns the position of one string (BR_CODE) within another (ALL_CODES), which will be 0 if it is not found. For more info, click on InStr in your code window, and press F1.


    Don't worry about asking "dumb" questions.. it is much better to learn how things work, rather than simply copy & paste, as it means you will be able to solve similar issues by yourself later. It makes us feel better about helping too, as you are actually learning, rather than getting us to do your work.

    To paraphrase what I've seen in a couple of members signatures: "It is better to appear dumb for a moment, than to remain dumb forever".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width