Loop to delete rows if it finds particular text

fari1

Active Member
Joined
May 29, 2011
Messages
362
I want the vba code, that loops through each row from A1:J20 and if it finds $ sign in any first row(means the first occurance of $) i want it to delete all the rows above it.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Give this macro a try and see if it does what you want...
Code:
Sub ClearAboveDollarSign()
  Dim DollarSign As Range
  Set DollarSign = Range("A1:J20").Find("$", Range("J20"), xlValues, xlPart, xlByRows, xlNext)
  If Not DollarSign Is Nothing Then
    If DollarSign.Row > 1 Then Rows("1:" & DollarSign.Row - 1).ClearContents
  End If
End Sub
 
Upvote 0
brilliant, its awsum:) thanks alot, but i also want to offset this data to the first row, move the data starting from $ sign to the first row of the sheet
 
Upvote 0
brilliant, its awsum:) thanks alot, but i also want to offset this data to the first row, move the data starting from $ sign to the first row of the sheet
In that case, change the ClearContents to Delete...
Code:
Sub ClearAboveDollarSign()
  Dim DollarSign As Range
  Set DollarSign = Range("A1:J20").Find("$", Range("J20"), xlValues, xlPart, xlByRows, xlNext)
  If Not DollarSign Is Nothing Then
    If DollarSign.Row > 1 Then Rows("1:" & DollarSign.Row - 1).Delete
  End If
End Sub
 
Upvote 0
yea its perfect, thanks alot, can this code include one more function? i want the code to look for $ sign from column A:I, i want to delete the columns that has $ sign
 
Upvote 0
Sorry about the delay... I had gone to sleep for the night. Give this modification to my macro a try (it will handle your initial request in addition to your latest one)...
Code:
Sub ClearAboveDollarSign()
  Dim DollarSign As Range, Cell As Range, FirstAddress As String
  Set DollarSign = Range("A1:J20").Find("$", Range("J20"), xlValues, xlPart, xlByRows, xlNext)
  If Not DollarSign Is Nothing Then
    If DollarSign.Row > 1 Then Rows("1:" & DollarSign.Row - 1).Delete
  End If
  With Columns("A:I")
    Set Cell = .Find("$", Range("I" & Rows.Count), xlValues, xlPart, xlByRows, xlNext)
    If Not Cell Is Nothing Then
      Do
        Cell.EntireColumn.Delete
        Set Cell = .Find("$", Range("I" & Rows.Count), xlValues, xlPart, xlByRows, xlNext)
      Loop While Not Cell Is Nothing
    End If
  End With
End Sub
 
Last edited:
Upvote 0
No worries, thanks for your reply.
someone from this forum already replied to it and its working, i asked another question, will you please be so considerate to resolve it, i want to move data from one column to another, my sheet has two column of just closing brackets ")", i want the vba code to look through columns A:I and look for the closing brackets, if it finds them in a column then move them to the left column which already contain numeric data. e.g (24 is in one column and closing bracket is in other. i want to combine them in one column.
will be highly thankful if you resolve this. my thread is located at
http://www.mrexcel.com/forum/showthread.php?p=2792674
 
Upvote 0
*** CORRECTION TO MY LAST MESSAGE ****

I would have edited my last message but the 10-minute time limit had expired. Anyway, use this code instead of what I posted last (there is a problem with that code)...
Code:
Sub ClearAboveDollarSign()
  Dim DollarSign As Range, Cell As Range, FirstAddress As String
  Set DollarSign = Range("A1:J20").Find("$", Range("J20"), xlValues, xlPart, xlByRows, xlNext)
  If Not DollarSign Is Nothing Then
    If DollarSign.Row > 1 Then Rows("1:" & DollarSign.Row - 1).Delete
  End If
  With Columns("A:I")
    Set Cell = .Find("$", Range("I" & Rows.Count), xlValues, xlPart, xlByRows, xlNext)
    If Not Cell Is Nothing Then
      Do
        Cell.Value = "#N/A"
        Set Cell = .Find("$", Range("I" & Rows.Count), xlValues, xlPart, xlByRows, xlNext)
      Loop While Not Cell Is Nothing
    End If
    .SpecialCells(xlCellTypeConstants, xlErrors).EntireColumn.Delete
  End With
End Sub

FOLLOW UP NOTE: You said in a message before this one that someone from the forum had resolve your latest question... where? I do not see such a message in this thread... all I see are messages between you and me.
 
Last edited:
Upvote 0
Thanks, its working, plz reply to my other code, i desparately need the solution of that
 
Upvote 0
Thanks, its working, plz reply to my other code, i desparately need the solution of that
I just did.

I'm still curious, though, where the other message you referred to is. Are you saying you see a message in this thread from someone other than you are me? If so, please post that person's sign-on name. Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,767
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top