Code to get data from data 10 lines away get 1 line

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone. I have a data range A1:B10000. I want to get 1 row every 10 lines to output column D, C. so that the code runs as fast as possible. Sincerely thank
1681460036292.png

 
try this:
VBA Code:
Sub test()
inarr = Range("a1:d10000")
Dim outarr(1 To 1000, 1 To 4)
indi = 1
For i = 1 To 10000 Step 10
 outarr(indi, 1) = inarr(i, 1)
 outarr(indi, 2) = inarr(i, 2)
 outarr(indi, 3) = inarr(i, 3)
 outarr(indi, 4) = inarr(i, 4)
 indi = indi + 1
Next i
Range("h1:k100") = outarr

 
End Sub
note how easy it is to change the ranges if you use absolute addressing, since learning how to use variant arrays is the easiest way to learn how to write fast vba code. It worth looking at how simple it can be to use variant arrays. The extra time taken to loop through a load of empty rows is insignificant compared to the saving in learning how NOT to loop through a worksheet
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I would edit the code to something like:
VBA Code:
Sub test()
    Dim iVar As Variant, oVar() As Variant, x As Long, z As Long
 
    iVar = Range("A1:D" & Range("A" & Rows.Count).End(xlUp).Row).Value
    ReDim oVar(UBound(iVar) / 10, UBound(iVar, 2) - 1)
 
    For x = 1 To UBound(iVar) Step 10
        oVar(z, 0) = iVar(x, 1)
        oVar(z, 1) = iVar(x, 2)
        oVar(z, 2) = iVar(x, 3)
        oVar(z, 3) = iVar(x, 4)
        z = z + 1
    Next x
 
    Range("H1").Resize(UBound(oVar) + 1, UBound(oVar, 2) + 1) = oVar
End Sub

I have made the ReDim part to be dynamic to the range size 'iVar'
I have also changed the last line of code to be dynamic to the size of the output array 'oVar'
Thank You
 
Upvote 0
Probably no use to you but I do love Excel 365 as an 365 option could be something like the below:
VBA GET 10 TO 1 (1).xlsm
ABCDEFGHIJK
11A1B1C11A1B1C1
22A2B2C211A11B11C11
33A3B3C321A21B21C21
44A4B4C431A31B31C31
55A5B5C541A41B41C41
66A6B6C651A51B51C51
77A7B7C761A61B61C61
88A8B8C871A71B71C71
99A9B9C981A81B81C81
1010A10B10C1091A91B91C91
1111A11B11C11
1212A12B12C12
1313A13B13C13
1414A14B14C14
1515A15B15C15
1616A16B16C16
1717A17B17C17
1818A18B18C18
1919A19B19C19
2020A20B20C20
2121A21B21C21
2222A22B22C22
2323A23B23C23
2424A24B24C24
2525A25B25C25
2626A26B26C26
2727A27B27C27
2828A28B28C28
2929A29B29C29
3030A30B30C30
3131A31B31C31
3232A32B32C32
3333A33B33C33
3434A34B34C34
3535A35B35C35
3636A36B36C36
3737A37B37C37
3838A38B38C38
3939A39B39C39
4040A40B40C40
4141A41B41C41
4242A42B42C42
4343A43B43C43
4444A44B44C44
4545A45B45C45
4646A46B46C46
4747A47B47C47
4848A48B48C48
4949A49B49C49
5050A50B50C50
5151A51B51C51
5252A52B52C52
5353A53B53C53
5454A54B54C54
5555A55B55C55
5656A56B56C56
5757A57B57C57
5858A58B58C58
5959A59B59C59
6060A60B60C60
6161A61B61C61
6262A62B62C62
6363A63B63C63
6464A64B64C64
6565A65B65C65
6666A66B66C66
6767A67B67C67
6868A68B68C68
6969A69B69C69
7070A70B70C70
7171A71B71C71
7272A72B72C72
7373A73B73C73
7474A74B74C74
7575A75B75C75
7676A76B76C76
7777A77B77C77
7878A78B78C78
7979A79B79C79
8080A80B80C80
8181A81B81C81
8282A82B82C82
8383A83B83C83
8484A84B84C84
8585A85B85C85
8686A86B86C86
8787A87B87C87
8888A88B88C88
8989A89B89C89
9090A90B90C90
9191A91B91C91
9292A92B92C92
9393A93B93C93
9494A94B94C94
9595A95B95C95
9696A96B96C96
9797A97B97C97
9898A98B98C98
9999A99B99C99
100100A100B100C100
Sheet1
Cell Formulas
RangeFormula
H1:K10H1=FILTER(A1:D100,--RIGHT(A1:A100,1)=1)
Dynamic array formulas.
 
Upvote 0
try this:
VBA Code:
Sub test()
inarr = Range("a1:B10000")
Dim outarr(1 To 1000, 1 To 2)
indi = 1
For i = 1 To 10000 Step 10
 outarr(indi, 1) = inarr(i, 1)
 outarr(indi, 2) = inarr(i, 2)
 indi = indi + 1
Next i
Range("d1:e100") = outarr

 
End Sub
this should be much faster than looping through the workssheet
Thank You
 
Upvote 0
Hi,

I am trying to use the code above for similar VBA for two different scenarios but am getting stuck.
I have tried to look at .PasteSpecial xlPasteValues but finding it difficult.

1st VBA
for all rows A1:B10000 copied over to column D and E.

2nd VBA
For first 50 rows for A1:B10000 copied over to column D and E.

Could someone provide help please.

Thank-you
 
Upvote 0
I virtually never use paste values in vba because I find it less typing to copy to a variant array like this
:
VBA Code:
Sub allrowswithvalues()
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(LastRow, 2))
Range(Cells(1, 3), Cells(LastRow, 4)) = inarr
End Sub
Sub rows50()
LastRow = 50
inarr = Range(Cells(1, 1), Cells(LastRow, 2))
Range(Cells(1, 3), Cells(LastRow, 4)) = inarr
End Sub
 
Upvote 0
I virtually never use paste values in vba because I find it less typing to copy to a variant array like this
:
VBA Code:
Sub allrowswithvalues()
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(LastRow, 2))
Range(Cells(1, 3), Cells(LastRow, 4)) = inarr
End Sub
Sub rows50()
LastRow = 50
inarr = Range(Cells(1, 1), Cells(LastRow, 2))
Range(Cells(1, 3), Cells(LastRow, 4)) = inarr
End Sub
Thank-you @offthelip so much for the 2 VBA this really helps, I do have a following up, if I needed to change cells from A1B1 to A3B3 to copy to D3E3 instead of D3E3 where in the VBA would I update this?
Thank-you
 
Upvote 0
The Range addressing system I was using uses row and column numbers instead of a column letter and a row number. Unfortunately for whatever reason EXCEL uses the row and column in the reverse order, ie. row then column, so in my equation :
VBA Code:
inarr = Range(Cells(1, 1), Cells(LastRow, 2))
there are 4 numbers which are :
VBA Code:
inarr = Range(Cells(startrownumber, startcolumnnumber), Cells(LastRownumber, lastcolumnnumber))
so to change to copy from starting in 1 to starting in row 3 you need change it to
VBA Code:
inarr = Range(Cells(3, 1), Cells(LastRow, 2))
you then need to do the same when you write it back to the worksheet :
change:
VBA Code:
Range(Cells(1, 3), Cells(LastRow, 4)) = inarr
to
VBA Code:
Range(Cells(3, 3), Cells(LastRow, 4)) = inarr
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,254
Members
449,305
Latest member
Dalyb2

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