Vb.net Count String In Excel - vb.net

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.

Related

Using other workbook without opening it

I created Excel macro, in which I'm searching current date in other, closed workbook and getting the number of the row where this date is. Everything works perfectly, but only if I'm opening this other workbook.
Is there a possibility to make the code works without opening this workbook? Or maybe it's possible to make this workbook not to appear, because I don't want it to appear on the screen?
I've tried many methods, e.g. Application.ScreenUpdating = false or ActiveWorkbook.Windows(1).Visible = False but it doesn't work the way I want it.
You need to create a new Excel Application to be able to hide the Window:
Dim xlApp As New Excel.Application 'New Excel Application
Dim xlWB As Excel.Workbook 'Workbook Object
xlApp.Visible = False 'Hide Application
Set xlWB = xlApp.Workbooks.Open("PATH TO FILE") 'Open File in new App
'do something here
xlWB.Sheets(1).Cells(1, 1).Value = "Hello, this is a Test"
xlWB.Close 'Close Workbook (Use SaveChanges:=True to save changes)
Set xlWB = Nothing 'Clear Object
xlApp.Quit 'Quit Excel App
Set xlApp = Nothing 'Clear Object

How to switch to open Excel spreadsheet and work on it from Word with VBA

I'm just trying to work on an open Excel spreadsheet from Word using VBA. I did a search on this and found the following code on How to get reference to an open Excel spreadsheet from Word?. The problem is, it seems to open a separate instance of Excel rather than the one I already have open, and then closes it after the action. How can I get the procedure to switch to the open Excel spreadsheet, perform the desired actions, and leave the spreadsheet open?
Sub DoStuffWithExcelInWord()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.csv")
Set wk = wkbk.Sheets(1)
Debug.Print wk.Cells(1, 1).Value 'Here's where I would like to insert my code
xl.Quit
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Thanks for any assistance!
Thanks again to dcromley for his earlier response. In the meantime, here's what I came up after a couple more searches on the Internet. It takes care of the situation where the Excel document is already open. Hopefully that will help others with similar situations, although it's not exhaustive (for example, if several Excel documents are open).
Sub DoStuffWithExcelInWordRevised()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
On Error GoTo 0
If xl Is Nothing Then
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.xlsx")
Set wk = wkbk.Sheets(1)
End If
xl.Visible = True
With xl
' Insert Excel code here
End With
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Try:
Add xl.Visible = True
Del xl.Quit
It appears that you are trying to do this with a Workbook that is already open. Since you want to use the open workbook / application then you need to:
Set xl = GetObject(,"Excel.Application")

Opening a workbook with VBA/macro is making it read only?

I would like my code to open a workbook (always the same one), detect the first free row, write to just two cells in that row, and then save/close the workbook. This seems like a simple problem, but the macro seems to be opening a copy of the file, and then locking it for editing.
Can you see any errors in my open code? I know that the file opens and that the row search works, but then it 1. never writes to the cells, and 2. locks the file.
Function WriteToMaster(Num, Path) As Boolean
'Declare variables
Dim xlApp As Excel.Application
Dim wb As Workbook
Dim ws As Worksheet
Dim infoLoc As Long
Set xlApp = New Excel.Application
'Specifies where the Master Move Key is stored
Set wb = xlApp.Workbooks.Open("DOC LOCATION")
Set ws = wb.Worksheets("Sheet1")
'Loop through cells, looking for an empty one, and set that to the loan number
infoLoc = firstBlankRow(ws)
MsgBox "First blank row is " & infoLoc & ". Num is " & Num
ws.Cells(infoLoc, 1).Value = Num
ws.Cells(infoLoc, 2).Value = Path
'Save, close, and quit
wb.Save
wb.Close
xlApp.Quit
'Resets the variables
Set ws = Nothing
Set wb = Nothing
Set xlApp = Nothing
'pieces of function from http://p2p.wrox.com/vb-how/30-read-write-excel-file-using-vb6.html
End Function
Thank you again, stackoverflow <3
Do you need to open a new excel app just to open a workbook?
Can't you just do something like this:
Sub Macro1()
Dim wkb As Workbook
Workbooks.Open Filename:="\User Documents$\bob\My Documents\workbook_open_example.xlsx"
Set wkb = Workbooks("workbook_open_example.xlsx")
End Sub

Excel worksheets in vb.net

Is there a way when to make the current worksheet your on in excel to be the worksheet shown when changing worksheets in vb.net? Say I'm on worksheet 1 and change it to worksheet 2 through vb.net, If I enter in data it will show up on worksheet two but in order to see the data I have to physically go to the excel file and select worksheet 2 in order to change the actually worksheet page being displayed.
Tried this code and it seems to work on my side. The trick is the method sheet.Activate()
Sub Main
Dim excelApp as Microsoft.Office.Interop.Excel.Application = _
new Microsoft.Office.Interop.Excel.Application()
Try
Dim inFile As String = "D:\temp\test.xls"
' Show excel '
excelApp.Visible = true
Dim excelWorkbook As Microsoft.Office.Interop.Excel._Workbook = _
excelApp.Workbooks.Open(infile)
Dim x as Integer
for x = excelApp.Sheets.Count to 1 step -1
Dim sheet as _Worksheet = CType(excelApp.Sheets(x), _Worksheet)
sheet.Activate() ' Activate the sheet'
Thread.Sleep(5000) ' Sleep for 5 seconds to see the sheet'
Next
Finally
if excelApp IsNot Nothing then
excelApp.DisplayAlerts = false
'excelApp.Quit(); 'remove the comment to close excel'
End if
End try
End Sub

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"))