How to select a cell A3 in Excel using VB.net? - vb.net

I want to select the Cell A3 using VB.net..
I tried doing this by:
sheet.Range("A3:A3").Select()
But this gives an exception = Select method of Range Class Failed !
What is the problem and how to do it ?
Please help.. I am waiting for the reply !

Assuming you meant Excel VBA try this:
sheet.Range("A3").Select
You can just specify the cell if all you want is one cell.

This program works for me in VB.NET, I agree with rajah9, check the other aspects.
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add()
oSheet = oBook.Worksheets(1)
oSheet.Range("A3").Select()
oExcel.ActiveCell.Value = "Put text here"
oBook.SaveAs("C:\Path\testinterop.xlsx")
oExcel.Quit()
End Sub
End Class
(based on, and drawn in part from, examples here)

Related

Run private sub in Visio

I wrote two macros in MS Visio. The first macro hides all layers, the second macro selects a specific layer to be shown. Both macros work fine by themselves, but when I try to run the first macro in the second macro I received a compile error, that the sub cannot be found. Any ideas on how to call the private sub correctly?
First sub, which hides all layers:
Private Sub Deselect_layers()
Dim vsoPage As Visio.Page
Dim vsoShape As Visio.Shape
Dim vsoLayers As Visio.Layers
Dim vsoLayer As Visio.Layer
Set vsoPage = ActivePage
Set vsoLayers = vsoPage.Layers
For Each vsoLayer In vsoLayers
If vsoLayer.CellsC(visLayerVisible).FormulaU = "1" Then
vsoLayer.CellsC(visLayerVisible).FormulaU = "0"
End If
Next
End Sub
Second sub, which shows one specifc layer:
Sub Select18()
Dim vsoPage As Visio.Page
Dim vsoShape As Visio.Shape
Dim vsoLayers As Visio.Layers
Set vsoPage = ActivePage
Set vsoLayers = vsoPage.Layers
RUNMACRO ("SelectLayers.Deselect_layers") <-- Error occurs here
vsoLayers.Item("18").CellsC(visLayerVisible).FormulaU = "1"
End Sub
What I have tried so far:
RUNADDON ("ThisDocument.Deselect_layers")
RUNMACRO ("ThisDocument.Deselect_layers")
Callthis ("ThisDocument.Deselect_layers")
RUNADDON ("ThisDocument.SelectLayers.Deselect_layers")
RUNMACRO ("ThisDocument.SelectLayers.Deselect_layers")
Callthis ("ThisDocument.SelectLayers.Deselect_layers")
No of these commands worked. Any help would be much appriciated!
Try this:
Private Sub Deselect_layers()
Debug.Print "In Deselect_layers()"
End Sub
Sub Select18()
Call Deselect_layers
End Sub

Share variables between excel and visual basic

I recently started programming with .NET visual basic using visual basic studio. I am also using excel VBA to make some macros. I would be very appreciative if someone could answer a question I have, apologies if the answer is obvious, I'm just getting started:
Basically, if I have set a variable in excel VBA, for example:
dim text as string
text = "hello world"
Would it be possible for me to use that variable when programming in visual basic and have it retain its value from when it was set in the excel VBA macro.
Please comment if you need clarification.
Many thanks.
SOLUTION:
Okay I managed to figure it out with the help of the solutions, the code that works in VB is as follows:
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Core
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oxl As excel.application
Dim owb As excel.workbook
Dim osheet As Excel.worksheet
Dim orng As excel.Range
Dim strtext As String
oxl = CreateObject("Excel.application")
owb = oxl.Workbooks.Open(Filename:="C:\Users\USERNAME\Documents\Variable Passing Test.xlsm")
oxl.Run("dosomethingtostrtext")
strtext = oxl.Run("getstrtext")
MsgBox(strtext)
End Sub
End Class
One way to access variables in another workbook's VBA code is to create functions that return those values to you. Then call those functions from your other app.
For example, let this be code in a module in your Excel file with the macro:
Option Explicit
Private strText As String
' Say you have a routine that manipulates strText (or not, even!)
Public Sub doSomethingToSTRTEXT()
strText = "Hello World!"
End Sub
' This is the function to call to retrieve strText
Public Function getSTRTEXT() As String
getSTRTEXT = strText
End Function
And this is code in a VBA project elsewhere (not running .Net on this machine, just microsoft office sad) where you have this:
Option Explicit
Sub Test()
' Declare
Dim WBK As Workbook
' Open the workbook in question
Set WBK = Workbooks.Open(Filename:="C:\Path\To\File\With\VBA.xls")
' This code's own variable
Dim strText As String
' Call the routine (or not) that does something to that workbook's vba's strText
Application.Run (WBK.Name & "!doSomethingToSTRTEXT")
' Now let's retrieve that value via function!
strText = Application.Run(WBK.Name & "!getSTRTEXT")
' Show it to me
MsgBox strText
End Sub
I would like to leave the conversion of the code right above ^ into VB.Net to you (or do a search here on SO for .Net code to handle Excel objects) and try it out

List all opened WorkBooks in the Task Bar

I am trying to list all the opened workbooks and their corresponding sheets in the task bar after that I should be able to select one workbook of the list and open it. My first try was to to show Excel process in Task-Manager but it only shows one open workbook without detecting the number of sheets.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim name As Process() = Process.GetProcessesByName("Excel")
For Each names In name
ListView1.Items.Add(names.MainWindowTitle)
If names.MainWindowTitle <> "" Then
ListBox1.Items.Add(names.MainWindowTitle)
End If
Next
End Sub
Next I tried to use this code but also I can only display one opened workbook, I don't know how to loop through them. I don't have problem of changing the whole code if you have any other method cause I am not sure that it's the solution.
Dim oXL As Microsoft.Office.Interop.Excel.Application
oXL = TryCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
oXL.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized
Dim y As String
y = oXL.ActiveWorkbook.Name
ListBox1.Items.Add(y)
Dim objExcel As Excel.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
Dim objWorkBook As Excel.Workbook = Nothing
Dim totalWorkBooks As Integer = objExcel.Workbooks.Count
For i As Integer = 1 To totalWorkBooks
objWorkBook = objExcel.Workbooks(i)
With objWorkBook
FullName = .FullName
OnlyName = .Name

Is it possible to pass a range to a userform-property?

I am trying to adapt the code found here (the third method, as that is what seems to be deemed best-practice), to suit my needs, but I'm not having any luck - the code I have so far, more or less copy-pastaed from that page is the following:
In the form-module:
Private calling_cell As Range
Property Set range_to_form(ByRef r As Range)
Set calling_cell = r
End Property
Private Sub UserForm_Activate()
Debug.Print calling_cell.Address
End Sub
In a worksheet_change-event:
Dim frm As ufRegLuft
If Not IsUserFormLoaded("ufRegLuft") Then
Set frm = New ufRegLuft
Else
Set frm = VBA.UserForms("ufRegLuft")
End If
Set frm.range_to_form = Target
ufRegLuft.Show
The problem:
This does not work - I get an error on the debug.print-line, saying "Run-time error '91': Object variable or With block variable not set". I must admit I'm pretty stumped at this point, I feel like I have tried every possible combination of set, let, get etc. So, can anyone here please help me figure out if it is possible to pass a Range-object to an userform, and if so, tell me what I'm doing wrong?
Your variable calling_cell is initialized in the form frm and not in ufRegLuft
Change the line ufRegLuft.Show to frm.Show ;)
Here is simple way to test it.
Form Module
Private calling_cell As Range
Property Set range_to_form(ByRef r As Range)
Set calling_cell = r
End Property
Private Sub UserForm_Activate()
MsgBox calling_cell.Address
End Sub
Normal Module
Sub ShowFormProp()
Dim frm As ufRegLuft
Set frm = New ufRegLuft
Set frm.range_to_form = Sheet1.Range("A1")
frm.Show
End Sub
You have to pass the variable to a sub in the form, (I believe user form_Initialize will fire)
I am not sure where r is set but create a public sub in the form that sets the range, call that before you show the form then show the form like below
In the form-module:
Private calling_cell As Range
public sub range_to_form(ByRef r As Range)
Set calling_cell = r
End Property
Private Sub UserForm_Activate()
Debug.Print calling_cell.Address
End Sub
In a worksheet_change-event:
Dim frm As ufRegLuft
If Not IsUserFormLoaded("ufRegLuft") Then
Set frm = New ufRegLuft
Else
Set frm = VBA.UserForms("ufRegLuft")
End If
frm.range_to_form r
'Could be "range_to_form r" not above
frm.Show

Calling Sheet from a WorkBook

I'm trying to call the 5th sheet in an open workbook. When I open the workbook from the program I seem to be able to do it:
Dim CurrentRun As New Excel.Application
Dim CurrentBook As Excel.Workbook
Dim CurrentSheet As Excel.Worksheet
Private Sub GeneralButtonOpener_Click(sender As Object, e As EventArgs) Handles GeneralButtonOpener.Click
CurrentRun.Visible = True
CurrentBook = CurrentRun.Workbooks.Add(MainTemplatePath)
CurrentSheet = CurrentBook.Worksheets(4)
CurrentSheet.Activate()
End Sub
But all my attempts at calling the sheet if the file is already open have failed:
Dim CurrentRun As Microsoft.Office.Interop.Excel.Application
Dim CurrentBook As Microsoft.Office.Interop.Excel.Workbook
Dim CurrentSheet As Microsoft.Office.Interop.Excel.Worksheet
CurrentRun = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
CurrentBook = CurrentRun.Workbooks
CurrentSheet = CurrentBook.Sheets(4)
CurrentSheet.Activate()
or
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim CurrentRun As Microsoft.Office.Interop.Excel.Application
Dim CurrentBook As Excel.Workbook
Dim CurrentSheet As Excel.Worksheet
CurrentRun = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
CurrentBook = CurrentRun.ActiveWorkbook
CurrentSheet = CurrentBook.Sheets(4)
CurrentSheet.Activate()
End Sub
I've looked at several examples, but I can't figure out where I'm going wrong. Which surprises me as there seem to be a lot of questions on the subject. Ether a pointer to where this is solved/addressed or what I'm specifically doing wrong would be appreciated.
Thanks!
Much to my surprise, I found that I in fact had dozens of instances of Excel running in the background. When I am debugging, and launch the COM the first instance of Excel is started. The second is started when I open the windows form (the main part of the add-in).
What I didn't know, was that when an exception was thrown, and I stop things from within Visual Studio, only the first instance of Excel was closed. I have code that tries and clean up open applications of Excel, but of course it was not reached because an exception was thrown.
And here I had been thinking I would put Error handling a bit down the road when I had things a little more developed. Clearly I need to address some basic error handling much earlier in my build process. I'm entirely self taught, and somehow made it three years without that being an issue.
Hopefully someone else who wasn't been taught that can see this before pulling their hair out for 14 hours.
Solution
Close all other instances of Excel and the above code works. Address cleanup in error handling and earlier as addressed here: http://support.microsoft.com/kb/317109
Maybe also call the GC, though that seems controversial.
Final code:
....
Imports System.Runtime.InteropServices
....
Dim CurrentRun As New Excel.Application
Dim CurrentBook As Excel.Workbook
Dim CurrentSheet As Excel.Worksheet
....
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
On Error GoTo VortexInYourSoul
CurrentRun = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
CurrentBook = CurrentRun.Workbooks(1)
CurrentRun.Visible = True
CurrentSheet = CurrentBook.Sheets(8)
CurrentSheet.Activate()
CurrentBook.ActiveSheet.name = "LLAMA LALA LLAMALMALMAL"
....
Exit Sub
VortexInYourSoul:
CurrentSheet = Nothing
CurrentBook.Close(False)
CurrentBook = Nothing
CurrentRun.Quit()
CurrentRun = Nothing
MsgBox("Error: " & Err.Description)
End Sub