User Form to Copy Rows

pwill

Active Member
Joined
Nov 22, 2015
Messages
406
Hi can anyone help with vba code for a UserForm?

I have a UserForm with Textbox1 under a Label(Start) and Textbox2 under a Label(How Many)
And a CommandButton

On Sheet1 -

Column A with Dates ("A2:A)

Column D with a Count From 1 to 12000 ("D2:D)

Columns E2:K with Row Data for each Date and Count

I want to enter a Count value from ColumnD into Textbox1 - ie - 100

Then in Textbox2 enter how many - ie - 20

Then Click the CommandButton and Copy the 20 Rows of Data From Columns ("A101:A120" & ", D101:K120") To Columns ("AI2:AQ")

eg

ABCDEFGHIJKLMAGAHAIAJAKALAMANAOAPAQ
1Date/YearCntResultsDate/YearCntResults
207/04/181000000026/05/181000000000
307/04/182000000027/05/181010000000
408/04/183000000027/05/181020000000
508/04/184000000028/05/181030000000
609/04/185000000028/05/181040000000
709/04/186000000029/05/181050000000
810/04/187000000029/05/181060000000
910/04/188000000030/05/181070000000
1011/04/189000000030/05/181080000000
1111/04/1810000000031/05/181090000000
1212/04/1811000000031/05/181100000000
1312/04/1812000000001/06/181110000000
1413/04/1813000000001/06/181120000000
1513/04/1814000000002/06/181130000000
1614/04/1815000000002/06/181140000000
1714/04/1816000000003/06/181150000000
1815/04/1817000000003/06/181160000000
1915/04/1818000000004/06/181170000000
2016/04/1819000000004/06/181180000000
2116/04/1820000000005/06/181190000000
2217/04/18210000000
2317/04/18220000000
2418/04/18230000000
2518/04/18240000000
2619/04/18250000000

<tbody>
</tbody>


Any help would be appreciated

Regards

pwill
 
Last edited by a moderator:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This would start in the exact row selected, 100 not 101 and would end at 120. Also as is, it copies over anything in the destination columns
From your command button, call copyrows

Code:
sub copyrows
dim startrow as long
dim endrow as long
dim x as long
dim cnt as long
startrow = int(userform1.textbox1.value)
endrow = int(userform1.textbox2.value)
if  startrow<1 or endrow<1 or endrow>startrow then
msgbox "Invalid start or end rows",vbcritical,"ALERT"
exit sub
endif
cnt = 2 'indicate first row where data should copy (could use lastrow+1 of destination column if you want to keep appending)
endrow = startrow+endrow
for x = startrow to endrow
cells(cnt,"AI") = cells(x,"a")
cells(cnt,"AJ") = cells(x,"b")
cells(cnt,"AK") = cells(x,"c")
cells(cnt,"AL") = cells(x,"d")
cells(cnt,"AM") = cells(x,"e")
cells(cnt,"AN") = cells(x,"f")
cells(cnt,"AO") = cells(x,"g")
cells(cnt,"AP") = cells(x,"h")
cells(cnt,"AQ") = cells(x,"i")
cnt = cnt + 1
next x
end sub
 
Last edited:
Upvote 0
Hi Roderick_E, Thank you that's exactly what I need and I can adjust to my needs with the rows but when I try to enter below 10 as a start value in textbox1 it gives the message box error?

is it possible adjust the code to include values starting from below 10?

pwill
 
Upvote 0
Hi Roderick_E, Thank you that's exactly what I need and I can adjust to my needs with the rows but when I try to enter below 10 as a start value in textbox1 it gives the message box error?

is it possible adjust the code to include values starting from below 10?

pwill

Glad it worked. I fixed that issue in this version:

Code:
sub copyrows
dim startrow as long
dim endrow as long
dim x as long
dim cnt as long
startrow = int(userform1.textbox1.value)
endrow = int(userform1.textbox2.value)
if  startrow<1 or endrow<1 then
msgbox "Invalid start or end rows",vbcritical,"ALERT"
exit sub
endif
cnt = 2 'indicate first row where data should copy (could use lastrow+1 of destination column if you want to keep appending)
endrow = startrow+endrow
for x = startrow to endrow
cells(cnt,"AI") = cells(x,"a")
cells(cnt,"AJ") = cells(x,"b")
cells(cnt,"AK") = cells(x,"c")
cells(cnt,"AL") = cells(x,"d")
cells(cnt,"AM") = cells(x,"e")
cells(cnt,"AN") = cells(x,"f")
cells(cnt,"AO") = cells(x,"g")
cells(cnt,"AP") = cells(x,"h")
cells(cnt,"AQ") = cells(x,"i")
cnt = cnt + 1
next x
end sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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