Simple Excel VBA – copy pasting to another workbook - vba

I've looked at couple of codes on copy pasting from one excel workbook to another. For some reason, I am getting errors even when I am directly copying the format. My code is only ~12 lines so far, so if you could look and see what the issue is, that would be really great help to me. Thank you very much!
Sub UpdateActualWorkbook()
Sheets("Input").Select
Range("BE9").Select
Selection.Copy
Dim Display As String
Display = Cells(1, 2).Value
If Display = "Yes" Then
Dim wb As Workbook
Set wb = Workbooks.Open("Book1")
wb.Sheets("Sheet1").Range("A1").PasteSpecial
End If
End Sub
Currently I am getting an error message on wb.Sheets("Sheet1").Range("A1").PasteSpecial
^this line. This does not change whether I make it .Paste or .Pastespecial.
I would be really grateful if anyone could help me. Thank you so much!

If:
Set wb = Workbooks.Open("Book1")
fails, then wb will be Nothing and the PasteSpecial line will raise an error. If you simply want to add a new workbook, then use:
Set wb = Workbooks.Add
and if you want to open an existing workbook, then give the full filespec.

I know it late but try this...
Sub tstcpy()
If Cells(1, 2).Value = “yes” Then
Workbooks("Book1a.xlsm").Sheets("Sheet1").Cells(9, 2).Copy _
Destination:=Workbooks("Book2.xlsx").Sheets("Sheet1").Cells(1, 1)
End If
End Sub

Related

Copy sheet INCLUDING comments

I am looking to copy a sheet from one workbook to another workbook exactly as it is, including comments. Thus far, I have not found a simple way to do this.
This is the code which works perfectly well for copying and pasting the contents of a sheet to a workbook without comments:
Sub copyOrRefreshSheet(destWb As Workbook, sourceWs As Worksheet)
Dim ws As Worksheet
On Error Resume Next
Set ws = destWb.Worksheets(sourceWs.Name)
On Error GoTo 0
If ws Is Nothing Then
sourceWs.Copy After:=destWb.Worksheets(destWb.Worksheets.Count)
Else
ws.Unprotect Password:="abc123"
ws.Cells.ClearContents
ws.Range(sourceWs.UsedRange.Address).Value = sourceWs.UsedRange.Value2
End If
End Sub
I am sure it will take roughly one line of code to fix this problem, I just do not know how. Thank you in advance.
Try change:
ws.Range(sourceWs.UsedRange.Address).Value = sourceWs.UsedRange.Value2
To:
sourceWs.UsedRange.Copy
ws.Range(sourceWs.UsedRange.Address).PasteSpecial(xlPasteAll)

VBA change value in another workbook

I need some help changing variables in another workbook.
First I open the workbook with Workbooks.Open ("test.xlsx")
When I try to change a cell value with Workbooks("test.xlsx").Worksheets("Sheet").Cells(1, 1).Value = VariableX it gives me error 9: subscript out of range. I don't see why it won't work. Can anyone help me out on this?
Workbooks.open Returns a Workbook-object. Use this to reference the Workbook you want to manipulate:
dim wb as Workbook
set wb = Workbooks.Open("test.xlsx")
wb.Worksheets("Sheet").Cells(1,1).Value = variableX
' Close the workbook afterwards and save the changes
wb.Close True
Once you have Opened the workbook, it is Active. Here is a small working example:
Sub Macro2()
Dim VariableX As Long
VariableX = 123
Workbooks.Open Filename:="C:\TestFolder\Book1.xlsx"
Worksheets("Sheet1").Cells(1, 1).Value = VariableX
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub

Not copying the values properly

I have a problem with my macro to copy paste only the values of the range A6:AM46,A52:AM84 to AN6 location on the same sheet.
Sub PréparerGrilles()
Range("A6:AM46,A52:AM84").Select
Selection.Copy
Range("AN6").Select
Application.CutCopyMode = False
ActiveSheet.Paste
End Sub
I get the 1004 error (multiple selection error)
Could you help me with this ?
To copy values from A6:AM46,A52:AM84 to AN6:BZ46,AN52:BZ84 you can do the following:
Sub PreparerGrilles()
Range("AN6:BZ46").Value = Range("A6:AM46").Value
Range("AN52:BZ84").Value = Range("A52:AM84").Value
End Sub
Version using the Range.Copy method:
Sub PreparerGrilles()
Range("A6:AM46").Copy Destination:=Range("AN6:BZ46")
Range("A52:AM84").Copy Destination:=Range("AN52:BZ84")
Range("AN6:BZ46").Value = Range("AN6:BZ46").Value
Range("AN52:BZ84").Value = Range("AN52:BZ84").Value
End Sub
I recommend that you don't slow your code down by using this. It will also lead to potentially incorrect values if your formulae refer to anything that wasn't part of the copy.
Version using a PasteSpecial xlPasteValues method:
Sub PreparerGrilles()
Range("A6:AM46").Copy
Range("AN6:BZ46").PasteSpecial xlPasteValues
Range("A52:AM84").Copy
Range("AN52:BZ84").PasteSpecial xlPasteValues
End Sub
I strongly recommend against using this method, as it leads to too many "unreproducible" errors due to the users copying things via the clipboard between when your code does the Copy and when it does the Paste, and also because of the fact that your Copy zaps whatever the user might have manually pasted to the clipboard.
Application.CutCopyMode = False cleans the clipboard....
Better code for that is:
Sub Test()
Dim wb As Workbook, ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("SOMMAIRE") 'this means it will only work on this sheet, you should change the name of the sheet or ask me what do you want in order to get it working on other sheets
ws.Range("A6:AL46").Copy
ws.Range("AN6").PasteSpecial xlValues
ws.Range("A52:AM84").Copy
ws.Range("AN52").PasteSpecial xlValues
End Sub
Edited: Now that should do the trick. Try it out and tell me if it works
Edited2: This is what you want, at least for what you asked so far.

Copy Paste Across Worksheets (VBA)

I don't know why, I just can't get this to work. I've simplified it right down to just three lines - but it's causing me problems still.
Basically I want to open a workbook and copy some data from it into a master workbook.
I have:
Sub copypaste()
Workbooks.Open("...Test.xlsx").Sheets("Sheet1").Cells(1, 1).Copy
ActiveWorkbook.Close
Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
End Sub
I've seen runtime error 438 (object does not support this property method), I can get paste that but just hit 1004 application defined error or object defined error.
I honestly have no idea where I'm going wrong on this simple task!
Thank you in advance,
Tom
Try closing the workbook after pasting the data.
As an example you can use something like:
Sub copypaste()
Dim WBopen As Workbook, Wb As Workbook
Set Wb = ActiveWorkbook
Set WBopen = Workbooks.Open("...Test.xlsx")
WBopen.Sheets("Sheet1").Cells(1, 1).Copy
Wb.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
WBopen.Close
End Sub
Because you are closing the Workbook before the data is pasted it fails.
It is also preferred to not use .Copy and .Paste when it can be avoided.
See example below for a direct setting of the Values:
Sub copypaste()
Dim wbMaster As Workbook, wbData As Workbook
Set wbMaster = Workbooks("Master.xlsm")
Set wbData = Workbooks.Open("Data.xlsx")
wbMaster.Sheets("Sheet1").Range("A1").Value = wbData.Sheets("Sheet1").Range("A1").Value
wbData.Close False
End Sub

Copy Data from one worksheet on another

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.