Macro won't paste a copied cell

ESH

New Member
Joined
Feb 4, 2006
Messages
16
The following macro breaks at the line "cell.PasteSpecial Paste:=xlPasteAll". Why?

VBA Code:
Sub PasteCopiedCell()

'Declare variables
Dim cell As range
Dim range As range
Dim targetCell As range

'Prompt user to select a cell
Set cell = Application.InputBox("Select a cell to copy:", "Copy Cell", Type:=8)

'Copy the cell
cell.Copy

'Prompt user to select a range
Set range = Application.InputBox("Select a range to paste the cell into:", "Paste Range", Type:=8)

'Prompt user to select a cell
Set targetCell = Application.InputBox("Select a cell to paste the cell into:", "Paste Cell", Type:=8)

'Loop through the range
For Each cell In range
  'If the cell on the same row in the targetCell column is not empty, then paste the copied cell
  If cell.Offset(0, targetCell.Column - cell.Column).Value <> "" Then
    cell.PasteSpecial Paste:=xlPasteAll
  End If
Next cell

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Is this better?
VBA Code:
Sub PasteCopiedCell()

'Declare variables
Dim cell As range
Dim Tcell As range
Dim Trange As range
Dim targetCell As range

'Prompt user to select a cell
Set cell = Application.InputBox("Select a cell to copy:", "Copy Cell", Type:=8)

'Copy the cell
cell.Copy

'Prompt user to select a range
Set Trange = Application.InputBox("Select a range to paste the cell into:", "Paste Range", Type:=8)

'Prompt user to select a cell
Set targetCell = Application.InputBox("Select a cell to paste the cell into:", "Paste Cell", Type:=8)

'Loop through the range
For Each Tcell In Trange
  'If the cell on the same row in the targetCell column is not empty, then paste the copied cell
  If Tcell.Offset(0, targetCell.Column - cell.Column).Value <> "" Then
    Tcell.Value = cell.Value
  End If
Next Tcell
End Sub
 
Upvote 0
Almost... that pastes the value into the correct cells, but I need it to paste the formula. I was able to fix my original macro by moving "cell.Copy" to just before the FOR loop. It works now, but I don't understand why the original doesn't.
 
Upvote 0

Forum statistics

Threads
1,215,155
Messages
6,123,332
Members
449,098
Latest member
thnirmitha

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