I have some data and some images on a sheet.
I have some code that copies the data & images from this sheet in one workbook to a sheet in another workbook.
The problem: it seems to be hit or miss if it will bring over the images. Sometimes they copy, sometimes they don't WTF?
wb.Sheets(form).Activate
wb.Sheets(form).Cells.Select
Selection.Copy
objwbk.Activate
ws.Range("A1").Select
ActiveSheet.Paste
Set
Application.CopyObjectsWithCells = True
Before copying
BTW your code
wb.Sheets(form).Activate
wb.Sheets(form).Cells.Select
Selection.Copy
objwbk.Activate
ws.Range("A1").Select
ActiveSheet.Paste
will reduce to:
Application.CopyObjectsWithCells = True
wb.Sheets(form).Cells.Copy ws.Range("A1")
I found the answer. I am creating a new book and pasting to the new book but it's in compatibility modes solve below
'Remember the users setting which currently is 97-2003 file format
SaveFormat = Application.DefaultSaveFormat
'Set it to the 2007-2010 file format xlsm
Application.DefaultSaveFormat = 52
'MAKE NEW BOOK HERE
'Set DefaultSaveFormat back to the users setting
Application.DefaultSaveFormat = SaveFormat
late (ok.. very late ..) answer but i've got the same problem today.
I've try to open the source workbook in read-only mode and it works for for me ™.
So, when you set your objwbk (somethink like set objwbk = open("c:\path\wbk.xls",,true) ) put the third argument to true.
Related
Recently, I encountered a problem which seems to be quite simple to resolve, but I'm new to VBA and due to this I need any advice.
I'm trying to write a macro in VBA which copies a very large sheet (around 140k of lines).
I tried different approaches based on the following topics:
Fastest Method to Copy Large Number of Values in Excel VBA
Large File Size Copy Ranges with VBA
Very large excel file - how to copy data between sheets?
My current solution is:
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Dim wb As Workbook
Set wb = ActiveWorkbook
With Workbooks.Open(FileName, ReadOnly:=True)
.Sheets(1).Visible = -1
.Sheets(1).Copy before:=wb.Sheets(1)
wb.Sheets(1).Name = "Name"
.Close False
End With
When I'm running this macros - Excel is not responding and program is not opening the file. My question is, is it any possibility to copy a large sheet using standard VBA methods? The code sample works absolutely fine with smaller files.
Try. It works fine for me.
Sub rten()
Dim wb As Workbook
Dim docname As String
docname = "test1"
Set wb = ActiveWorkbook
With Workbooks.Open(docname, ReadOnly:=True)
.Sheets(1).Visible = -1
.Sheets(1).Copy before:=wb.Sheets(1)
wb.Sheets(1).Name = "Name"
.Close False
End With
End Sub
It might be because you have a sheet called "Name" that its crashing. Have you checked to see if you already have it there?
I have a Macro that opens a workbook (wb1) and copies it into another workbook (wb2), then closes wb1. However I am always prompted the message below that there is a large amount of clipboard data which I don't want to be prompted. After doing some research I found putting the 'Application.CutCopyMode' set to false (which clears the clipboard) would resolve this issue but it hasn't.
Application.CutCopyMode = False
...
'copy the range from source book
wb1.Worksheets("Sheet1").Range("A1:V2").Copy
'paste the data on the target book
wb2.Worksheets("Sheet1").Range("A1:V2").PasteSpecial
wb.Close savechanges:=False
How can I close the file without this message?
If you want to suppress any messages, add the following before closing the file:
Application.DisplayAlerts = False
If you want to clear the clipboard content, you should add the Application.CutCopyMode = False after performing the Copy/Paste operations. Like this:
...
'copy the range from source book
wb1.Worksheets("Sheet1").Range("A1:V2").Copy
'paste the data on the target book
wb2.Worksheets("Sheet1").Range("A1:V2").PasteSpecial
Application.CutCopyMode = False
wb.Close savechanges:=False
What exactly is the special pasting you want to do? If you just want to copy the values across, then instead of copying & pasting, just assign the values directly:
wb2.Worksheets("Sheet1").Range("A1:V2").value = wb1.Worksheets("Sheet1").Range("A1:V2").value
I'm attempting to copy data from one workbook to another. After some browsing on the internet this is the code i found and it produces a run-time error 1004
Sub Name_Transfer()
Dim wbSource As Workbook
Dim wbDestination As Workbook
'open the source workbook and select the source sheet
Set wbSource = Workbooks.Open( _
Filename:="C:\TestFolder\2013 Cockpit Chart.xls")
'Set the destition workbook variable
Set wbDestination = Workbooks("U:\my documents\ATM Platform 2013\Advanced Team Management.xlsm")
'copy the source range
wbSource.Sheets("Sheet1").Range("A2:B4").Copy
'paste the value at E9
wbDestination.Sheets("DataStore").Range("A4:B6").Value = _
wbSource.Sheets("Sheet1").Range("A2:B4").Value
Application.CutCopyMode = False
ActiveWorkbook.Save
End Sub
What is causing the 1004 error? Can it be fixed? or is there a far better way to be doing this?
Number of rows in source do not match with number of rows in destination.
Try this
wbDestination.Sheets("Delivery").Range("A4:B6").PasteSpecial (xlPasteValues)
or
wbDestination.Sheets("DataStore").Range("A4:B6").Value = _
wbSource.Sheets("Sheet1").Range("A2:B4").Value
Also specify the path for
Set wbDestination = Workbooks("Advanced Team Management.xlsm")
like you did for wbSource in case Advanced Team Management.xlsm is closed.
forgot to mention this was being done in Excel 2007...
The reason behind the 1004 error was it was looking for a .xls file not a xlsx file
so the code should of looked like this
'open the source workbook and select the source sheet
Set wbSource = Workbooks.Open( _
Filename:="C:\TestFolder\2013 Cockpit Chart.xlsx")
To close the file i just used
ActiveWorkbook.Close
Or as Siddharth so rightly said to use:
wbSource.close savechanges:= false
Thanks for your help contributors... I probably would of never seen this without all your help.
I need to open and close a file using a macro, but I don't want to save it. I can get to wear excel prompts you to Save or Don't save, what is the VBA command for dont save. This is what I am using I just need it to not save and close excel all the way.
Sheets("Sheet1").Select
Range("A1").Select
Sheets("Sheet6").Select
Range("A1").Select
Workbooks.Open Filename:= _
"X:\File.xlsx"
Workbooks.Close
Place False in first argument after Close method to tell VBA to not save the Workbook changes. So:
Workbooks.Close False
or
Workbooks.Close SaveChanges:=False
If I understand well, try to use Application.DisplayAlerts:
Application.DisplayAlerts=False
Workbooks.Close
Application.DisplayAlerts=True
You can use the Saved property of the Workbook object for this. Setting this property to True will stop the prompt from appearing (but won't actually save the workbook):
Dim wb as workbook
Set wb = Workbooks.Open("X:\File.xlsx")
' do stuff here
wb.Saved = True
wb.Close
See http://msdn.microsoft.com/en-us/library/ff196613.aspx for reference
Despite many posts I have looked through being of along the same lines as my question, none of the answers satisfy what I am looking for. If you can link me to one I'd gladly read it.
I have a workbook with worksheets. For simplicity, let's say my workbook has a worksheet. And in my worksheet which is called "Sheet1", there is data in cells A1 to A4.
What I want my VBA code to do is:
Copy row 1 (or specifically cells A1 to A4) of Workbook 'A' into Range variable 'myRange'
Create a new workbook, let's call this one Workbook 'B'
Give Workbook 'B's default "sheet1" a new name to "Test Name"
Open Workbook 'B' (though I realise that VBA code "Workbooks.Add" opens a new book so this step may be redundant since Workbooks.Add covers half of point 2 and 3)
Paste 'myRange' into first row of 'Workbook B'
Save 'Workbook B' with name "Test Book" and a timestamp enclosed in square brackets. The file must also be of the file extension "xls"
Close 'Workbook B' and return to 'Workbook A'
What I have so far is this:
Sub OpenAndSaveNewBook()
'Declarations
Dim MyBook As String
Dim MyRange As Range
Dim newBook As Workbook
'Get name of current wb
MyBook = ThisWorkbook.Name
Set MyRange = MyBook.Sheets("Sheet1").Range("A1,F1")
'Create/Open new wb
newBook = Workbooks.Add
'Save new wb with XLS extension
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "/" & "TEST-BOOK", _
FileFormat:=xlNormal, CreateBackup:=False
'===NOTE: BEFORE THE FOLLOWING RUNS I NEED TO PERFORM ACTIONS ON CELLS VIA VBA ON
'===WORKBOOK 'A'. DOES THE NEWLY CREATE WORKBOOK BECOME THE PRIMARY/ACTIVE WORKBOOK
'===? AND SO THEN DO I NEED TO ACTIVATE WORKBOOK 'A'?
ActiveWorkbook.Close savechanges:=True
'Return focus to workbook 'a'
MyBook.Activate
End Sub
As you can see, I am lacking the code that will handle:
the pasting of my copied data to the new workbook
the changing of the new workbook's sheet1 name to something else
adding a timestamp to the filename string on save
Lastly, I have included a question in my code as I think I may have a misunderstanding of the ActiveWorkbook method. AFAIK when the code "Workbooks.Add" runs this becomes the Active Workbook, i.e. one with the focus. Does this effect how the VBA code running on Workbook 'A'? Does this mean that if I wanted to add code to manipulate cells of Workbook 'A' then I would need to use "MyBook.Activate" where 'MyBook' holds the string of Workbook 'A's actual title?
Any help will be greatly appreciated.
Thanks,
QF
Instead of copy pasting the way you mentioned above, you can directly do this. This will also negate the use of a variable.
MyBook.Sheets("Sheet1").Rows("1:4").copy _
newBook.Sheets("Sheet1").Rows("1")
EDIT
I just noticed an error with your code.
newBook = Workbooks.Add
This line will give you an error as you have to use Set
Your code can be written as
Option Explicit
Sub OpenAndSaveNewBook()
Dim MyBook As Workbook, newBook As Workbook
Dim FileNm As String
Set MyBook = ThisWorkbook
FileNm = ThisWorkbook.Path & "\" & "TEST-BOOK.xls"
Set newBook = Workbooks.Add
With newBook
MyBook.Sheets("Sheet1").Rows("1:4").Copy .Sheets("Sheet1").Rows("1")
'Save new wb with XLS extension
.SaveAs Filename:=FileNm, FileFormat:=xlNormal, CreateBackup:=False
.Close Savechanges:=False
End With
End Sub
MORE EDIT
Elaborating on the use of SET
I would recommend you to see this post.
LINK: Worksheets does not work
Avoid references to ActiveWorkbook in favour of explicit references wherever possible.
As you've found, it can be confusing knowing what's currently active, and you do not need to activate a workbook to manipulate it.
So you should be using
newBook.SaveAs...
newBook.Close...
Recorded macros tend to activate workbooks in order to work on them, but that's because that's the way a human who recorded them works! All activation really does is change focus.
The same applies to making selections and then manipulating the current selection; it's not necessary in VBA and tends to be slower than direct manipulation.
The awesome thing about Excel is the 'Record Macro' function. I started recording a macro and just followed the steps you outlined, then made a few minor modifications to the code that Excel provided as the recorded macro:
Range("A1:F1").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Sheets("Sheet1").Name = "Test Name"
Application.CutCopyMode = False
myNewFileName = myPath & myTestName & "_" & Date & ".xls"
ActiveWorkbook.SaveAs Filename:=myNewFileName _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
The Date function returns the current system date. It is important to note that the square brackets that you wanted are not valid filename characters; Excel will throw an error if you try to use those.
Turn the macro-recorders on; carefully execute the steps you want; stop the recorder; "edit" the macro generated. Fix as you need to make the program you intend, e.g., to parameterize it.