I am retrieving data from another sheet using vlookup function, the source file opened in background mode, below is the code i used for hide the excel opening and screen update
Application.ScreenUpdating = False
app.Visible = False
But without above line vlookup working fine, when open in the invisible mode only vlookup function not working.
partner = "C:\K\KDt\Con.xls"
Set pf = app.Workbooks.Open(partner)
Set ps = pf.Worksheets("Con")
ThisWorkbook.Sheets("KQC").Range("H4:H" & k) = "=IFERROR(VLOOKUP(A4&""SP"",Contract_PARTNERS.xls!$H:$R,4,),"""")"
The issue is cause by how Excel treats links between workbook references. Your code is creating a link to a file that Excel expects is not open and is therefore incorrect. Even if it is not visible it is open.
To reference another workbook you do not have to have that file open at the same time.
Example formula when file is open:
=VLOOKUP(A1,'1.csv'!$A:$I,9,0)
Same formula when file is closed
=VLOOKUP(A1,'C:\Users\nable\Documents\csvs\[1.csv]1'!$A:$I,9,0)
Excel should auto make the adjustment when the file is open/closed if the link was established at the time the formula was put in.
Related
I am writing a macro in VB in excel 2013. The only remaining bug left is when the macro runs, all of the other workbooks i have open close, even ones unrelated to the macro. I do not want users of my macro to lose other tabs of excel they may have open if they run my program. I have tried using "Application.Visible = true" but it is not giving me the desired behavior. Is there a way to run a macro without affecting any other currently open workbooks? Or is it possible that something else I have written is overriding the Application.Visible method? If someone could provide a recommendation it would be much appreciated.
Application.Visible = false
Simply does what it says, set the application "Excel in your case" to invisible.
If you want to hide something specific you have to call this:
e.g Sheet
Sheets("Whatever").Visible = False
e.g Workbook
ActiveWorkbook.Windows(1).Visible = False
//ActiveWorkbook needs to have an reference to the specified Workbook which you get as an example if you store the opening of the workbook (ActiveWorkbook = Workbooks.Open )
It sounds like you have a line
Workbooks.close
(which will close all workbooks) when what you wanted to do was close the specific workbook with the macro. That should be
ThisWorkbook.Close
I have a strange problem, I suscpect it's connected to the version of the Excel, but I'm not sure at all. I can't figure it out alone so I need your help. I have a macro, which operates on a fresh workbook - it's not saved anywhere, as the worker will save it manually afterwards. The macro is a .xlam format add-in, adding a couple of buttons to the ribbon and these buttons start the code.
Inside the code I have simple lines for opening a new workbook, chosen earlier by an user:
Application.DisplayAlerts = False
Set wbMPA = Workbooks.Open(MPA_file)
Application.DisplayAlerts = True
Earlier, the code sets active workbook as an object/workbook the macro will mainly work on (tried both versions):
Set dwb = Application.ActiveWorkbook
and later in the code
dwb.activate
OR:
dwb = ActiveWorkbook.Name
and then
workbooks(dwb).Activate
The lines are in separate subs, but the variable is globally declared.
The code works fine until the opening of wbMPA (watching it in the locals all the time). When I try to open the new file with the code above, the earlier workbook (dwb) just closes itself from unknown reasons.
The error I get from the 1st method is this:
error screenshot
The second one gives a simple "Subscipt out of range".
The errors, however, are not a problem. The problem is the cause of them, which is closing of the workbook from unknown reasons.
It happens only when I open the completely new workbook (using the excel icon on the Start bar) - when I do it from File -> New -> Blank Workbook using already opened workbook, the error does not occur.
Another strange thing - me and my colleague from work use 2013 version of Excel. I never have this error, she has it every time.
This is a general scheme of the code, other things are meaningless in this case because there are no other manipulations of workbooks/worksheets.
Dim dwb As Object
Dim wbMPA As Object
Sub_1()
Set dwb = ActiveWorkbook
Set wbMPA = Workbooks.Open(MPA_file)
Call Sub_2
End Sub
Sub_2()
dwb.Activate
End Sub
I get an error on the activation of dwb in Sub_2, because it closes itself for God knows what the reason on the opening of wbMPA in the Sub_1.
If you have only opened a blank workbook (clicking Excel from Toolbar, for example) and then you open any named workbook before making any changes to the blank workbook, the blank workbook will disappear. I believe that is normal/expected behavior.
I can't speculate why this happens on one computer but not another, but this is always how I have observed new/blank documents (Excel, PowerPoint, Word) to behave, and assume this to be the normal behavior. You may have some different option/configuration on your Excel environment which is changing this default behavior, or maybe you are slightly altering the blank file before running the macro, and your co-worker isn't, etc.
A word of caution to avoid relying on ActiveWorkbook -- and especially in this case if the expectation is to always Set dwb to a new/blank workbook, the best way to do that is to explicitly create a new/blank workbook, rather than relying on the user to manually open a new/blank target workbook.
Set dwb = Workbooks.Add
If, on the other hand dwb must be assigned to some other known/existing workbook, then you should be either providing the file path to an Open statement, or the workbook name to the Workbooks collection.
On a related note, it's almost never necessary to Activate a workbook, see here:
How to avoid using Select in Excel VBA macros
And further note: your variables aren't globally scoped, they're scoped only to the module using Dim statement. A public declaration uses the Public keyword, not the Dim keyword. Both module-scoped and global-scoped should be used with caution (Public moreso than module-scoped) and in most cases it's preferable to pass objects/variables by reference to dependent subs and functions:
How to make Excel VBA variables available to multiple macros?
I'm trying to get an Excel file using BindToMoniker. For this question I'm starting with the no running instance of Excel (no workbooks open). It opens one Excel instance with two Excel windows shown. The one is entitled Excel and the other is titled with my workbook name. Neither of them show any open workbook.
Using Windows 10 64 bit with Excel 2016.
Dim wb As Excel.Workbook = System.Runtime.InteropServices.Marshal.BindToMoniker(PathName)
Dim xlApp As Object = wb.Parent
xlApp.Visible = True
'error here
xlApp.Windows(wb.Name).Visible = True
I should be able to unhide the window using xlApp.Windows(wb.Name).Visible = True instead I get a COMException: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) Just to verify I click on either window and in the View tab click Unhide. It shows the exact wb.name. I click OK to unhide and it does so just fine with the second window disappearing. Yet if I try to do that in the code it throws an error. So why does my code throw an error and/or how can I fix it?
EDIT:
This also throws an error "invalid index"
xlApp.Windows(1).Visible = True
EDIT 2:
This works
I break the code at xlApp.Windows(1).Visible = True
Click on the Excel window entitled with wb.name
Execute the line xlApp.Windows(1).Visible = True
My workbook is unhidden and all is well. But if I click on the other window entitled "Excel" then it doesn't find any windows and my workbook stays hidden. I don't understand why it's opening two windows. That seems to be a problem.
I tried restarting my computer; no change.
So why am I getting two windows and why can't it find the hidden workbook to unhide it? What's the solution?
I have an Excel 2010 spreadsheet that I use to maintain tests for an automated test application written with Selenium WebDriver. The spreadsheet has 6 ActiveX buttons that Upload (to a database), Import (from another sheet), Clear All Sheets, Export (to another sheet), Merge (two sheets), and Restore (a test suite from the database). My problem seem to stem from the macro that runs when the Import button is selected
The macro takes two sheets, source and destination, clears the destination sheet and copies the cells and data from the source workbook to the corresponding sheet in the destination workbook. From there, it can either be uploaded to the database or run from the sheet by the testing app
A couple weeks ago when selecting the Import button Excel crashed. I looked on the web and found a solution that worked. It involved disabling all macros, shutting down trust documents and trusted locations, recompiling, and reopening the workbook. This worked for a couple weeks until the same thing happened again this past weekend and the fix did not work anymore.
Here is the code I have:
numSheets = wrkBook.Sheets.Count
appName = wrkSheet.Cells(2, 3).Value
bkPath = Application.GetOpenFilename("Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
If bkPath <> "False" Then
Call Clear_All(wrkBook, wrkSheet)
'get the number of sheets in the source workbook
destCnt = wrkBook.Sheets.Count
Set importBook = Workbooks.Open(bkPath)
For a = 1 To destCnt
thsShtName = wrkBook.Worksheets(a).Name
'activate the source workbook
importBook.Activate
'get the numer of sheets in the source workbook
srcCnt = importBook.Sheets.Count
'loop through each sheet checking title
For b = 1 To srcCnt
'get the name of the 'a' sheet
**srcSheetName = importBook.Worksheets(b).Name**
'do more stuff with the source sheet
Next b
Next a
End If
The code is crashing on the following line
srcSheetName = importBook.Worksheets(b).Name
Here are a few things that I have noticed. Once the importBook variable is set, I cannot expand the variable in a watch window even if the code has not reached the offending line. However, the macro will successfully get the srcCount (number of sheet on the source sheet). Once the macro gets to the offending line it crashes consistently with a message stating simply that Excel has encountered an error and needs to close . No information, Excel closes, and sometimes restarts to a blank workbook
OK...I figured this out myself after heeding Siddarth Rout's suggestion about a corrupt spreadsheet. I took the steps that I described above
So here's what I did.
I copied one of the other spreadsheets and cleared it. As soon as I ran the Import macro, it passed through the line where it was failing before, so a corruption seems to have been the issue. I then stepped through the original code and rewrote the Import macro to simplify the code in it. It now seems to be working just fine
Thanks for the suggestions!!
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.