Copy sheet INCLUDING comments - vba

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)

Related

How can I get a button to fully execute a Macro in Excel?

When I execute the following macro without a button, The shapes will delete beautifully. When I execute the macro with a button, the shapes are selected, but they are not deleted. I don't understand why. Is there something wrong with the code? Thanks in advance for your help!
Sub DeleteAllObjects()
Dim ws As Worksheet
Set ws = Worksheets("Flowchart")
ws.Shapes.SelectAll
Selection.Delete
End Sub
I used an iterative approach because my observation was that your method did not work if the ws was not Active at runtime (and to avoid the spaghetti-code of needlessly activating objects).
Your problem is because the Excel Selection only exists relative to the ActiveSheet, so when you do Selection.Delete you're actually deleting something on the ActiveSheet, which isn't the same as the ws!
I have tested this and it works when called from button (on another sheet) or when invoked manually from F5 in the IDE.
Sub DeleteAllObjects()
Dim ws As Worksheet
Dim s As Long
Set ws = Worksheets(3)
For s = ws.Shapes.Count To 1 Step -1
ws.Shapes(s).Delete
Next
End Sub

VBA: Referencing a Worksheet in the Active Workbook

While this seems very basic, I am continually getting an error message while trying to select a cell in a certain sheet on my workbook in my Macro. Does any one know why this will not work? I'm getting error message Run Time Error '1004'.
The sheets name is "Sheet1"and my code is below:
Application.ActiveWorkbook.Worksheets("Sheet1").Range("N2").Select
It's bad practice to use ActiveWorkbook when you don't need to. It's always better to set your workbooks and worksheets to actual variables that you can call on. I think your code is activating another workbook then trying to select a range in a worksheet it can't find.
Sub TryThis()
Dim wbk As Workbook
Dim ws As Worksheet
Set wbk = Workbooks("myWorkbook.xlsm")
Set ws = wbk.Worksheets("Sheet1")
'Now when we say "ws." it is actually "Workbooks("myWorkbook.xlsm").Worksheets("Sheet1")."
'This is okay to start with but it's better to work with the cells directly
ws.Select
Range("N2").Select
Selection = "myText"
'This is much faster and you won't have to worry about what is currently selected
ws.Range("N2") = "myText"
End Sub

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

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?