+ Reply to Thread
Results 1 to 6 of 6

Loop the code untill end

Hybrid View

  1. #1
    Registered User
    Join Date
    02-29-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    41

    Loop the code untill end

    Hi,

    I have a code which searches for 2 values in a column and replaces value in rows in between those values. But i have many cobinations like this and the code works for only first , so i just want to loop untill last cell in excel.

    My code is :

    Sub Test() 
    Dim F1 As Range, F2 As Range
    With ActiveSheet
    Set F1 = .Columns("H").Find(What:="FIRST", LookIn:=xlValues, LookAt:=xlWhole)
    Set F2 = .Columns("H").Find(What:="SECOND", LookIn:=xlValues, LookAt:=xlWhole)
    If Not F1 Is Nothing And Not F2 Is Nothing Then ActiveSheet.Rows(F1.Row & ":" & F2.Row).Select
    
    Selection.Replace What:="XG13", Replacement:="REPLACED", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Selection.Replace What:="XG14", Replacement:="REPLACED", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    End With
    End Sub

    Kindly help with this ,

    Thanks & Regards ,

    Mayur
    Last edited by jeffreybrown; 07-12-2013 at 05:00 PM. Reason: Added code tags...

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

    Re: Loop the code untill end

    Maybe:

    Sub mayurpatil()
    Dim x As Long
    x = ActiveSheet.UsedRange.Rows.Count
    For Each cell In Range("H2:H" & ActiveSheet.UsedRange.Rows.Count)
        If cell.Value = "FIRST" Then cell.Select
        Do Until ActiveCell.Value = "SECOND" Or ActiveCell.Value = "" Or ActiveCell.Row = x
            ActiveCell.Replace "XG13", "REPLACED", xlPart
            ActiveCell.Replace "XG14", "REPLACED", xlPart
            ActiveCell.Offset(1).Select
        Loop
    Next cell
    End Sub
    Untested, try on a sample first.

  3. #3
    Registered User
    Join Date
    02-29-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Loop the code untill end

    Hi,

    Sorry not working John . Please if anyone can just loop the code . Bcoz its working justr not getting loop .

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

    Re: Loop the code untill end

    Can you attach a file with some sample data?

    To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.

  5. #5
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Loop the code untill end

    @Mayur,

    Administrative Note:
    • We would love to continue to help you with your query, but first, before we can proceed…
    • Please see Forum Rule #3 about the proper use of code tags and adjust accordingly...
    HTH
    Regards, Jeff

  6. #6
    Registered User
    Join Date
    02-29-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Loop the code untill end

    Hi,jeffreybrown

    My apologies for the same , I will reframe the question.

    I have a code which searches for 2 values in a column and replaces value in rows in between those values. But i have many cobinations like this and the code works for only first , so i just want to loop untill last cell in excel.

    My code is :

    Sub Test() 
    Dim F1 As Range, F2 As Range
    With ActiveSheet
    Set F1 = .Columns("H").Find(What:="FIRST", LookIn:=xlValues, LookAt:=xlWhole)
    Set F2 = .Columns("H").Find(What:="SECOND", LookIn:=xlValues, LookAt:=xlWhole)
    If Not F1 Is Nothing And Not F2 Is Nothing Then ActiveSheet.Rows(F1.Row & ":" & F2.Row).Select
    
    Selection.Replace What:="XG13", Replacement:="REPLACED", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Selection.Replace What:="XG14", Replacement:="REPLACED", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    End With
    End Sub

    Kindly help with this ,

    Thanks & Regards ,

    Mayur

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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