Unable to paste data in Do While Loop using pastevalue function

amardeep_s

New Member
Joined
Aug 6, 2015
Messages
7
hi All,

i am new to this forum and newbie to the Excel VBA. I am trying to complie simple loop VBA code to copy paste data until it finds empty cell. Code function properly however i want data to be pasted in Value format. Below is the code. Appreciate if anyone could help me with this.

Sub Macro_Loop()
Range("A2").Select
Do While ActiveCell.Value <> Empty
Range("A2", Range("B2").End(xlDown)).Copy Range("K2:L2")
ActiveCell.Offset(1, 0).Select
Loop

End Sub

Code that gives me error when i try to use paste value function:

Sub Macro_Loop()

Range("A2").Select
Do While ActiveCell.Value <> Empty
Range("A2", Range("B2").End(xlDown)).Select
Selection.Copy
Range("K2").Select
Selection.PasteSpecials :=xlPasteValues
ActiveCell.Offset(1, 0).Select
Loop

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi,

Your error comes from the fact that when you paste, you paste more than one cell. So if next you ask him to get back to active cell, it does not work.

Your code seems strange to me as you will always copy-paste the same things at the the place whatever the stage of the loop. None the less you should not have an error using following :
Code:
[COLOR=#000000][FONT=Calibri]Sub Macro_Loop()
[/FONT][/COLOR][COLOR=#000000][FONT=Calibri]Dim Rng as Range
Dim i as Long
i=2
Set Rng=Cells(i,1)
[/FONT][/COLOR][COLOR=#000000][FONT=Calibri]Rng.Select
[/FONT][/COLOR][COLOR=#000000][FONT=Calibri]Do While Rng.Value <> Empty
[/FONT][/COLOR][COLOR=#000000][FONT=Calibri]Range("A2", Range("B2").End(xlDown)).Select
Selection.Copy
Range("K2").Select
Selection.PasteSpecials :=xlPasteValues[/FONT][/COLOR][COLOR=#000000][FONT=Calibri]
i=i+1
[/FONT][/COLOR][COLOR=#000000][FONT=Calibri]Loop
[/FONT][/COLOR][COLOR=#000000][FONT=Calibri]
[/FONT][/COLOR][COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
Hi,

thanks for your reply. It does make sense of the error now. However, when i run your code it throws me syntax error on the pastevalue line.

Selection.PasteSpecials :=xlPasteValues

i tried and changed it to below but still it gives me error "Object doesnt support this method or property"

Selection.PasteSpecials Paste:=xlPasteValues

Any idea whats going wrong here??
 
Upvote 0
PasteSpecial not PasteSpecials


Code:
      Range("A2", Range("B2").End(xlDown)).Copy
        Range("K2").PasteSpecial xlPasteValues

but other parts of your code don't make much sense
 
Last edited:
Upvote 0
or....
Code:
With Range("A2", Range("B2").End(xlDown))
  Range("K2:L2").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
 
Upvote 0
thanks, i am able to achieve what i intended to with the help of With-EndWith functionality. No need to loop using Do-While which doesnt make sense here. Appreciate for your help and support :)
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,790
Members
449,468
Latest member
AGreen17

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