Issue with PasteSpecial after copying from refreshed SQL data source

tianimo

New Member
Joined
Sep 29, 2006
Messages
9
Hi,

I've got an issue with copying and pasting values from an SQL data source. I'm using Excel 2007.

I have set up an SQL data connection which calls a Stored Procedure in a database, retrieves a couple of values, and displays them in a sheet in the workbook.

I have a sub which can pass a user-defined parameter to the connection and update the call to the SP, so the right data is retrieved based on the user-defined input. This works fine and has been tested by repeatedly feeding it different inputs one after the other.

I am now incorporating this in to a routine where I refresh the SP connection based on the user input, retrieve the values, and then copy and paste special the values in to a different sheet where I will manipulate them.

This process works fine the first time; however, if I change the variable and run the process again, even though I can see (by stepping through the code line by line) that the SP is refreshing and retrieving the new values, when I perform the (what follows is just illustrative)

Range("A1:B1").Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


it is pasting the previous details that were retrieved, and not the new ones!

I thought it might be that I should clear the clipboard, but having added " Application.CutCopyMode = False " in to the code doesn't help. Sometimes I find if I save the workbook and then rerun the procedure it's fine, but not always - anyway, forcing a save just to get round the issue seems like a terible hack rather than understanding the problem and resolving it!

Any help would be really appreciated. Thanks.
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I've just done more testing. If I put a break in the code at the point when I am copying and pasting the values and step through the procedure, it works every time. If I remove the breaks and let it run from start to finish, it shows the odd behaviour described above.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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