Getting blank sheet when copying to new workbook

draziw

New Member
Joined
May 26, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have some code to massage excel workbook in an access VBA script.
I copied some code from this site re-copying sheet to new workbook which I have been attempting to modify.

At this stage I have two problems.

1(. The new workbook has no data.
2(. I get a pop up message saying that I have a large amount of data in the clipboard, (I thought this code disabled that)
VBA Code:
 Dim excelapp As Object, wb As Object, ws As Object, ws2 As Object
 
 excelapp.CutCopyMode = False

Here is the code of the subroutine.

Code:
Sub MySheetCopy(SBName)

    Dim mySourceWB As Workbook
    Dim mySourceSheet As Worksheet
    Dim myDestWB As Workbook
    Dim myNewFileName As String
      Dim stPathName As String
      Dim excelapp As Object, wb As Object, ws As Object, ws2 As Object
   Set excelapp = CreateObject("excel.application")
    
    '   First capture current workbook and worksheet
    Set mySourceWB = ActiveWorkbook
    Set mySourceSheet = ActiveSheet
   stPathName = "C:\RSL LogBookMgmt\LogBook files\Reports\"

'   Build new file name based
    myNewFileName = stPathName & SBName & " Monthly Charitable Activities Form.xlsx"
 
   Workbooks.Add
    ActiveWorkbook.SaveAs Filename:=myNewFileName
    Set myDestWB = ActiveWorkbook

'   Copy over sheet from previous file
    mySourceWB.Activate
    Cells.Copy
    myDestWB.Activate
    Range("A1").Select
    'ActiveSheet.Paste
    With Workbooks.Add.Sheets(1).Range("A1")
   .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
  
   End With
   excelapp.CutCopyMode = False


   mySourceWB.Close savechanges:=True
   myDestWB.Close savechanges:=True
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Couple things for you at a quick glance.

VBA Code:
myNewFileName = stPathName & SBName & " Monthly Charitable Activities Form.xlsx"

What is SBName supposed to be? Right now it is nothing apparently.



Change:
VBA Code:
excelapp.CutCopyMode = False

To:
VBA Code:
Application.CutCopyMode = False



Delete the following:
VBA Code:
      Dim excelapp As Object, wb As Object, ws As Object, ws2 As Object
   Set excelapp = CreateObject("excel.application")
 
Upvote 0
Couple things for you at a quick glance.

VBA Code:
myNewFileName = stPathName & SBName & " Monthly Charitable Activities Form.xlsx"

What is SBName supposed to be? Right now it is nothing apparently.



Change:
VBA Code:
excelapp.CutCopyMode = False

To:
VBA Code:
Application.CutCopyMode = False



Delete the following:
VBA Code:
      Dim excelapp As Object, wb As Object, ws As Object, ws2 As Object
   Set excelapp = CreateObject("excel.application")
Thank you for your response.
SBName is passed to the routine from the calling section, there is nothing wrong with the file name where this used.
My problem with the .xlsx file is that the spreadsheet (sheet 1) contains no data or formatting.

your suggested changed would return the code to original which produces the following error.
1622079499684.png

I had found the modification of the code that I had made on line that corrected this error for Access VBA.

The workbook is created! I was thinking this is something to do with copying the values and formats portion of the code or maybe saving after copying.
 
Upvote 0
VBA Code:
myNewFileName = stPathName & SBName & " Monthly Charitable Activities Form.xlsx"
should be
VBA Code:
myNewFileName = stPathName & "Monthly Charitable Activities Form.xlsx"
AND try
VBA Code:
mySourceWB.Activate
    mySourceSheet.usedrange.Copy myDestWB.Range("A1")
 
Upvote 0
VBA Code:
myNewFileName = stPathName & SBName & " Monthly Charitable Activities Form.xlsx"
should be
VBA Code:
myNewFileName = stPathName & "Monthly Charitable Activities Form.xlsx"
AND try
VBA Code:
mySourceWB.Activate
    mySourceSheet.usedrange.Copy myDestWB.Range("A1")
Michael thank you for your response.
SBName stands for sub branch name and is prepended to the source file name to make a new dest file name, from what I can see this achieves what I require. Surely the source and destination file names need to be different?
your suggested change
VBA Code:
mySourceWB.Activate
    mySourceSheet.usedrange.Copy myDestWB.Range("A1")
produces the following error.
Screenshot 2021-05-27 160855.png
 
Upvote 0
As my spreadsheet is relatively small I decided to specify a cell range.

here is that portion of code modified.
VBA Code:
    mySourceWB.Activate
    Cells.Copy
    myDestWB.Activate
    Range("A1:J73").Select
    'ActiveSheet.Paste
    With Workbooks.Add.Sheets(1).Range("A1:J73")
   .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
   
   End With
   excelapp.CutCopyMode = False
'   Application.CutCopyMode = False

I still get the 2 problems as stated in my original post!
 
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

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