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

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

Related

VBA - Copying cells across Workbooks

I'm writing a VBA program that changes the visuals of an excel database. At the end, I need to add a "header" (5 rows of text) at the top. The problem is, I cannot insert the text with VBA, since it contains letters (for ex. á, é...) that aren't compatible with VBA, I need to insert that text from another excel file.
The macro I have is stored in a standalone excel workbook, that also contains the cells of the header I need to copy into my database. The problem is, the name of the excel files I am working with varies. Is there a way I could switch between those 2 files and copy cells across them. Can I store the name of the excel file I am working with and later use it in the VBA code to switch between the workbooks?
Not sure if this 100% answers your question but hope it helps, you can open and store both workbooks as objects using:
Dim wb as Workbook, wb2 as Workbook
Set wb = Workbooks.Open("C:\User\Sample_Workbook_File_Path_1.xlsx")
Set wb2 = Workbooks.Open("C:\User\Sample_Workbook_File_Path_2.xlsx")
From there you can call values from either workbook using things like:
'to get the second workbooks excel file name into a worksheet: "Sample_Workbook_2"
wb.Worksheets("Sample_Worksheet").Range("A1").Value = wb2.Name
'to copy files
wb2.Worksheets("Second_Workbooks_Worksheet").Range("A2:A100").Copy _
wb.Worksheets("Sample_Worksheet").Range("A2")
'Alternatively you can store the entire workbooks path name instead of the file name using:
wb.Worksheets("Sample_Worksheet").Range("A1").Value = wb2.Path

Copy Word table to Excel without splitting cells

I'm struggling to paste a word table into excel without the deliminator splitting out by paragraph, as I want to grabs those paragraphs and paste into excel later.
Does anyone know how to prevent excel from splitting paragraphed text when it is pasted into excel from a word table?
Sub InputDoc()
Dim Oo As OLEObject
Dim wDoc As Object 'Word.Document
'Search for the embedded Word document
For Each Oo In Worksheets("Input").OLEObjects
If InStr(1, Oo.progID, "Word.Document", vbTextCompare) > 0 Then
'Open the embedded document
Oo.Verb xlVerbPrimary
'Get the document inside
Set wDoc = Oo.Object
'Copy the contents to cell A1
wDoc.Content.Copy
Worksheets("Paste").Range("A1").PasteSpecial Paste:=xlPasteFormats
The text you're copying has CRLF in the string and you want to avoid Excel interpreting that as multiple cells.
Just avoid using Paste and use Range.Value instead. Problem solved.
'Search for the embedded Word document
For Each Oo In Worksheets("Input").OLEObjects
If InStr(1, Oo.progID, "Word.Document", vbTextCompare) > 0 Then
'Open the embedded document
Oo.Verb xlVerbPrimary
'Get the document inside
Set wDoc = Oo.Object
'Copy the contents to cell A1
Worksheets("Paste").Range("A1").value = wDoc.Content
This will put the entire wDoc.content into one cell. That's not what you really want though. You really need to loop through the table in Word and use .Value to insert the data into Excel.

Referencing Excel Sheet placed in PowerPoint

I have added an Excel sheet into a PowerPoint 2010 document using the Insert tab --> Object --> Microsoft Excel 97-2003 Worksheet (Create New) option. I want to reference some of the cells in the Excel sheet in another slide of my PowerPoint. Is there a way to do this?
The purpose is I have a client who insists on a PPT report, except I need to use Excel to create the information required. Rather than constantly having two documents open and transferring the info from the Excel sheet to the PPT slide, I wanted to consolidate into one document, thus the Excel sheet added into the PPT file.
I'm not an expert at VBA by any means, but I know enough to muddle my way through if I need to use VBA to accomplish this.
I've uploaded pictures of an example (I hope). On slide 1, I have three cells filled in using the inserted Excel sheet. Slide 2 is where I need to reference those cells (text boxes with text in red). The information in those cells will change week from week and I need the text boxes in slide 2 to update with it. Any help would be appreciated.
Use the Selection Pane in PowerPoint to identify the embedded object's name, and reference it, then use the OLEFormat.Object to get a handle on the Workbook object, and from there you're just working with an instance of Excel.Workbook class, so all your familiar Excel properties & methods should be available:
Option Explicit
'Requires reference to Excel
Sub populate_text_box()
Dim obj As Object
Dim tb As Shape 'TextBox
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set obj = ActivePresentation.Slides(1).Shapes("Object 3")
Set wb = obj.OLEFormat.Object
Set ws = wb.Sheets(1)
Set tb = ActivePresentation.Slides(2).Shapes("TextBox 1") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("B2").Value
Set tb = ActivePresentation.Slides(2).SHapes("TextBox 2") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("D2").Value
Set tb = ActivePresentation.Slides(2).SHapes("TextBox 3") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("F2").Value
End Sub

Paste Excel Range into Powerpoint as Table

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)

Excel VBA - Can I manipulate data on another workbook?

Background:
Every month I need to run a report. However, before I can do this, I must export some data in to excel. Once the data is in excel, I have to alter some columns and get the format correct before it is appended to the end of another document to do some analysis on.
What I would like:
I would like to have the document that I append the data to open. I will then export the data in to excel from my program (excel just opens with the data and it is not saved anywhere) and from my larger document, run a VBA script that will alter the data on the other workbook (Book1) so that it can be copied over to the analysis document when in the correct format.
What I have so far:
I have started basic. So far all I am trying to do is set all the cells to the correct height to make it easier to read. However, when I run this code, I get:
Run-time error '9':
Subscript out of range
The code I have so far is:
Sub Data_Ready_For_Transfer()
' Format all cell heights to 15
With Workbooks("Book1.xlsm").Worksheets("Sheet1")
Cells.RowHeight = 15
End With
End Sub
It appears to be having issues with the With Workbooks("Book1.xlsm").Worksheets("Sheet1") part of the code. I have also tried With Workbooks("Book1").Worksheets("Sheet1") and I have tried this with the open, unsaved document and a saved version of the workbook.
Am I missing something obvious?
As follow up from comments, workbook Book1 was opened in another instance of Application object.
In that case this code should work (for already opened workbook):
Sub Data_Ready_For_Transfer()
Dim wb As Workbook
Set wb = GetObject("Book1")
'if we get workbook instance then
If Not wb Is Nothing Then
With wb.Worksheets("Sheet1")
.Cells.RowHeight = 15
End With
End If
End Sub
One more issue, I've changed Cells.RowHeight = 15 to .Cells.RowHeight = 15 to specify, that Cells belongs to workbook Book1 sheet Sheet1.