Paste Excel Range into Powerpoint as Table - vba

I am trying to write get write a macro in excel to copy a range of cells from excel to powerpoint. However, I can't seem to figure out the line to paste the range into powerpoint as a table (like right clicking and hitting 'paste') in powerpoint.
I tried Activesheet.shapes.paste but that gave me an error about data type
I also tried Activesheet.shapes.pastespecial, but none of the options are for pasting as a table as far as I can tell. I do not want to paste as a picture or anything like that.
Any ideas?

James - check this out: http://www.thespreadsheetguru.com/blog/2014/3/17/copy-paste-an-excel-range-into-powerpoint-with-vba
It looks like the relevant code snippet from this post is the following:
'Copy Range from Excel
Set rng = ThisWorkbook.ActiveSheet.Range("A1:D12")
'Copy Excel Range
rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)

Related

Copy Range of cells to Visio (2013) using VBA code

I have an Excel range of cells (let's call it "Table A") and I would like to copy that Range/Table into Visio using VBA.
Without VBA - I can copy the range in Excel, go to Visio - Insert - Chart - go to Sheet1 tab - Paste
Using VBA - I am a novice so this is what I think it should look something like:
Set AppVisio = CreateObject("visio.Application")
AppVisio.Visible = True
AppVisio.Documents.AddEx "basicd_u.vst", 0, 0
Worksheets("Sheet1").Range("TABLEA").Copy _
AppVisio.ActiveWindow.Page.Add.Paste

Select data range for a chart from different sheet using VBA (EXCEL)

How can I select data range for a chart from different sheet using VBA? Suppose that data sheet name is data_sheet, chart sheet name is chart_sheet, and my data range is A1:A20. How can I do this in excel? I checked THIS but didn't work for different sheets. Otherwise, I checked THIS but returned this error: Subscript out of range:
With Worksheets("chart_sheet")
ActiveChart.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
End With
Assuming "chart_sheet" is the name of your Chart and "data_sheet" is the name of your Worksheet, I think you want to do the following:
Charts("chart_sheet").SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
Your With block was not doing anything useful - the purpose of a With block is to allow you to just type . as a shortcut for something like Worksheets("data_sheet")..
So something like:
With Sheets("chart_sheet")
.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
End With
would work, because the .SetSourceData is an abbreviation of Sheets("chart_sheet").SetSourceData. (Notice also that the Sheets collection contains both Worksheets and Charts objects, so Charts("chart_sheet") and Sheets("chart_sheet") both point to the same thing.)
ActiveChart refers to the currently active chart, just as ActiveSheet returns to the currently sheet. If you don't have a chart active when that piece of code executes, you will get an error.
So the following piece of code would also probably have worked for you:
Sheets("chart_sheet").Activate
ActiveChart.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
as chart_sheet is probably not a worksheet, did you try this ?
with sheets("chart_sheet")

Excel IF cell reference formula being changed when recorded in VBA

I have a macro that works fine. I'm adding a formula to it that describes the status of the workbook for someone else to review. When I record the macro of implementing the formula into the sheet, VBA records it using relative references even though I do not have the relative references button selected. The relative references do not point correctly so I need to fix it. I checked this post (adding a dynamic cell reference in vba) and am now thinking that I should adjust the formula with some VBA reference code but I'm not sure if what the post is using is suitable for me. Am I going in the right direction?
Excel Formula:
=IF(Selections!K2="","Not prepped","Prepped")
When recorded into VBA:
ActiveCell.FormulaR1C1 = _
"=IF(Selections!R[-41]C[-1]="""",""Not prepped"",""Prepped"")"
What I need in VBA code:
ActiveCell.FormulaR1C1 = _
=IF(Selections!K2="""",""Not prepped"",""Prepped"")
ok, then is this your answer:
dim rng as range
set rng = thisworkbook.sheets("Sheet1").range("L5")
rng.Formula = "=IF(Selections!K2="""",""Not prepped"",""Prepped"")"

Excel VBA add hyperlink to shape to link to another sheet

I have a macro that creates a summary sheet at the front of a Workbook. Shapes are created and labeled after the sheets in the workbook and then hyperlinks are added to the shapes to redirect to those sheets, however, when I recorded the macro to do this, the code generated was:
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 1")).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection.ShapeRange.Item(1), Address:=""
The hyperlinks that were manually created in excel while recording the macro work just fine and when hovering over them, display the file path and " - Sheet!A1" but they don't seem to actually be adding the link location into the address portion of the macro. Does anyone know the code that should go in that address section to link to the sheet?
The macro recorder doesn't record what is actually happening in this case. The property you are looking for is SubAddress. Address is correctly set in your code.
Create a hyperlink from a shape without selecting it
You want to avoid selecting things in your code if possible, and in this case it definitely is. Create a shape variable and set it to the shape you want to modify, then add the hyperlink to the sheet the shape is on. Note that you can also set the text for the screen tip.
In the example below, the shape I want to modify is on Sheet 6, and hyperlinks to a range on Sheet 4.
Sub SetHyperlinkOnShape()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet6")
Dim hyperLinkedShape As Shape
Set hyperLinkedShape = ws.Shapes("Rectangle 1")
ws.Hyperlinks.Add Anchor:=hyperLinkedShape, Address:="", _
SubAddress:="Sheet4!C4:C8", ScreenTip:="yadda yadda"
End Sub

Saving/restoring clipboard in Excel VBA

I have an Excel workbook with many sheets. Most sheets have some code in the Worksheet_Activate() subroutine.For some reason this code causes the contents of the clipboard to be lost - extremely annoying, as spreadsheet user cannot copy and paste between sheets.
In an attempt to fix this I have added the following lines at the beginning of the Worksheet_Activate code:
Dim dClipBoard As MsForms.DataObject
On Error Resume Next
Set dClipBoard = New MsForms.DataObject
dClipBoard.GetFromClipboard
And the following lines just before exit of the Worksheet_Activate code:
On Error Resume Next
dClipBoard.PutInClipboard
Unfortunately, it doesn't work. My clipboard is still lost when I move from sheet to sheet.
I can imagine 2 different workarounds for this case.
1- If possible change the code in the worksheets. Where ever the .copy command is used replace it with codes like these:
range("Some Range") = range("Some other range")
2- If changing the code is not possible (using the approach you are currently using) instead of storing the data to an MsForms.DataObject you could copy them to some auxiliary sheet, And retrieve the data from the worksheet before the exit worksheet event triggers.