+ Reply to Thread
Results 1 to 8 of 8

Coding Help

  1. #1
    Registered User
    Join Date
    08-22-2017
    Location
    US
    MS-Off Ver
    2013
    Posts
    4

    Coding Help

    Hello, I am trying to write some code that will look at a column of numbers, and delete the rows that contain a number that does match any of the numbers in a specific array.

    For example, say I define an array as [1,2,3,4] and my column of numbers in excel as [1,2,3,4,5,6,7,8,9]. The code will delete the rows that do not match any of the numbers in the array, in this example, rows 5, 6, 7, 8, and 9 will be deleted. Here's what I have -

    Private Sub WriteToACell()

    Set WS = ActiveSheet

    Dim SomeArray(3) As Integer

    SomeArray(0) = 1
    SomeArray(1) = 2
    SomeArray(2) = 3
    SomeArray(3) = 4


    For i = 1 To 10 Step 1
    For j = 0 To 3 Step 1
    If WS.Cells(i, 1).Value = SomeArray(j) Then [ Next i ]
    ElseIf WS.Cells(i, 1).Value <> SomeArray(j) Then WS.EntireRow.Delete
    Next j
    Next i

    End Sub


    I'm am trying to compare each cell to the array by cycling through the array, but it doesn't seem to be working. Any help is appreciated!

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Coding Help

    Hi steelmaster,

    There is a trick to delete rows using the For Next loop.

    You need to start at the bottom and move up using "Step -1" at the end of the For statement.

    When you delete rows and move down, you miss some rows.

    I hope this hint is enough for you to see how to do it.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    08-22-2017
    Location
    US
    MS-Off Ver
    2013
    Posts
    4

    Re: Coding Help

    Good hint

    However the problem of comparing each cell to the entire array still alludes me, I'm not able to find the proper syntax online. Here's what I have -

    Private Sub WriteToACell()

    Set ws = ActiveSheet

    Dim SomeArray(3) As Integer

    SomeArray(0) = 1
    SomeArray(1) = 2
    SomeArray(2) = 3
    SomeArray(3) = 4


    Last = Cells(Rows.Count, 1).End(xlUp).Row
    For i = Last To 1 Step -1
    If ws.Cells(i, 1).Value <> SomeArray(0) Then ws.Cells(i, 1).EntireRow.Delete
    Next i

    End Sub


    As you can see I am comparing each cell to SomeArry(0) at the moment (which is 1) as a placeholder for now.

    Thanks!

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Coding Help

    OK SteelMaster,

    Try this:
    Please Login or Register  to view this content.
    Del Row If Not in Arry.xlsm

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Coding Help

    Another version ..
    Please Login or Register  to view this content.
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Coding Help

    Maybe:

    Please Login or Register  to view this content.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Coding Help

    @ Yasser

    The numbers in the array should be the numbers that remain on the worksheet. That said.

    Please Login or Register  to view this content.
    PS. Good use of Application.Match and Arrays. [thumb]
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Coding Help

    Please take a moment to read the forum rules and then amend your thread title to something descriptive of your problem - not what you think the answer might be. (think google search terms?). Once you have done this please send me a PM and I will remove this request. (Also, include a link to your thread - copy from the address bar)

    Many members search our previous posts, and thread titles play a big part of the search. I doubt anybody would do a search based on your title?

    To change a Title on your post, click EDIT POST then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (note: this change is not optional )
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] In need of help regarding combo box coding and button coding (Access form project)
    By mailblade in forum Access Tables & Databases
    Replies: 2
    Last Post: 01-09-2016, 01:34 AM
  2. macro coding help (i have no background in coding)
    By notgoodenough in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-05-2014, 10:22 PM
  3. [SOLVED] Vb macro coding for transpose - complete coding?
    By Dharani Suresh in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-02-2013, 11:16 PM
  4. [SOLVED] Vb macro coding for transpose - complete coding?
    By Dharani Suresh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-02-2013, 05:04 AM
  5. [SOLVED] CheckBox coding to work with ComboBox coding
    By JimExcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2013, 12:23 PM
  6. Coding and Reverse Coding a Questionnaire
    By lorr3 in forum Excel General
    Replies: 2
    Last Post: 11-18-2012, 06:43 PM
  7. Implant macro coding into ASP coding
    By Sam yong in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-15-2005, 06:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1