473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Button Command - Copy and Paste {Excel}

i have a tabel in sheet1 - columns a,b,c,d and rows 2 to 100 with different data.
i want to put in sheet2 a command button to copy row from sheet1( a2,b2,d2) in sheet2 in the cells ( e1,e2,f3). when i press the button command again copy next row from sheet1(a3,b3,d3) in same cells from sheet2, and so long when i press the command button.
p.s-> help and tanks!

Expand|Select|Wrap|Line Numbers
  1. Sub ok_click()
  2. Dim a As Range, b As Range, c As Range
  3. Set a = Sheets("sheet2").Range("e1")
  4. Set b = Sheets("sheet2").Range("e2")
  5. Set c = Sheets("sheet2").Range("f3")
  6. For j = 0 To 1
  7. j = j + 1
  8.     Sheets("sheet1").Cells(j, 5).Copy Destination:=a
  9.     Sheets("sheet1").Cells(j, 2).Copy Destination:=b
  10.     Sheets("sheet1").Cells(j, 3).Copy Destination:=c
  11.  
  12. Next
  13.  
  14.   End Sub
  15.  
Jul 17 '11 #1

✓ answered by Mihail

Note that I rename the sheets. Excel think now that the sheets are named Sh1 and Sh2. On the other way, you'll see that the sheets are named Foaie1 and Foaie2. There are two different things: The internal names of sheets and what you see as sheets names.
I can't test the code in 2003 version. I save for you in that version. Anyway now you have the idea.
Good luck !

11 6190
ADezii
8,834 Expert 8TB
This should point you in the right direction:
Expand|Select|Wrap|Line Numbers
  1. Dim rng1 As Excel.Range
  2. Dim rng2 As Excel.Range
  3.  
  4. Set rng1 = Worksheets("Sheet1").Range("A2:D100")
  5.  
  6. For Each rng2 In rng1
  7.   Worksheets("Sheet2").Cells(rng2.Row, rng2.Column + 4) = rng2.Value
  8. Next
Jul 17 '11 #2
hello, ADezii! i thank you for your response ;), but i have a problem! when i push the command button I would like to copies one row from sheet1(the example: row 2) in sheet2(for example in row 2). when i push the command button again, i want to copies next row from sheet1(the example: row 3) in same place in sheet2 ( row 2 )! i press command button again, i want to copies next row from sheet1( the example: row 4) in same place in sheet2(row 2).
Sorry I don't know so good english!
Jul 18 '11 #3
ADezii
8,834 Expert 8TB
My Post was to only point you in the right direction. What you need to do is to:
  1. Create a Global or Static Variable to keep tract of the Current Row Number.
  2. In the Click() Event of the Command Button on Sheet 2, increment the Value of the Variable representing the Row Number, then copying the data (1 Row at a time) over from Sheet1 keeping the Rows the same but offsetting the Column by 4.
  3. Provide a mechanism to RESET the Variable.
  4. If you are still having problems, let me know, and I'll post something a little more definitive.
Jul 18 '11 #4
Mihail
759 512MB
Hello !
From your name I think you are a Romanian guy, aren't you ?
Of course that no matter. So:

Do you really wish to COPY (values and formats) or you wish only to transfer values ?

One more question:
Why you use a FOR - NEXT cycle and, IN this cycle you increase the value for j ?

Your code:
Expand|Select|Wrap|Line Numbers
  1. For j = 0 to 1
  2.      j=j+1
  3.      ......
  4. Next j
This cycle will be executed only one time and the value for j will be, every time, 1.
Jul 18 '11 #5
Hi!
1)Yes, i'm Romanian guy :D.
2)I wish to transfer values(text)!
3)I used the statement because I thought it goes without writing too many lines of program. it's too very difficult :(
Jul 19 '11 #6
Thanks for the advice. but I have one question - what I do, can be done or not ?
Jul 19 '11 #7
Mihail
759 512MB
Yes. It can be done.

Two more questions:
1) You have a form which contain a button for COPY-PASTE or your button is in a sheet? If it is in a sheet, in which one ?
2) What version of Excel you use?

I understand why you use FOR-NEXT cycle. But I explain you that the statement "j=j+1" INSIDE the cycle is wrong:
When the cycle start, j will be initialized: j=0. Immediately, the statement "j=j+1" asign to j values 1 (j=0+1 => j=1). So, in the next lines j will be 1. When NEXT statement is executed, the cycle variable (j, in your case) will be increased by 1. So j will be... 2 (1+1) grater than the value "1" in "FOR j = 0 to 1". In this case Visual Basic exit from the cycle and execute the statement after NEXT. Hope you understand.

As soon you answer to my questions I'll send you the program to do your job.

Of course, I can explain better in Romanian language but the forum rules... :)

Good luck !
Jul 19 '11 #8
1) the command button is in a sheet. in sheet2.( i don't use form)
2)excel 2003 - sp2


I understand, but the code does not work if you change the value for example: j = 0 to 5 Step 1.
Thanks a lot!
Jul 19 '11 #9
ADezii
8,834 Expert 8TB
I do have a viable solution which I will Post later on. Actually, I'll give you what I have now as an Attachment, and should you have any questions, simply ask.
Attached Files
File Type: zip Copy_Cell_Values.zip (17.1 KB, 177 views)
Jul 19 '11 #10
Mihail
759 512MB
Note that I rename the sheets. Excel think now that the sheets are named Sh1 and Sh2. On the other way, you'll see that the sheets are named Foaie1 and Foaie2. There are two different things: The internal names of sheets and what you see as sheets names.
I can't test the code in 2003 version. I save for you in that version. Anyway now you have the idea.
Good luck !
Attached Files
File Type: zip Book1.zip (12.7 KB, 148 views)
Jul 20 '11 #11
NeoPa
32,556 Expert Mod 16PB
To copy the values of the cells from Sheet1.A2,B2,D2 (Notice we are ignoring column C here) across to Sheet2.E1,E2,F3 (Notice these are three separate cells, but a completely different shape from the From cells) and changing this each time it's used to progress from A2,B2,D2 in Sheet1 up to A100,B100,D100 in the same sheet, before wrapping round again to row 2, you can use code similar to :
Expand|Select|Wrap|Line Numbers
  1. Public Sub OK_Click()
  2.   Static lngRow As Long
  3.   Dim shtTo As Worksheet
  4.  
  5.   lngRow = IIf((lngRow Mod 100) = 0, 2, lngRow + 1)
  6.   Set shtTo = Worksheets("Sheet2")
  7.   With Worksheets("Sheet1")
  8.     shtTo.Range("E1:E2") = .Range(Replace("A%R:B%R", "%R", lngRow)).Value
  9.     shtTo.Range("F3") = .Range("D" & lngRow).Value
  10.   End With
  11. End Sub
Jul 23 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Faith | last post by:
Hello. I need to take a column from Excel(unknown amount of rows) that will be selected by the user and copy those cells. Then I will need to paste those cells into the first column in a Data...
1
by: Sean Howard | last post by:
Dear All, As is my want I need to do something in Access that seems simple but cannot fathom out. I have main form with two subforms, both datasheets with an almost identical table structure....
2
by: Mansi | last post by:
I'm trying to automate excel from c#. One of the things I need to do is to copy/paste/insert rows in excel via c# code. I tried to do the following: 1) Select a row in excel (a12 to k12) 2)...
2
by: Keith | last post by:
I'm trying to come up with a way to create a contextmenu that will do all the "standard" functions (cut, copy, paste, undo, etc). There seems to be a lot of information out there - but nothing...
2
by: Max81 | last post by:
Im trying to build a calculator for an assignment! (due wednesday) im having trouble finding any code for creating Copy/Paste menu buttons (just need the "doing" code) can anyone help? in...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
0
by: Richard Crowley | last post by:
I have a dynamically-generated asp web page which uses Office Web Components to display an Excel bar chart of the user-selected data. The page works fine, but the users would like to copy-n-paste...
7
by: VoTiger | last post by:
Hi everyone, i am ttrying to find a way to do the copy / paste between an existing excel file and my flexgrid (in runtime application). But the fact is that i don't know how to proceed. The...
0
by: six888 | last post by:
i need to make a report using excel. im given a few data in excel document for a 1 month report (1 document for each week). each data document have a few work sheet.i need only certain information in...
2
by: nelsonbrodyk | last post by:
Hey All, I am trying to implement buttons that follow the command pattern. .NET exposes ApplicationCommands.Cut, copy and paste. I am trying the following: <Button...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.