What am I missing here? (VBA Code)

goldenvision

Board Regular
Joined
Jan 13, 2004
Messages
234
Surely this shouldn't be so complicated? I am missing something obvious here. I just don't know what it is.

I am trying to copy a value from cell A1 in sheet From and paste it into the next available empty cell in column B of sheet To

Code:
Private Sub CommandButton1_Click()
Dim rownumber As Integer

Sheets("From").Select
Range("A1").Select
Selection.Copy

Sheets("To").Select
rownumber = 1
Do Until Cells(rownumber, 2) = ""
MsgBox rownumber

Range("B" & rownumber).Select
Selection.Paste
rownumber = rownumber + 1
Loop
End Sub

help
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi
Paste the following codes in the macro window ( Alt f8)

Code:
x = Worksheets("sheet2").Cells(Rows.Count, 2).End(xlUp).Row
Worksheets("sheet2").Cells(x + 1, 2) = Worksheets("sheet1").Cells(1, 1)
Run the macro. It will add cell A1 of sheet1 to col B of sheet 2
Ravi
 
Upvote 0
i can see how that solution will work. go to the last cell and move up until you hit a blank. i really want to know why the solution i am trying to implement won't work.
 
Upvote 0
You need to put your Paste otside the loop:

Code:
Private Sub CommandButton1_Click() 
   Dim rownumber As Integer 
   Sheets("From").Select 
   Range("A1").Select 
   Selection.Copy 
   Sheets("To").Select 
   rownumber = 1 
   Do Until Cells(rownumber, 2) = "" 
      MsgBox rownumber 
      rownumber = rownumber + 1 
   Loop 
   Range("B" & rownumber).Select 
   Selection.Paste 
End Sub
 
Upvote 0
You need to put your Paste otside the loop:

Code:
Private Sub CommandButton1_Click() 
   Dim rownumber As Integer 
   Sheets("From").Select 
   Range("A1").Select 
   Selection.Copy 
   Sheets("To").Select 
   rownumber = 1 
   Do Until Cells(rownumber, 2) = "" 
      MsgBox rownumber 
      rownumber = rownumber + 1 
   Loop 
   Range("B" & rownumber).Select 
   Selection.Paste 
End Sub

As that well known philosopher Homer J Simpson once said "D'Oh!"
 
Upvote 0
Private Sub CommandButton1_Click()
Dim rownumber As Integer
Sheets("From").Select
Range("A1").Select
Selection.Copy
Sheets("To").Select
rownumber = 1
Do Until Cells(rownumber, 2) = ""
' MsgBox rownumber
rownumber = rownumber + 1
Loop
Cells(rownumber, 2).Select Falls over here
Selection.Paste
End Sub
 
Upvote 0
Try This -

Code:
Private Sub CommandButton1_Click()
Dim rownumber As Integer
Sheets("From").Select
Range("A1").Select
Selection.Copy
Sheets("To").Select
rownumber = 1
Do Until Sheets("To").Cells(rownumber, 2) = ""
' MsgBox rownumber
rownumber = rownumber + 1
Loop
Sheets("To").Cells(rownumber, 2).Select
ActiveSheet.Paste
End Sub

DP
 
Upvote 0
You know what, it beats me :eek:

All I know is that sometimes if you want to select a Cell you need to specify the Sheet as well.

Maybe someone can enlighten us as to why!
 
Upvote 0
You really don't need to use Select.

And I don't see why you need a loop.
Code:
Private Sub CommandButton1_Click()

Sheets("From").Range("A1").Copy Sheets("To").Range("B" & Rows.Count).End(xlUp).Offset(1)

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,047
Members
448,940
Latest member
mdusw

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