0

So here is the task I am required to do.

I have a worksheet in which the user can specify a Column name and an element under the column, once chosen, the macro will find and delete every element with said name.

My issue comes from the final part of the macro, the delete. My loop doesn't delete all the rows, it will only find one instance of the element and delete it, then go to the next element and delete it, leaving every other element with the same name intact.

Here is the function within the macro, I apologize in advance for the poor code quality as I am not well versed in vba.

Function LineDelete() As Variant
Dim NbLignes As Integer
Dim ctr As Integer
Dim ctr2 As Integer
Dim Table As Variant

Worksheets("parametrage_suppr_ligne").Activate

ctr = 1
ctr2 = 1
NbLignes = Cells.Find("*", Range("A1"), , , xlByRows, xlPrevious).Row - 4
ReDim Table(1 To NbLignes, 2)


While ctr <= NbLignes
   Table(ctr, 1) = Cells(ctr + 4, 1).Value
   Table(ctr2, 2) = Cells(ctr2 + 4, 2).Value
   ctr = ctr + 1
   ctr2 = ctr2 + 1
Wend

Call FileOpen
Call delInvalidChars
Call OrderRows

Dim newCtr As Integer
Dim rng As Range
Dim rngHeaders As Range
Dim newString As Variant
Dim i As Integer

NbLignes = 0
NbLignes = Cells.Find("*", Range("A1"), , , xlByRows, xlPrevious).Row
Set rngHeaders = Range("1:1")
newCtr = 1

For i = NbLignes To FirstRow Step -1
    Set rng = rngHeaders.Find(Table(newCtr, 1))
    If Table(newCtr, 1) = rng Then
        MsgBox "All is gud!!"
        newString = Cells.Find(Table(newCtr, 2))
        If Table(newCtr, 2) = newString Then
            MsgBox newString
            Range(Cells.Find(Table(newCtr, 2)).Address).EntireRow.Delete
            newCtr= newCtr + 1
        End If
    End If
    newCtr = newCtr + 1
Next i
End Function

So now to explain a bit what I've done here. At first I store the options in a 2 dimentional table with a simple loop, in this table I store the name of the column a well as the name of the element under the column that has to be deleted.

After that I call the functions which open a txt file which is then transformed into an excel file, it is in this new excel file that the deletes have to be done.

I then reset the NbLignes variable as well as call new variables.

Here is where the issues begin, I thought that by iterating on the number of lines the new excel file has; the program was going to look for all of the instances of the word in the column and was going to delete them, but so far it will only do it 3 times.

I am totally lost as to what modify to be able to fix this.

Here is what the config table looks like, this is what the user can modify to specify what to delete, it is also what I store inside of the 2d Table: User can add as many columns and names as needed
User can add as many columns and names as needed

EDIT: What the code does now after updating is that it deletes all the elements that have the same name as the first one in the image (fun_h_opcomp), the expected outcome would be that as soon as all those elements are deleted, the program should then pass on to the next one (fun_b_pterm) and so on.

  • 8
    If you delete rows in a loop you always need to start at the bottom and go backwards to the top like `For i = LastRow To FirstRow Step -1`. Otherwise your loop count's wrong everytime a row gets deleted. Probably you can also fix it by using a `newCtr = newCtr - 1` right after the `.Delete` line (which should then correct the mis-counting). – Pᴇʜ Aug 14 '18 at 11:54
  • 1
    Possible duplicate of [Delete a row in Excel VBA](https://stackoverflow.com/questions/7851859/delete-a-row-in-excel-vba) – eirikdaude Aug 14 '18 at 12:00
  • @Pᴇʜ That actually worked much better, issue is now that now I cannot seem to be able to advance to the other elements inside of the table as newCtr = newCtr - 1 always keeps the counter at 1. Feels good finally seeing some progress on this though, I really appreciate it. – SoftJellyfish Aug 14 '18 at 12:22
  • Please update the code in your question ([edit]) to exactly what you have now. Describe what your code does vs. what it should do. The `newCtr` only stays `1` if **every** row gets deleted. Otherwise it will increase by `+1`. – Pᴇʜ Aug 14 '18 at 12:27
  • Done, I edited with what I have now – SoftJellyfish Aug 14 '18 at 13:01
  • 1
    Your `For loop` is not completed.. you have `Wend` instead of `Next i` – T. Nesset Aug 14 '18 at 13:04
  • Whoops, that was my bad, I miss-edited the code. – SoftJellyfish Aug 14 '18 at 13:09
  • @SoftJellyfish: why use a loop, you could just use [autofilter, then delete the rows in one step using SpecialCells](https://stackoverflow.com/questions/17194394/vba-how-to-delete-filtered-rows-in-excel) – Our Man in Bananas Aug 14 '18 at 15:54

1 Answers1

0

Of course the i was just an Example for that counter and you must use your newCtr counter here, and FirstRow must be set to a value.

Const FirstRow As Long = 1
Dim newCtr As Long 'always use Long for row counting
For newCtr = NbLignes To FirstRow Step -1
    Set rng = rngHeaders.Find(Table(newCtr, 1))
    If Table(newCtr, 1) = rng Then
        MsgBox "All is gud!!"
        newString = Cells.Find(Table(newCtr, 2))
        If Table(newCtr, 2) = newString Then
            MsgBox newString
            Range(Cells.Find(Table(newCtr, 2)).Address).EntireRow.Delete
        End If
    End If
Next newCtr

There is no need to increment/decrement newCtr anymore because this is automatically done by the Next statement.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • Oh ok this works much better, there is an issue though, I think I didn't explain it better, but the Table() variable is where I store the small table from the image, which is in a different worksheet, so in this case it would be Table(283, 1) as that is the last row in the big Excel Worksheet. So in short, Table should only go as far as 3 in this case as that's the number of entries in the other Worksheet, but I should be able to iterate through the whole new Worksheet, which the loop you provided does well. – SoftJellyfish Aug 14 '18 at 13:59
  • @SoftJellyfish: why use a loop, you could just use [autofilter, then delete the rows in one step using SpecialCells](https://stackoverflow.com/questions/17194394/vba-how-to-delete-filtered-rows-in-excel) – Our Man in Bananas Aug 14 '18 at 15:54