Copy Paste Across Worksheets (VBA) - 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

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)

Call "ThisWorkbook"

I am trying to switch between a template (hard coded) and a dynamic report which changes name weekly (ThisWorkbook). I am struggling with calling the variable x to bring focus to the workbook. I am copying the template formulas and pasting them into the dynamic report.
Sub wkbk()
Dim x As Excel.Workbook
Set x = ThisWorkbook
Dim pth As String
pth = x.FullName
Windows(pth).Activate
End Sub
Here is the VBA code I am using:
Windows("BBU_CMD_TEMPLATE.xlsx").Activate
Cells.Select
Selection.Copy
Windows(pth).Activate
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
Why not just use ThisWorkbook.Activate? There's generally no need to assign a variable to represent a built-in like ThisWorkbook so the rest of those variables are unnecessary unless you're using them elsewhere in that procedure (from the snippet provided, you aren't, so you don't need them).
Sub wkbk()
ThisWorkbook.Activate
End Sub
However, what's the point of wkbk procedure? If solely to activate the workbook, that's not needed either and there are plenty of reasons to avoid Activate.
Sub CopySheetFromTemplateToThisWorkbook()
Dim tmplt As Workbook
On Error Resume Next
Set tmplt = Workbooks("BBU_CMD_TEMPLATE.xlsx")
If tmplt Is Nothing Then
MsgBox "Template file needs to be open..."
Exit Sub
End If
On Error GoTo 0
With ThisWorkbook
tmplt.ActiveSheet.Copy After:=.Sheets(.Sheets.Count)
End With
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.

Simple Excel VBA – copy pasting to another workbook

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

I need to insert tab name in cell A1 of every tab with changing tab names

I need to open a worksheet with a fixed name and insert the name of each tab (which will change according to the current date) at the top of the sheet.
I modified some code from a previous answer and had the code working when it did not include the code to open the workbook. Now it flicks through the tabs but doesn't insert the name into Cells(1, 1) and I have no idea why. It also bugs at the end: Run-time error 91, which is less problematic but would be good to fix.
Any tips or advice much appreciated. Below is my current code:
Sub PSOPENTAB()
ChDir "G:\directory"
Workbooks.Open Filename:="G:\directory\filename.xls"
Windows("filename.xls").Activate
ActiveWorkbook.Worksheets(1).Activate
Call nametop
End Sub
Sub nametop()
Dim i As Long
With ThisWorkbook
'exit if Activesheet is the last tab
If .ActiveSheet.Index + 1 > .Worksheets.Count Then
Exit Sub
End If
For i = .ActiveSheet.Index To .Worksheets.Count - 1
.ActiveSheet.Cells(1, 1) = .Worksheets(i).Name
ActiveSheet.Next.Select
Next i
End With
End Sub
You need to reference your objects correctly.
Your problems are:
You use Thisworkbook in your nametop routine. So it will always work on the workbook containing the code.
You can change it to ActiveWorkbook but that may lead you to other problems in the future. See this cool stuff to know more about why to avoid Activeworkbook/Activesheet and the like
Applying what's discussed there, try below code:
Sub PSOPENTAB()
Dim wb As Workbook
Set wb = Workbooks.Open(Filename:="G:\directory\filename.xls")
nametop wb
End Sub
Sub nametop(wb As Workbook)
Dim ws As Worksheet
For Each ws In wb.Worksheets
ws.Cells(1, 1) = ws.Name
Next ws
End Sub
Above code adds the name of the sheet in Cell A1 of every sheet.
Is this what you're trying?