Domanda

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
È stato utile?

Soluzione

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

Altri suggerimenti

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

Application.DisplayAlerts = False
wbk.Close
Application.DisplayAlerts = True
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top