vb.net fill combobox using named ranges (excel) - vb.net

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)

Related

Working with Named Ranges and Objects

I need to copy a range of cells from one workbook to another. However, unfortunately, because of the size of the two workbook I can't have them open at the same time.
So the idea was to do it in two steps:
1) Open workbook1 save the range from one workbook to a object range and close workbook1
2) Open workbook2 save the range from the object to the range in workbook
But this is not working. Could someone help with the code. Thanks. Sample code below
Dim Temp as Range
Workbooks.Open (Model1)
Workbooks(Model1).Activate
Temp = Range("First_Input").Value
Workbook(Model1).Close
Workbooks.Open(Model2)
Workbooks(Model2).Activiate
Range("Second_Input").Value = Temp.Value
A working example below:
Comments are embedded and works as is (without changes). Try this on a
workbooks first with random data.
The example works for a workbook already open, just alter it for "Opening" a workbook.
I have used the range address in the example. You can play with this depending on what you want to do.
The below works so should be easy to implement, copy and paste into excel.
Public Sub CopyData()
Dim wkb1 As Workbook
Dim wkb2 As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim wkb1rng As Range
Dim wkb2rng As Range
'Point to Workbook and Sheet 1
'Set wkb1 = Workbooks.Open(Model1) ' To Open Workbook
Set wkb1 = Workbooks("Book1") ' If workbook is open already
'Sheets is the Index use String for the sheet name
Set sht1 = wkb1.Sheets(1)
' Dont need this if you moving objects directly in and out of memory.
' Workbooks(Model1).Activate
' Point to Range
Set wkb1rng = sht1.Range("First_Input")
' What is the address of the Range
Dim address As String
address = wkb1rng.Cells.address
'Point to Workbook and Sheet 2
Set wkb2 = Workbooks("Book2")
'Sheets is the Index use String for the sheet name
Set sht2 = wkb2.Sheets(1)
'I imagine Second_Input should be output?
'Use this only if the data range is exactly the same size
'Set wkb2rng = Range("Second_Output")
'else use this ...
Set wkb2rng = sht2.Range(address)
'Copy data across ...
wkb2rng.Value = wkb1rng.Value
End Sub

Excel VBA - update BuiltinDocumentProperties using value from different workbook

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

Pulling cell values from Excel into Word (Runtime error 429)

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)

(Access names from closed excel worksheet through VB.NET.) What is wrong with this?

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.

Getting max cells filled number with vb.net

Is there a way to get the max number of user filled cells in an excel sheet without passing a range or iterating through max cells per excel spreadsheet?
right now I have
Imports Excel = Microsoft.Office.Interop.Excel
Private Sub DoStuff()
Dim oApp As New Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
oWB = oApp.Workbooks.Open(txtExcel.Text)
oSheet = CType(oWB.ActiveSheet, Excel.Worksheet)
For Each iCount As Integer in oSheet.Cells.Rows.Count
'get cell text for every row in a specific column'
dim sCellText as string = oSheet.Cells(iCount, 1).toString
'do stuff'
Next
End Sub
Right now the oSheet.Cells.Rows.Count = 1048576 and looking through the sheet there is less than 1000 rows of data.
Try oSheet.UsedRange
Here you can check for the column in the Range instance returned by above, to figure out cells filled in the specific column.
OR
WorksheetFunction.CountA(range("A1:A20000"))