Excel Code Help, Loop related

tzexu1610

New Member
Joined
Jan 15, 2018
Messages
15
So... I've got 3 columns in my excel spreadsheet, I,J and L. Looks something like this:

I J L
1 1 1
3 3 3
4 7 4
5
6
7

The column L is supposedly empty but I wanna make those numbers appear there.L would be based on I and J. The below is my code. Some help would be much appreciated .

Sub trial2()


Dim cell As Range
Dim i As Integer
Dim j As Integer
Dim c As Range




i = 0
j = 0


For Each c In Columns("L")
i = i + 1
j = j + 1
c = i

If Cells(i, "I").Value = Cells(i, "J").Value Then
Cells(j, "L").Value = Cells(i, "J").Value
ElseIf Cells(i, "I").Value < Cells(i, "J").Value Then
Cells(j, "L").Value = Cells(i, "I").Value + 1



End If


Next c


End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
MAybe this

Code:
Sub trial2()
Dim cell As Range, i As Integer, j As Integer
Dim lr As Long
lr = Cells(Rows.Count, "I").End(xlUp).Row
i = 0
j = 0
For Each cell In Range("I1:I" & lr)
    i = i + 1
    j = j + 1
    If Cells(i, "I").Value = Cells(i, "J").Value Then
        Cells(j, "L").Value = Cells(i, "J").Value
        ElseIf Cells(i, "I").Value < Cells(i, "J").Value Then
        Cells(j, "L").Value = Cells(i, "I").Value + 1
    End If
Next cell
End Sub
 
Upvote 0
Hi thanks a lot for replying Michael. Not exactly what I need though. This is what comes out.
i
940897
j
940897
L
940897
940902940921940903
940921940921940921

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>

but i need something like this:

i j L
940897940897940897
940902940921940903
940921940921940904

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>
940905
940906
all the way until 940921

so basically i need a list of the numbers. sorta in between i and j .
 
Upvote 0
oops the 940905, 940906...... 940921 are supposed to be under column l . the formatting is really bad sorry im like a first timer
 
Upvote 0
How do you get 940904 in the last row of the example ???
Your condition is, if I = J then use J !
In the last line of your example you are using a number that has no relevance to either I or J ???
 
Upvote 0
How do you get 940904 in the last row of the example ???
Your condition is, if I = J then use J !
In the last line of your example you are using a number that has no relevance to either I or J ???

ah sorry i made a mistake, didnt see that.



but i need something like this:

i j L
940897940897940897
940902940921940902
940921940921940903

940904

940905

940906

until 940921


<tbody>
</tbody>


the heading for i and j are sorta like sample start and sample end. while L heading is like total samples. Hope that helps you understand. Sorry for the inconvenience.
 
Upvote 0
Hope that helps you understand

Sorry, the data you are posting makes no logical sense to me

Maybe instead of posting "sorta" data, you post real time data we can work with..
 
Upvote 0
Sorry, the data you are posting makes no logical sense to me

Maybe instead of posting "sorta" data, you post real time data we can work with..

Ah sorry that actually is real time data. i just changed the headers hoping it'll help u understand better but i'll post original as u requested.



from(I) to(J) total(L)
940897940897
940902940921
940921940921

<tbody>
</tbody>


so above is original. below is what i need to generate using macro.

from(I) to(J) total(L)
940897940897940897
940902940921940902
940921940921940903

940904

940905

940906

until 940921

<tbody>
</tbody>



 
Upvote 0
Maybe this, but it assumes there will be data in cols I & J

Code:
Sub trial2()
Dim cell As Range, i As Integer, j As Integer
Dim lr As Long
lr = Cells(Rows.Count, "I").End(xlUp).Row
i = 1
j = 1
For Each cell In Range("I2:I" & lr)
    i = i + 1
    j = j + 1
    If Cells(i, "I").Value < Cells(i, "J").Value Then
        Cells(j, "L").Value = Cells(i, "I").Value + 1
        ElseIf Cells(i, "I").Value = Cells(i, "J").Value Then
        Cells(j, "L").Value = Cells(i - 1, "L").Value + 1
    End If
Next cell
End Sub
 
Upvote 0
Maybe this, but it assumes there will be data in cols I & J

Code:
Sub trial2()
Dim cell As Range, i As Integer, j As Integer
Dim lr As Long
lr = Cells(Rows.Count, "I").End(xlUp).Row
i = 1
j = 1
For Each cell In Range("I2:I" & lr)
    i = i + 1
    j = j + 1
    If Cells(i, "I").Value < Cells(i, "J").Value Then
        Cells(j, "L").Value = Cells(i, "I").Value + 1
        ElseIf Cells(i, "I").Value = Cells(i, "J").Value Then
        Cells(j, "L").Value = Cells(i - 1, "L").Value + 1
    End If
Next cell
End Sub


Solved! Thanks for your help[ Michael! :)
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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