+ Reply to Thread
Results 1 to 4 of 4

Macro Loops through 10 rows, find's next cell value > 0 places X in adjacent cell & Stops.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-16-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    7

    Macro Loops through 10 rows, find's next cell value > 0 places X in adjacent cell & Stops.

    Hi All,

    I am looking to create a macro that will loop through a column of at most 10 rows. The macro needs to identify the next number > 0 in the column, set the cell value of the adjacent cell to "X" then stop. When the macro is executed again, it needs to delete the "X" it just created prior to identifying the next number > 0 and placing an "X" in it's corresponding adjacent cell. If the macro has reached the bottom of the range it needs to go back to the top of the range and begin again.

    I'm feeling pretty stuck here and could use some guidance. Attached please find the template for reference.

    Thanks in advance,

    MIACG

    X_Loop.xlsm
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Macro Loops through 10 rows, find's next cell value > 0 places X in adjacent cell & St

    like this ?
    X_Loop.xlsm

    contains:
    Option Explicit
    
    Private Sub cmdStart_Click()
        Dim xlRng As Range
        Dim i As Integer
        Dim lngStart As Integer, lngEnd As Integer, lngCol As Integer
        lngStart = 7
        lngEnd = 16
        lngCol = 4
        Set xlRng = Range(Cells(lngStart, lngCol + 1), Cells(lngEnd, lngCol + 1)).Find(What:="x", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not xlRng Is Nothing Then
            xlRng.ClearContents
            If xlRng.Row <> lngEnd Then
                lngStart = xlRng.Row + 1
            End If
        End If
        
        For i = lngStart To lngEnd
            If Val(Cells(i, lngCol).Value) > 0 Then
                Cells(i, lngCol + 1).Value2 = "X"
                Exit Sub
            End If
        Next i
    End Sub
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    04-16-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro Loops through 10 rows, find's next cell value > 0 places X in adjacent cell & St

    A million thanks to you man. That works brilliantly! Really appreciate your help here.

    I'm pretty new to VBA but I'm trying to learn while I can. So I understand, this macro sets the range based on the start and ending position. Then it identifies an X. If it finds the X, it clears the contents from the cell. If the "X"s position is not at the end of the range then it begins its next position one row below where it cleared the contents of the x. Then the variable "i" is determined in conjunction with the column to determine if the value is > 0, whereby it will execute to the next column (lngCol +1 ) a value of "X". Just curious why do you denote Value2 as such instead of Value?

  4. #4
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Macro Loops through 10 rows, find's next cell value > 0 places X in adjacent cell & St

    Hi,

    it works exactly like you described, for 'value2' see this description from MS: http://msdn.microsoft.com/en-us/libr...ice.11%29.aspx

    In this macro it makes no difference but since I know value2 I don't use value any more while working with date/time values, and as I am doing this a lot I am using value2 for everything

    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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