I want to get the access of the data in a workbook in another repertory but I don't know the syntax, can you help me please ?
I have tried something like that but it doesn't work
Workbooks("U:\a.xlsx").Sheets("a").Range("A2")
To read a value you need to open that file first.
Dim MyWb As Workbook
Set MyWb = Workbooks.Open(Filename:="U:\a.xlsx", ReadOnly:=True) 'readonly if you only need to read
'read the value
Debug.Print MyWb.Worksheets("a").Range("A2")
'close it after reading
MyWb.Close SaveChanges:=False
Or if you want to do it hidden in the background without showing the workbook:
'open a new hidden Excel
Dim ExApp As Excel.Application
Set ExApp = New Excel.Application
ExApp.Visible = False
'open the workbook in that hidden ExApp
Dim MyWb As Workbook
Set MyWb = ExApp.Workbooks.Open(Filename:="U:\a.xlsx", ReadOnly:=True)
'read the value
Debug.Print MyWb.Worksheets("a").Range("A2")
'close it after reading
MyWb.Close SaveChanges:=False
'close hidden Excel
ExApp.Quit
Here a proper error handling might be useful to ensure the ExApp is closed in case of any error. Otherwise the process will stay open.
You'll need to open the workbook to access it.
Like:
Dim otherwb as Workbook
Set otherwb = Workbooks.Open("U:\a.xlsx")
Dim otherwbRange as Range
Set otherwbRange = otherwb.Sheets("a").Range("A2")
You need to create an Object for the workbook first.
Try something like:
Dim excel_wb2 As Excel.Workbook
Set excel_wb2 = Excel.Workbooks.Open("U:\a.xlsx")
x = excel_wb2.Sheets("a").Range("A2")
Related
I have a macro in my Outlook that whenever I receive an e-mail with a certain subject, it automatically opens an Excel workbook and pastes a portion of the e-mail subject in a specific cell in one of the worksheets. It works perfectly. Now I need to do this exact same process but pasting this information in an already opened workbook, instead of opening a closed file.
I've tried different solutions from my limited Excel VBA knowledge (ActiveWorkbook, worbooks(filename).activate, etc.) but none of that worked and I have not found anything similar online, as most macros are written as being run from an Excel file and not Outlook.
This is part of our current code, that opens the file and pastes the e-mail subject (which is the "ticker" value) in a specific cell on the "Lista Empresas" worksheet. What I need is a code that does the same, but in an workbook that is already opened (let's call it "test1").
Dim exapp As Excel.Application
Dim ExWbk As Workbook
Dim ExWbk2 As Workbook
Set exapp = New Excel.Application
Set ExWbk2 = exapp.Workbooks.Open("\\XXX\ListaEmpresas_ajustado.xlsm", UpdateLinks:=0)
exapp.Visible = True
ExWbk2.Sheets("Lista Empresas").Range("P2").Value = ticker
ExWbk2.Sheets("Lista Empresas").Range("P3").Calculate
There are a few scenarios to handle here. First, is Excel running? If no, then do what you are doing already. If yes - is the correct workbook open? If yes - return it, otherwise open it.
Dim ExWbk2 As Workbook
Dim wb As Workbook
Dim exapp As Excel.Application
On Error Resume Next
Set exapp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set exapp = Nothing
End If
On Error GoTo 0
If exapp Is Nothing Then
' Excel is not running
Set exapp = New Excel.Application
Set ExWbk2 = exapp.Workbooks.Open("\\XXX\ListaEmpresas_ajustado.xlsm", UpdateLinks:=0)
ExWbk2.Visible = True
Else
' Excel is running, but is the right book open?
For Each wb In exapp.Workbooks
Debug.Print wb.Name ' <-- This will help you find what to look for
If wb.Name = "ListaEmpresas_ajustado" Then
' Yes, it is!
Set ExWbk2 = wb
End If
Next
If ExWbk2 Is Nothing Then
' No, it wasn't
Set ExWbk2 = exapp.Workbooks.Open("\\XXX\ListaEmpresas_ajustado.xlsm", UpdateLinks:=0)
End If
End If
The trick to find out if Excel is running is GetObject. It will fail if it can't find it.
The for loop is there to allow for finding the correct workbook, based on the name. Adjust as needed.
The following code gets the object if you know the name of the sheet currently active in Excel instance. I guess this could be got from the application title using the first bit of code.
Dim exapp As Excel.Application
Dim ExWbk As Workbook
Dim ExWbk2 As Workbook
Set exapp = GetObject("ListaEmpresas_ajustado.xlsm").Application
exapp.Visible = True
ExWbk2.Sheets("Lista Empresas").Range("P2").Value = ticker
ExWbk2.Sheets("Lista Empresas").Range("P3").Calculate
Sub test()
Dim objXL
Set objXL = CreateObject("Excel.Application")
Dim FileName As Variant
Dim UpdateLinks As Variant
Dim ReadOnly As Variant
With objXL
.Workbooks.Open(FileName:= "test.xls",UpdateLinks:=0,ReadOnly:=False)
.Visible = True
End With
Set objXL = Nothing
End Sub
Here is the code I used to open an Excel file in Macro, but it reported a syntax error in the 8th line with Workbooks.Open() function. I tried but could not figure out where I got wrong. More information about Workbooks.Open() function can be found here: http://msdn.microsoft.com/en-us/library/office/ff194819(v=office.15).aspx
Thanks for your attention!
You don't need the () in a method call (unless you are assigning the result). Use
.Workbooks.Open(FileName:= "test.xls",UpdateLinks:=0,ReadOnly:=False)
That said, why are you you creating an application object? If this is coded in Excel this works
Dim wb As Workbook
Set wb = Workbooks.Open(FileName:="test.xls", UpdateLinks:=0, ReadOnly:=False)
or just
Workbooks.Open FileName:="test.xls", UpdateLinks:=0, ReadOnly:=False
Although it is better to specify the full path to your file.
.Workbooks.Open FileName:="test.xls", UpdateLinks:=0, ReadOnly:=False
I have an excel file that was created by vb6 application and after I save it, I want it to be printed into the default printer..,
Tnx, any help would be appreciated.
Private Sub Command1_Click()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSH As Excel.Worksheet
'open excel application
Set xlApp = New Excel.Application
'Open excel workbook
Set xlWB = xlApp.Workbooks.Open(FileName:="C:\YourFile.xls")
'There are two ways to access specific worksheets
'By index number (the first worksheet in this case)
Set xlSH = xlWB.Worksheets(1)
'or by the Sheet's Name
Set xlSH = xlWB.Worksheets("TestSheet")
PrintSheet xlSH, "MyFoot", "MyHead"
'Close workbook (optional)
xlWB.Close
'Quit excel (automatically closes all workbooks)
xlApp.Quit
'Clean up memory (you must do this)
Set xlWB = Nothing
Set xlApp = Nothing
End Sub
Sub PrintSheet(sh As Worksheet, strFooter As String, strHeader As String)
sh.PageSetup.CenterFooter = strFooter
sh.PageSetup.CenterHeader = strHeader
sh.PrintOut
End Sub
Yet, to answer your question, you can use :
ActiveWorkbook.PrintOut Copies:=1, Collate:=True
and you can find much information here : http://www.exceltip.com/excel_tips/Printing_in_VBA/210.html
Anyway, i insist, you should accept answers from your previous questions or people won't care answering your new ones.
Max
How do I edit excel spreadsheets from word using VBA?
First you need to set a reference to the version of Excel you are running. In the VBE go to Tools>References and click Microsoft Excel 12.0 Object Library (12.0 for 2007, 11.0 for 2003) etc.
Then you can code something like this (opens a new instance of Excel, opens, edits and saves a new workbook). You'd use GetObject to access a running instance of Excel:
Sub EditExcelFromWord()
Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set appExcel = CreateObject("Excel.Application")
With appExcel
.Visible = True
Set wb = .Workbooks.Add
Set ws = wb.Worksheets(1)
ws.Range("A1").Value2 = "Test"
wb.SaveAs ThisDocument.Path & Application.PathSeparator & "temp.xls"
Stop 'admire your work and then click F5 to continue
Set ws = Nothing
Set wb = Nothing
Set appExcel = Nothing
End With
End Sub
The code snippet below is intended to open a specific worksheet of a specific XL file and make it the active worksheet. However, the debug.print gives an output of different sheets. Shouldn't objws.activate make objws the active worksheet?
Dim XLApp As New Excel.Application
Dim ObjXL As Excel.Workbook
Dim ObjWS As Excel.Worksheet
Set ObjXL = XLApp.Workbooks.Open(TargetXL)
Set ObjWS = ObjXL.Worksheets(TargetWS)
ObjWS.Activate
Debug.Print ObjWS.Application.ActiveSheet.Name & "," & ObjWS.Name
Your debug line is printing the name of the active sheet of the application, which may be a sheet in a workbook other than the workbook you have just opened. If you activate that workbook first, then the application's active sheet should become the sheet in that workbook.
I.e.
Dim XLApp As New Excel.Application
Dim ObjXL As Excel.Workbook
Dim ObjWS As Excel.Worksheet
Set ObjXL = XLApp.Workbooks.Open(TargetXL)
Set ObjWS = ObjXL.Worksheets(TargetWS)
ObjXL.Activate ' add this line
ObjWS.Activate
Debug.Print ObjWS.Application.ActiveSheet.Name & "," & ObjWS.Name
You may also want consider whether you really need to activate the worksheet. If the user specifically needs to be viewing the sheet after your VBA has run, then this is a valid reason. If however, you only need to act on the sheet in your code, then you can just reference all it's properties by using the ObjWS variable without actually activating it.
Try Select() instead.