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
Related
I use two screen monitors. The left monitor has an Excel Workbook opened. The right monitor has a Microsoft Word document opened. I use a Word Userform to bring data from the opened workbook to the opened Word document.
But sometimes I need to change the activated/selected worksheet (I mean, the one that is in front of the others; th3e one I can see), and I would like to do so from a button in my Microsoft Word userform.
The code below does not work, and I have no idea how to achieve that goal.
Please, would someone have an idea how to achieve that?
Sub ACTIVATE_WORKSHEET_ASDF()
Dim MyExcel As Excel.Application
Dim MyWB As Excel.Workbook
Set MyExcel = New Excel.Application
Set MyWB = MyExcel.Workbooks.Open("MY EXCEL WORKBOOK FULL PATH")
Dim ASDF As Worksheet
Set ASDF = MyWB.Sheets("ASDF")
ASDF.Select 'DO NOT WORK
ASDF.Activate 'DO NOT WORK
Debug.Print ASDF.Cells(1,1) 'DOES WORK!
MyWB.Close False
Set MyExcel = Nothing
Set MyWB = Nothing
End Sub
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")
I am trying to rename a copied worksheet in Excel using word VBA. When I try to set my worksheet variable to the new worksheet it tries to open Microsoft Outlook 2016. With each line after that (stepping through) it opens a dialogue box asking me to create a new Microsoft Outlook account.
The code does successfully create the copies of the template sheet but it fails to rename them.
** Edited to clarify the real issue and to add "Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn) 'Opens Excel" which was already in the workbook macro when the problem occurred.
Dim oXL As Excel.Application 'Requires loading "Microsoft Excel 16.0 Object Library" from Tools -> References
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oXL = New Excel.Application
Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn) 'Opens Excel
oXL.Visible = True
For i = LBound(seller_names) To UBound(seller_names)
With oXL.ActiveWorkbook
.Sheets("Template").Copy After:=.Sheets(.Sheets.Count)
Set oSheet = Sheets(.Sheets.Count) 'set worksheet to be the template copy
If Len(seller_names(i)) > 31 Then
oSheet.name = Left(seller_names(i), 31)
Else
oSheet.name = seller_names(i)
End If
End With
Next i
The line "Set oSheet = Sheets(.Sheets.Count) is where the open Outlook bug occurs. Both lines oSheet.name = run without generating errors but fail to rename the worksheet.
I fixed this issue by changing
.Sheets("Template").Copy After:=.Sheets(.Sheets.Count)
to
.Sheets("Template").Copy After:=.Sheets("Template (2)")
I am opening an excel workbook from excel and delete a sheet inside. I want to do this without the message from Excel:
"Excel will permanently delete this sheet, do you want to continue"
However I cannot make the "DisplayAlerts = False" work correctly.
Public Sub xportQuery()
Dim appExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim PathDaily, FileName As String
PathDaily = Forms!Menu!Text69
FileName = Forms!Menu!Text84
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open(PathDaily & FileName)
appExcel.Visible = True
'Set appExcel = Nothing
Set myWorkbook = Nothing
appExcel.DisplayAlerts = False
Workbooks(FileName).Sheets("Sheety").Delete
appExcel.DisplayAlerts = True
End Sub
Writing it this way I get a "Subscript out of range" on the Sheets("Sheety").delete
If I take of the two DisplayAlerts lines, the code works but with the alert.
How do I write my code correctly to work without any alert message?
Note: DoCmd.SetWarnings didn t work either as the message is displayed in Excel
Thanks To Sam's Comment:
it works either by changing
Workbooks(FileName).Sheets("Sheety").Delete
into
myWorkbook.Sheets("Sheety").Delete
or
AppExcel.Workbooks(FileName).Sheets("Sheety").Delete
However the rest of the macro can still use Workbooks(Filename) without the "AppExcel." Reference
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.