Copy "Value" with row offset of the "Value"

ibmy

Board Regular
Joined
Mar 4, 2020
Messages
125
Office Version
  1. 2016
Platform
  1. Windows
Hi,

offset copy.xlsb
FG
1expected result
2
3
43
5
63
7
811
9
102
112
12
13
14
155
16
17
18
195
20
21
22
Sheet1


Result in G4, I was thinking formula of "Row(6)-3 +1" or "Row(6)+1 -3" but do not know how to implement in vba code.
Data of 100k row.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
@Phuoc , thanks for reply.

My bad, forgot to add Column E.
Column F is the result of sum of "1" in Column E

offset copy.xlsb
EFG
1expected result
2
3
413
51
613
7
8111
9
1012
1112
12
13
14
1515
161
171
181
1915
20
21
22
Sheet1
 
Upvote 0
Try this:

Book1
EFG
1expected result
2
3
4133
51 
61 
7 
8111
9 
10122
111 
12 
13 
14 
15155
161 
171 
181 
191 
20
21
22
23
Sheet1
Cell Formulas
RangeFormula
F4:F19F4=IF(AND(E4=1,E3=""),SUM(E4:INDEX(E4:$E$30,MATCH(TRUE,E4:$E$30="",0))),"")
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
@Phuoc , thanks for the formula, it work fast on data that have less row but slow on data that have lots of row (100k+ rows) .

If in F2 is 3, what is your desired result?
It never happen because let say E2 to E10 contains "1" so total is 9 in F10, so Row(10)-9 +1 = Row(2)

Right now I have 2 idea:
1. Count/ Sum Column E from last row up to E2
2. Offset Copy row using formula "Row(x)-y +1" or "Row(x)+1 -y" ; y=value
 
Upvote 0
Try this:
VBA Code:
Sub ibmy_1()
' group looping

Dim i As Long, j As Long, h As Long
Dim va, vb
Dim t As Double

t = Timer
va = Range("E1", Cells(Rows.Count, "E").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 1)
h = UBound(va, 1)

For i = 2 To h
 j = i
    Do
        i = i + 1
        If i > h Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    i = i - 1
    If va(i, 1) = 1 Then
        vb(j, 1) = i - j + 1
    End If
Next
'result
Range("G1").Resize(UBound(vb, 1), 1) = vb

Debug.Print "Completion time:  " & Format(Timer - t, "0.00") & " seconds"
End Sub

Book3
EFG
1
2
3
413
51
61
7
811
9
1012
111
12
13
14
1515
161
171
181
191
Sheet2
 
Upvote 1
Solution
@Akuini , thanks, the code works great.

May I know what line in code need to change if I want the result in Column F?
 
Upvote 0
@Akuini ,

8.30.xlsb
EF
1expected result :
23
31
41
51
61
71
82
91
101
11
12
135
141
151
161
171
181
19
Sheet7


What to change in code to produce result in 1 up cell?
Currently I add this line at the end of code.
VBA Code:
Range("F2").Select
Selection.Delete Shift:=xlUp

VBA Code:
Sub ResultF_start()
' group looping

Dim i As Long, j As Long, h As Long
Dim va, vb

va = Range("E1", Cells(Rows.Count, "E").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 1)
h = UBound(va, 1)

For i = 2 To h
 j = i
    Do
        i = i + 1
        If i > h Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    i = i - 1
    If va(i, 1) = 1 Then
        vb(j, 1) = i - j + 1
    End If
Next

'result
Range("F1").Resize(UBound(vb, 1), 1) = vb

Range("F2").Select
Selection.Delete Shift:=xlUp

Debug.Print "Completion time:  " & Format(Timer - t, "0.00") & " seconds"

End Sub
 
Upvote 0
What to change in code to produce result in 1 up cell?
In my code on post #6, just change this part:
Rich (BB code):
vb(j, 1) = i - j + 1
to this:
Rich (BB code):
vb(j - 1, 1) = i - j + 1
 
Upvote 1

Forum statistics

Threads
1,216,494
Messages
6,130,977
Members
449,611
Latest member
Bushra

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