I've been having trouble taking cell values from excel and using them in a word macro (I'm trying to insert string values from sheet cells at various bookmarks in a word doc). Right now I'm just trying to be able to access cell values, but I'm coming up with an error 429 (ActiveX component can't create object). Any help/advice on how to approach pulling values from excel and using them in word would be appreciated.
Dim objExcel As New Excel.Application
Dim exWbs As Excel.Workbooks
Dim exWb As Excel.Workbook
Dim strWbName As String
Dim cisInfo As CIS
Sub PopulateDoc()
Set exWb = New Excel.Workbook
exWb = exWbs.Open("CIS.xlsx")
exWb.Sheets("Property Information").Cells(2, 8).Value = "test"
End Sub
You cannot create a New instance of a WorkBook instead:
Set exWb = objExcel.Workbooks.Open("CIS.xlsx")
(You also need to have added a reference to the Microsoft Excel XX Object Library)
Related
Can anyone please help me with the code? Im looking to create a macro where I can update the Powerpoint's chart from the data selected/copied in the excel file. Below are the following steps to understand my request well:
New data are available in excel file. I will the select/copy the data
Charts are existed in the powerpoint, but the data are old. Need to update only the data which are copied/selected in the excel file using VBA automations.
Below are my code, pls assist.
Sub update_Chart()
Dim myDocument As Variant
Dim myChart As Variant
Dim Wb As Object
Dim App As Object
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
Set myChart = ActiveWindow.Selection.ShapeRange(1).Chart
myChart.ChartData.Activate
Set Wb = myChart.ChartData.Workbook
Set App = Wb.Application
clipboard.SetText ActiveSheet.Range("A1")
clipboard.PutInClipboard
Wb.Close (0)
End Sub
Now the above code does not able to update the new data.
I want to create a "driver" workbook where someone can update values that will be applied to another workbook. The contents of cell B8 of "macros.xlsm" contains the text string I want to use for the author of "report1.xlsx". I have written the following macro but keep getting a
"Object doesn't support this property or method" error on the last line.
Sub add_properties()
Dim xL As Excel.Application
Set xL = New Excel.Application
Dim mainWB As Excel.Workbook
Dim reportWB As Excel.Workbook
Set mainWB = xL.Workbooks.Open("C:\Users\ga1085\adHoc\macros.xlsm")
Set reportWB = xL.Workbooks.Open("C:\Users\ga1085\adHoc\report1.xlsx")
MsgBox mainWB.Sheets("adHoc").Range("B8").Value
mainWB.Sheets("adHoc").Range("b8").Copy
reportWB.BuiltinDocumentProperties("author").PasteSpecial (xlPasteValues)
End Sub
I am also using "macros.xlsm" to update margins, headers, etc for "report1.xlsx" - will this work on those too?
Try opening your workbooks this way. You'll need to add the Microsoft Excel Object Library to your references.
Dim xL As excel.Application
Set xL = New excel.Application
Dim mainWB As excel.Workbook
Dim reportWB as excel.Workbook
Set mainWB = xL.Workbooks.Open("macros.xlsm")
Set reportWB = xL.Workbooks.Open("report1.xlsx")
I figured out how to get it to work. Macros.xlsm is already open and contains the macro that I want to execute. I didn't have to use PasteSpecial. I think the main difference is that I had to declare a variable "author" then use the variable "author" to update the report workbook. I am new to VBA - and would appreciate any other input or explanations. Thank you for your help.
Sub add_properties()
Dim author
Dim reportWB As Excel.Workbook
Set reportWB = Workbooks.Open("C:\Users\ga1085\adHoc\report1.xlsx")
author = Workbooks("macros.xlsm").Sheets("adHoc").Range("b8").Value
reportWB.BuiltinDocumentProperties("author").Value = author
End Sub
I'm trying to get my application to read a excel file that works with named ranges.
My excel has alot of named ranges, and I would like to access the values in these named ranges to fill my combobox on my windows-user-form.
But i have no idea how get these values.
For now i have this to access my excel spreadsheet + range
Dim excel As Application = New Application
Dim w As Workbook = excel.Workbooks.Open("C:\temp\test.xlsm")
Dim rng As Range
rng = w.Worksheets("Sheet1").Range("range")
w.Close()
On this "Sheet1" there is a Named Range called "Range" and it contains 6 cells. I want to add these cell values to my combobox1 using the Named Range.
But i can't figure out a way to do this.. Can someone help me? :)
You can try using Goto and Selection. Here is a sample that returns an array of values, as per your requirement:
Dim xl As Excel.Application
Dim wb As Excel.Workbook
xl = New Excel.Application
wb = xl.Workbooks.Open("C:\Temp\range.xlsm")
xl.Goto("NewNamedRange")
Dim rng As Excel.Range = xl.Selection
Debug.Print(rng.Value.ToString)
I am writing a VB.Net program to pull information from an Excel sheet and display it on my keyboard's LCD panel. However, I've run into some trouble. I managed to get it to display on the LCD, and read the data from Excel.
However, it isn't pulling from the worksheet that I want. It pulls from the middle worksheet in the book, and this is a large, multi-megabyte book that has numerous sheets. My code is as follows.
Dim Excel As Microsoft.Office.Interop.Excel.Application
Dim intraday As Microsoft.Office.Interop.Excel.Workbook
Dim iSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim t As String
Excel = CreateObject("Excel.Application")
Excel.Workbooks.Open("C:\excelworkbook.xlsm")
iSheet = Excel.Workbooks(1).Worksheets(9)
t = Excel.Cells(11, 1).Value.ToString
Can you try this code.
Dim Excel As Microsoft.Office.Interop.Excel.Application
Dim intraday As Microsoft.Office.Interop.Excel.Workbook
Dim iSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim t As String
Excel = CreateObject("Excel.Application")
intraday=Excel.Workbooks.Open("C:\excelworkbook.xlsm")
iSheet = intraday.Worksheets(n)
t = Excel.Cells(11, 1).Value.ToString
where n is the desired worksheet.
a) I have assigned a name "abc" to range A3 to G3 using name manager functionality in excel 2007
b) I want to access this name from VB.NET to assign values in a loop
How to do this? I am new to VB.NET
Excel workbook will not be open
I tried it using usual VBA like code.
For i = 1 To k
costs("abc").RefersToRange.Cells(3, i).value = "Cost" & i
Errors:
1) 'Microsoft.Office.Interop.Excel.Names' cannot be indexed because it has no default property.
2) Loop control variable cannot be a property or a late-bound indexed array.
Thanks for your help in advance.
You can access the range as follows. This finds all the values from a range named abc that is defined as A3:G3 in the first worksheet of an excel file:
'initialise the objects and open the file
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open("C:\foo\book1.xlsx")
Dim xlSheet As Excel.Worksheet = xlWorkBook.Worksheets(1)
For i As Integer = 1 To 7
xlSheet.Range("abc").Cells(1, i).value = "Cost" & i.ToString
Next
'save file
xlWorkBook.Save()
'close objects and clean up
xlWorkBook.Close()
xlApp.Quit()
xlSheet = Nothing
xlWorkBook = Nothing
xlApp = Nothing
Provided you are using Excel 2007 or above, you can create and edit Excel workbooks without opening Excel itself using the OpenXML SDK. Instead of using Interop to access Excel, the SDK works against the "raw" Excel file. I haven't specifically worked with named ranges, but there is a sample which uses them and should help you get started.