PDA

View Full Version : [SOLVED] Excel Code Help, Loop related

tzexu1610
01-15-2018, 06:40 PM
Hi guys

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

SamT
01-15-2018, 07:42 PM
L1 Formula = "=If(I1=J1,J1,If(I1<J1,I1+1,0))"
Copy down

tzexu1610
01-15-2018, 07:57 PM
hi thanks for your reply but i kinda need a different outcome.

something like this:

i j L

940897
940897

940897

940902
940921

940902

940921
940921

940903

940904

940905

940906

until 940921

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.

p45cal
01-16-2018, 03:55 AM
If you must cross-post then you are supposed to supply links to those cross posts.
https://www.mrexcel.com/forum/excel-questions/1039060-excel-code-help-loop-related.html
Anywhere else? (MrExcel has the same requirement)

I suggest you attach a small workbook where:
On Sheet 1 you have your original arrangement of data
On Sheet 2 you have what you want the result to be.

Computers aren't very good with the likes of 'sorta' and 'kinda'.

SamT
01-16-2018, 05:09 AM
i kinda need a different outcome.
That's an entirely different situation than what's in your first post.

Are you sure that's what you want? I would hate to have you change your mind again after somebody put in a lot of time and effort on providing that.

tzexu1610
01-16-2018, 05:39 PM
That's an entirely different situation than what's in your first post.

Are you sure that's what you want? I would hate to have you change your mind again after somebody put in a lot of time and effort on providing that.

All G Sam I managed to figure it out. Thanks everyone for helping :)