Andreas w
Andreas w

Reputation: 11

Delete row in Excel

how can i make it so this vba scripts deletes row containing a Percentage

Sub Macro2()

    Dim celltxt As String
    Dim a As Variant

    For Each i In Range(Cells(3, 2), Cells(160, 2))
        If i.Value > 1 Then
            i.Offset(0, 2).Value = i.Offset(0, 2).Value + 1
        End If

        If i.Value < 1 Then
            i.Offset(0, 2).Value = i.Offset(0, 2).Value + "NEI"
        End If

        If i.Value = "%" Then
            i.Offset(0, 2).Value = i.Offset(0, 3).Value + "joda"
        End If
    Next i

End Sub

Sub slettRadD()
    Range("D1:D160").Select
    Selection.ClearContents
End Sub

Sub SlettAlle()
    Range("A3:D160").Select
    Selection.ClearContents
End Sub

in this section

If i.Value = "%" Then
    i.Offset(0, 2).Value = i.Offset(0, 3).Value + "joda"
End If

i need it to delete the Horisontal row, or just delete the content in that cell. containing a percentage or percentage sign. just checked to see if i could even find it, but it does not seem work just adding a value in those colums.

Upvotes: 0

Views: 84

Answers (3)

LFB
LFB

Reputation: 686

Try

If i.Value = "%" Or Right$(i.NumberFormat,1) = "%" Then i.EntireRow.Delete

I think it will do the job. But it is going to possibly skip some rows, because after deletion, it won't compensate de increment in the loop.

If you need to delete ranges, try doing it from the bottom-up or right-left

This link has options: Reverse order of For Each loop

Upvotes: 0

QHarr
QHarr

Reputation: 84465

For formatted cells and if just cell contains (%)

If Right$(i.NumberFormat, 1) = "%"  Or i = "%" Then

Upvotes: 1

Raunak Thomas
Raunak Thomas

Reputation: 1403

If i.Style = "Percent" Then
'Do Whatever here for example
  i.ClearContents
End If

Upvotes: 2

Related Questions