Loop to compare Cells in a Column

KORKIS2

Board Regular
Joined
Jun 5, 2015
Messages
143
So I want to go down a column and compare cells.
I know alot of C programming but not much VBA.

So here is a flow of what I want.
Lets Say

Basically keep looping and working down the Column until the next spot it larger than previous then exit the loop.



P= blank array
X= Cell Value A1
Y= Cell A2

IF X <Y Then
Y stored in P exit
Else Y+1 =Y(Meaning A3)
Return to IF

Basically keep looping and working down the Column until the next spot it larger than previous then exit the loop.
 
You've changed the start row to 8, you told me earlier that it started on 7. You can't change that number without changing other parts of the code. Please set it back to 7 temporarily (in both your worksheet and in the VBA code) for testing purposes.

Is Sheet1 the correct sheet name for the sheet you are working with? Do you see the list of sheet names in the window pane on the left hand side of your screen? Sheet1 is the sheet codename, not the name you see in the workbook itself. The displayed sheet names are in parenthesis in that window. Make sure you are using the correct code name, or if you want to use the true sheet name, replace all instances of "sheet1" with 'Worksheets("yoursheetname") '

Finally, let's make sure you are actually running the code. add one additional line at the very end:

msgbox "test"

then run the code and make sure that message box comes up.
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The messege box pops up I changed everything back to 7 I actually just recopied the code you put in originally. I made sure the sheet name was sheet1 :(
I don't understand. I've been looking at it all afternoon.
 
Upvote 0
Try this code with the additional messageboxes:

Code:
Sub GetRows()

Dim lLastRow As Long
Dim aSrc As Variant
Dim aResults() As String   'item, start row, stop row
Dim i As Long, k As Long
Dim sCurrVal As String

lLastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
msgbox llastrow
aSrc = Sheet1.Range("A7:A" & lLastRow) 'this is your raw data

sCurrVal = aSrc(1, 1) 'set current item
ReDim aResults(1 To 3, 1 To 1) 'create result array
aResults(1, 1) = sCurrVal
aResults(2, 1) = 7 'assumes your data starts in row 1

k = 1 'result counter
 
For i = 1 To UBound(aSrc, 1)
    If aSrc(i, 1) <> sCurrVal Then
        'write stop row value
        aResults(3, k) = i + 5
        'set new search item
        sCurrVal = aSrc(i, 1)
        'load result array
        k = k + 1
        ReDim Preserve aResults(1 To 3, 1 To k)
        aResults(1, k) = sCurrVal
        aResults(2, k) = i + 6
    End If
Next i
aResults(3, k) = i + 6 'this will capture the last entry
msgbox k & " items searched"
msgbox "Result array first entry: " & aresults(1,1) & " " & aresults(2,1)
'now write result array to worksheet in 3 columns
Sheet1.Range("D1").Resize(k, 3) = Application.Transpose(aResults)

End Sub

report back what each of those message boxes says
 
Upvote 0
What were you talking about with defining the sheet on the left side of the panel I only know how to change the name by going to the bottoms tabs
 
Upvote 0
change every instances of "Sheet1" in my code to:

Worksheets("Your sheet name here")

Again, "Sheet1" is the sheet codename, which you can only see in the visual basic editor, in the top left corner. And yes, by default in a new workbook, my "Sheet1" will always point to workbook "Sheet 1"
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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