VBA - Changing Reference document in named formula - vba

EDIT: I wanted to clarify my earlier question. I have existing name formula in my name manager. I would like to set these variables listed below to reference different columns from another document. These variables are referenced in my other spreadsheet.
My question is
1) How do I set the variables to reference another document's column?
2) How can I make it so the Macro allows me to choose which document each time?
Here's my code
Sub UpdateReport()
Dim wbk As Workbook
Set wbk = Workbooks.Open("U:\user\Destination.xls")
Dim x As Variant
Dim FY123 As Range
Dim FY134 As Range
Dim FY145 As Range
Dim FY156 As Range
Dim FY167 As Range
Dim FINCC As Range
Set FINCC = wbk.Worksheets("Worksheet2").Column("D")
Set FY123 = wbk.Worksheets("Worksheet2").Columns("N")
Set FY134 = wbk.Worksheets("Worksheet2").Columns("O")
Set FY145 = wbk.Worksheets("Worksheet2").Columns("P")
Set FY156 = wbk.Worksheets("Worksheet2").Columns("Q")
Set FY167 = wbk.Worksheets("Worksheet2").Columns("R")
End Sub
Thanks,
GS
EDIT: Clarified question

Related

Get Word bookmark index to replace image inside bookmark from Excel

This question is related with a previous one.
I have an open Word document with a bunch of bookmarks, each with an inline image of an Excel table previously exported from Excel.
Now, I need to update the tables in the Word document as they have changed in Excel.
The way I'm doing this is matching the table names in Excel with the bookmark names in Word. If they are equal than I want to replace the existing images in Word by the current ones.
This is my code so far:
Option Explicit
Sub substituir()
Dim Mark As String
Dim Rng As Range
Dim ShpRng As Range
Dim WordApp As Object
Dim DocumentoDestino As Object
Dim folha As Worksheet
Dim tabela As ListObject
Dim nomeTabela As String
Set WordApp = GetObject(class:="Word.Application")
Set DocumentoDestino = WordApp.ActiveDocument
For Each folha In ThisWorkbook.Worksheets
If folha.Visible Then
'loop all excel tables
For Each tabela In folha.ListObjects
tabela.Name = Replace(tabela.Name, " ", "")
Mark = CStr(tabela.Name)
With ActiveDocument
If .Bookmarks.Exists(Mark) Then
Set Rng = .Bookmarks(Mark).Range ' returns runtime error 13: Type mismatch, I guess it is because .Bookmarks expects the bookmark index instead of the name.
If Rng.InlineShapes.Count Then
Set ShpRng = Rng.InlineShapes(1).Range
With ShpRng
Debug.Print .Start, .End
ShpRng.Delete
End With
End If
End If
End With
Next tabela
End If
Next folha
End Sub
The code seems ok, except for the line marked above that returns runtime error 13, is there any way to get to the bookmark index instead of the name or another way to fix the issue?
Thanks in advance!
The problem is from the Range object. There is such an object in Excel as well as in Word. Since you are running Excel, both Rng and ShpRng are declared as Excel ranges implicitly. Declare them as Word.Range.
Quite generally, be more careful with your use of variables. You perfectly declared Set DocumentoDestino = WordApp.ActiveDocument, but then you proceed with
With ActiveDocument
If .Bookmarks.Exists(Mark) Then
In Excel, there is no ActiveDocument. Perhaps that is why Excel correctly divines your intention to refer to DocumentoDestino. However, if you don't keep tight control instances are likely to arise - whenever you least expect them, of course - when Excel makes the wrong guess.

Excel VBA: Is it possible to refer to a column in a table using the variable name for the table?

I have a table in my worksheet called tblMetaData. I have created a Range variable named rngTblMetaData to refer to this table. Is it possible to refer to columns within this table using the variable? For instance in the code below I want to refer to a column in the table "tblMetaData" with the header "Track Name". I would typically do this with "WsMetaData.Range("tlbMetaData[Track Name]")". Is it possible to shorten this by using the "rngTblMetaData" variable instead? Thank you!
Ex:
Public Wb As Workbook
Public WsMetaData As Worksheet
Public rngTblMetaData As Range
Sub SetVariables()
Set Wb = ThisWorkbook
Set WsMetaData = Wb.Sheets("MetaData")
Set rngTblMetaData = WsMetaData.Range("tlbMetaData")
If you are working with an Excel table (which is a ListObject), then you should dim it as such. You can either treat the table as a range on the worksheet, and refer to it as you suggested, or work directly with the ListObject, but that means that you refer to columns by number, so will have to search for your heading:
Public Wb As Workbook
Public WsMetaData As Worksheet
Public tblMetaData As ListObject
Public columnData As Range
Sub SetVariables()
Set Wb = ThisWorkbook
Set WsMetaData = Wb.Sheets("MetaData")
Set tblMetaData = WsMetaData.ListObjects("tblMetaData")
Set columnData = WsMetaData.Range("tblMetaData[Track Names]")
'or
Dim columnNumber As Long
columnNumber = WorksheetFunction.Match("Track Names", tblMetaData.HeaderRowRange, 1)
Set columnData = tblMetaData.ListColumns(columnNumber).DataBodyRange
End Sub

Defining a variable as a range - VB.NET

I think I'm losing my mind - how do you declare a variable as a string and then set it equal to a range in an Excel workbook in VB.NET? In VBA this was easy:
Dim SQL as string
SQL = ActiveWorkbook.Sheets("MySheet").Range("SQL")
If I try do something like this in VB.NET (in Visual Studio 2015), first I can't find Activeworkbook. Second, if I try Excel.Range("SQL"), I get an error saying that 'Range' is an interface type and cannot be used as an expression. Also, it doesn't look like the Range data type exists either. Surely this functionality exists in VB.NET, right?
Thanks for the help!
To work on Excel since VB.NET, first you must add the reference to your Project :
Microsoft.Office.Interop
To Add a Reference :
In Solution Explorer, right-click on the References node and choose Add Reference.
Import the Reference in your code :
Imports Microsoft.Office.Interop
Try to use this code :
Dim AppExcel As New Excel.Application 'Create a new Excel Application
Dim workbook As Excel.Workbook = AppExcel.Workbooks.Add() 'Create a new workbook
Dim sheet As Excel.Worksheet = workbook.Sheets("Sheet1") ' Create variable a Sheet, Sheet1 must be in WorkBook
'Work with range
Dim cellRange1 As Excel.Range = sheet.Range("A1") 'Range with text address
cellRange1.Value = "Text in Cell A1"
Dim cellRange2 As Excel.Range = sheet.Cells(2, 2) 'Range("B2:B2") with index; Cells(N°Row,N°Col)
cellRange2.Value = "Text in Cell B2"
Dim tableRange3 As Excel.Range = sheet.Range("A1:F4") 'Range with text address
Dim tableRange4 As Excel.Range = sheet.Range(sheet.Cells(1, 1), sheet.Cells(4, 6)) 'Range("A1:F4") with index; Cells(N°Row,N°Col)
AppExcel.Visible = True 'To display the workbook
Code without variable sheet
Dim AppExcel as New Excel.Application
Dim workbook As Excel.Workbook = AppExcel.Workbooks.Add()
'Range
Dim cellrange1 as Excel.Range = AppExcel.ActiveWorkbook.Sheets("Feuil1").Range("A1")
You would need to start from your application object. Suppose that's AppExcel:
Dim AppExcel As New Excel.Application
From there, you could do:
Dim cellrange1 as Excel.Range = AppExcel.ActiveWorkbook.Sheets("MySheet").Range("SQL")
Because you've declared cellrange1 as a Range it can't be set to Range("SQL").Value.
Value returns an object which is the value contained in that Range.
That's so wordy. To put it (maybe) more clearly, Range("SQL") returns a Range. Range("SQL").Value returns an object.
If you want to get the value, that would be cellrange1.Value, or perhaps cellrange1.Text. Assuming that the range contains some sort of SQL, I'd go with Text.
An unfortunate aspect of Excel interop programming is that many properties return objects rather than strongly-typed values. For example, the object returned by Range.Text is always going to be a string, but the property still returns an object. That means that Visual Studio intellisense will often not tell you what type a property returns. You'll need to look up properties and functions in the documentation to really know what they return.

vba: Offset doesn't work from Word script in Excel doc

I have a Word document that should open an Excel document, find the first empty cell in a range and start filling some cells with information. Herefore I'd like to use Offset, but for some reason it gives an error with Offset. Here's the relevant part of the code:
Sub ExcelDoc()
Dim XLapp As Object
Dim objExcelDoc As Object
Dim objOverzicht As Object
Dim c As Range
'Set objOverzicht
Set objOverzicht = ActiveDocument
'Set XLapp and objExcelDoc
Set XLapp = CreateObject("Excel.Application")
XLapp.Visible = False
Set objExcelDoc = XLapp.Documents.Open("C:\Document.xlsm")
'Set c
Set c = objExcelDoc.Sheets("Overzicht").Range("B3")
Do
Set c = c.Offset(1, 0)
Loop Until c = ""
When I run the code, it marks ".Offset" and displays the message "Compile error: Method or data member not found". What am I doing wrong here?
Many thanks in advance for looking into this!
Word itself has a "Range" object, so when you declare Dim c As Range c is created as Word.Range object, which has no Offset property. So you have 2 ways of solving the problem:
Just declare c as Variant: Dim c as Variant
Make an MS Excel reference and declare variable with it:
2.1 In VBA Editor go to Tools->References and turn on "Microsoft Excel XX.X Object Library"
2.2 Declare variable with reference: Dim c as Excel.Range

Using a Variable to Dim a Worksheet

I have a sheet with one cell that is = the name of a folder that I want to dim as a variable. what I want to do is set that cell = the filename variable. It will probably be easier to look at my code. I am currently getting the "object required error on my "set Input 1" and my way to set the variable is presumably wrong as well
Dim WbkA As Workbook
Dim Input1 as string
Set Input1 = Workbooks.Open(Filename:="G:\Reporting\ReportCompare.xls").worksheets("Sheet4").Range("A4").Value
Set wbkA = Workbooks.Open(FileName:"Input1")
You try to assign a reference of an object with the keyword Set to a data type (String).
Remove the keyword Set and it's gonna be okay.
The code needs to be reordered slightly in order to breakout the steps.
Get the file path and name from the workbook and store it as a string
variable (Input1).
Open the file using the value stored in the string variable (Input1).
Set a reference to the open file as an object variable (WbkA).
Listed below is the code
Sub test()
Dim Input1 As String
Dim WbkA As Workbook
Input1 = Worksheets("Sheet4").Range("A4").Value 'Get the path and file name
Workbooks.Open Filename:=Input1 'Open the file
Set WbkA = ActiveWorkbook 'Set the reference to the workbook
MsgBox WbkA.Name 'Show the name value from the object.
End Sub