Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable the clipboard prompt in excel vba when I close the workbook?

So I'm working with multiple workbooks from which I copy all the data from Sheet1 from each one into their respective sheet on the master workbook. After I do that, I have the multiple workbooks encoded to close. However, an annoying prompt asking if I want to keep the copied data on clipboard consistently pops up and I want to either have it not pop up or when it does, I want "No" to be automatically chosen.

I know there's a similar question that's been asked but it hasn't worked for me and I'm thinking it's because I have a rectangular area of data instead of just a column? I really new at vba but I tried messing around with the code in Disable clipboard prompt in Excel VBA on workbook close but I've had no luck.

Here's my original code:

Sub CFM56copydata()
Dim wbk As Workbook
'The workbook is opened using the text from a textbox in a userform i.e:
strFirstFile = Userform1.dog.Text
Set wbk = Workbooks.Open(strFirstFile)
With wbk.Sheets("Sheet1")
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
End With
Set wbk2 = ThisWorkbook
wbk2.Sheets("dog").Range("A1").Insert
wbk.Close
End Sub

and here's how I tried to tweak it so I avoided using the clipbaord at all. (Didn't work, gives me a debug error on line 12)

Sub fix()
Dim wbk As Workbook
strFirstFile = Userform1.CFM56path.Text
Set wbk = Workbooks.Open(strFirstFile)
Set wbk2 = ThisWorkbook
Dim rSrc As Range
Dim rDst As Range
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Set rSrc = Selection
Set rDst = wbk2.Sheets("dog").Cells("A1").Resize(rSrc.Rows.Count, rSrc.Columns.Count)
rDst = rSrc
wbk.Close
End Sub
like image 716
SRoy Avatar asked Nov 15 '25 00:11

SRoy


2 Answers

If the clipboard prompt is the problem, just empty it after your done pasting.

wbk2.Sheets("dog").Range("A1").Insert
Application.CutCopyMode = False
wbk.Close
like image 143
ron Avatar answered Nov 17 '25 18:11

ron


It should work if you change your wbk.Close statement to:

Application.DisplayAlerts = False
wbk.Close
Application.DisplayAlerts = True
like image 42
Netloh Avatar answered Nov 17 '25 20:11

Netloh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!