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.
Related
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'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)
Hello so I am trying to find an exact string in a excel workbook and count how many times it in that sheet and was stuck on how I should do this can anyone help?
'Open a blank Excel File.
oXL = CType(CreateObject("Excel.Application"), Excel.Application)
'Sets oxl visible off so you can't see it open.
oXL.Visible = True
' Get a new workbook.
oWB = oXL.Workbooks.Open(dicJobList)
'Set oSheet to active sheet.
oSheet = CType(oWB.ActiveSheet, Excel.Worksheet)
FillGrid()
oSheet.Cells.Find("DC")
'This is where I would need to count how many times
'It was found.
I want to sum a range from a sheet from one excel workbook and write it to another workbook. I came up with a following code but it is not working.
1) it opens a file open windows which asks me to select workbook to update
2) after selecting a workbook it writes "0" as a value.
Code:
Dim xl1 As New Excel.Application
Dim xl2 As New Excel.Application
Dim wb1 As Excel.Workbook
Dim wb2 As Excel.Workbook
Dim st1 As Excel.Worksheet
Dim st2 As Excel.Worksheet
wb1 = xl1.Workbooks.Open("F:\excelsumtest\file1.xlsx")
wb2 = xl2.Workbooks.Open("F:\excelsumtest\file2.xlsx")
st1 = wb1.Worksheets(1)
st2 = wb2.Worksheets(1)
st2.Cells(1, 1).formula = "=Sum(st1!A1:S1)"
Cheers
Try changing this line:
wb2 = xl2.Workbooks.Open("F:\excelsumtest\file2.xlsx")
'to this:
wb2 = xl1.Workbooks.Open("F:\excelsumtest\file2.xlsx")
I'm tying both workbooks to 1 instance of an Excel application. This isn't tested, but I think that this way, Excel will understand that it's the worksheet from the xl1 workbook that you're trying to insert.
If not, you might have to fully define the workbook you're referencing in your formula.
Hope this helps.
This worked for me:
Dim xlsApp As New Microsoft.Office.Interop.Excel.Application
xlsApp.DisplayAlerts = False