Open and update an existing workbook with 2 worksheets - vba

This may be simplistic to most of you. I just started using VBA to create and update excel workbooks. I found some code on the internet to open and update an existing workbook and worksheet. Like I said, I am brand new at this. Does this code even make sense? I just need to know how to open an existing workbook and all the examples I have found aren't working in our environment.Thanks for any help I can get
Dim wbSource, xlApp, srcWorksheet
'initialize
Set xlApp = CreateObject("Excel.Application")
'open source and target files
Set wbSource = lApp.Workbooks.Open("X:\GCIXCycleCompare_test_auto.xlsx")
set srcWorksheet = wbSource.Worksheets("NewCycle")
srcWorksheet.sheets("NewCycle").Activate
srcWorksheet.Rows("1:1").Delete

If you are in Excel VBA, this isn't quite what you want. This code was written for an external app (say written in VB6) to open Excel remotely and then do stuff to that copy of Excel. If you are already in Excel/VBA you obviously don't need to do that.
In VBA, the equivalent code would be something like this:
Public Sub MyCode()
Dim wb as Workbook
Dim ws as Worksheet
Set wb = Application.Workbooks.Open("X:\GCIXCycleCompare_test_auto.xlsx")
Set ws = wb.Worksheets("NewCycle")
ws.Rows(1).Delete
End Sub
If you run this code (by click F5 from inside VBA ... or by run macro in Excel) it should open up the test file off the X: drive, and then delete the first row of it.

Related

VBA for MS Access: write to an excel file without opening, write to last row, save and close

I am using VBA in MS Access for the first time and cannot get the following right:
Launch an excel file (without actually opening the file), then write to the last row in the excel file, and then save the file (with the same path and name as before, essentially replace the previous file), then close the excel file.
Please assist! So far I can write to an excel file, but cannot save and close without closing the whole MS Access application.
If you could please give a sample of working code to do the above, I will tailor it for my requirements.
Thanks!
Christine
First of all, in order to update and save a file like you want to, you have to open it first- so it is a little confusing/contradictory when you say that you don't want to 'actually open' an excel file... I took it to meant that you just don't want the excel application showing- which you would want something like this:
Public Sub demoCode()
Dim excelApp As Excel.Application
Dim targetWB As Workbook
Dim targetRange As Range
'Create new Excel Application
Set excelApp = New Excel.Application
'Keep hidden
excelApp.Visible = False
'Have new Excel App open workbook
Set targetWB = excelApp.Workbooks.Open("C:\Filename.xlsm")
'Set targetRange to 1 row past the first sheet's usedrange
Set targetRange = targetWB.Sheets(1).Range(targetWB.Sheets(1).UsedRange.address)(targetWB.Sheets(1).UsedRange.Rows.Count + 1, 1)
'Paste # targetRange
'Close and save workbook
targetWB.Close (True)
'Close Excel App
excelApp.Quit
End Sub
Hope this helps,
TheSilkCode

How do you retrieve a pointer to the active workbook In a vb .net excel document customization (not add in)

I am new to VB.net and am moving an app from VBA. I want to use the document customization model, but can't for the life of me find a way to retreive a pointer to the workbook that is hosting the customization. I have tried
dim wb as workbook = Me.Workbook
dim wb as workbook = thisWorkBook
dim wb as workbook = ThisApplication.workbook
etc. I only need this in order to be able to select worksheets in the customization so if there is anotherway to do that then that would answer my question too. For example I want to execute this to get a handle to a specific worksheet.
dim ws as worksheet = wb.sheets("My Sheet Name")
Apologies for what is clearly a very basic question but I have googled around for about three hours so far with no luck.
After a lot of additional trial and error I came across this solution.
You can access the application workbook through the Globals variable produced by VSTO. Thus,outside of the class declaration for thisWorkbook -- the following will work:
Dim wb as ThisWorkBook
wb = Globals.ThisWorkbook
Note that web is not of type Workbook but ThisWorkBook which is an overloaded class defined by Visual Studio in the document customization template.

Vba closing excel applications/workbooks freezes

I have a problem with the following code:
Dim excelapp as object
set excelapp = CreateObject("excel.application")
dim ws as object
dim wb as Workbook
wb= excelapp.Workbooks.Open(path)
ws= wb.Sheets(1)
'in the code i send the worksheet object around by reference in order to read the
'worksheet and manipulate data, i dont create other instances of excel apps or
'workbooks
then i try :
wb.Close
and i have also tried :
excelapp.Quit
Neither have worked, they both freeze and say they are waiting on OLE actions, and i have multiple excel processes opening if i do not call these, when i try to open the excel files i had opened via code, i can only open them as read-only because theyre checked out to me.
I also tried executing a shell script that closes all applications "Excel.Exe" but it closes...the actual excel file where the vba is being executed, so thats not a good solution.
Thank you in advance.
It might be that the Excel app has detected that the workbook has changed and is putting up a dialog box (which is invisible because the app is not visible). Try:
wb.Close False
Which tells Excel to ignore any changes to the workbook.

Linking un-named workbook to variable in VBA

I am writing a macro that will copy and paste information form one workbook into another workbook in excel 2010. The workbook that the data is in is the same workbook as the macro. I have made VBA create a new workbook to paste the data in. How do I assign the new workbook that VBA has just created to a variable.
Thanks For Any Help
You haven't mentioned exactly how you create the workbook, but you can set a reference to the new Workbook object in the same statement that creates it.
Example:
Option Explicit
Sub AddWorkbook()
Dim oWb As Workbook
Set oWb = Workbooks.Add
'Do something with the new workbook
Debug.Print oWb.FullName
Set oWb = Nothing
End Sub
try seeing names of all workbooks by iterating over Workbooks. I think the name of newly created workbook will "Workbook1" until there is already no other unnamed workbook. So basically newly created workbook is still not unnamed.

How to open a file in an active workbook? VB.NET 2008

I have a program that filters data and then outputs it to a tabg delimited file. I then use Excel Interop to open this file in Excel because directly outputting to a worksheet takes too long. So currently I am using this code:
AD.DF.WriteToFile(vbTab)
Dim WB As Workbook = ExcelApp.Workbooks.Open(AD.DF.DatafileInfo.WriteToFileLocation)
ExcelApp.Visible = True
The first line takes the filtered data and outputs to a tab delimited file. The second opens that same file in a new workbook in Excel. And obviously the third makes Excel visible. Here is my problem though: right now when this code is run there are two open workbooks. I already have an active workbook and I would just like to open this file to that workbook.
If this is possible, how do I do it?
Thank you.
Look at the GetObject function.
Dim MyXL As Object
MyXL = GetObject(, "Excel.Application")
should get you a reference to the currently running instance of Excel.
In the code I created an object that is an Excel Workbook. I then set the created workbook as the ExcelApp.ActiveWorkbook. Then I was able to open the file without another workbook being created.