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!!
Related
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.
All,
I just upgraded to Excel 2013 and am running into a strange issue. I have a macro-enabled workbook that has worked successfully for quite some time now. I was updating some of the code and came across this error (32809) when trying to write to a specific sheet. In troubleshooting, I tried this.
Sheets("Summary").Range("G8").Value = "Test"
This resulted in the same error. Then I tried this.
Debug.Print ActiveSheet.Name
Same error. So I selected another sheet on the workbook and debug.print on the name. Worked fine.
Then I tried this.
Debug.Print Sheets(2).Name (This is the sheet number of the problemic sheet)
Same error. It seems to me that there is some form of corruption with this sheet but I am reticent about deleting and recreating. Any suggestions?
Thanks!
While it wasn't a perfect solution, here is how I finally resolved it. I had a user that was still on Excel 2010 make a copy the corrupt sheet within the same workbook, delete the corrupt sheet, then rename the new sheet to the original name. I was then able to use the workbook in Excel 2013 without issue.
I have a Workbook Open Macro which counts the number of rows in a table on opening so that I can summarize how many records were added or deleted during the data entry session:
Private Sub Workbook_Open()
Dim TotalRows As Integer
TotalRows = Worksheets("Data").ListObjects(1).ListRows.Count
If Range("LastRecordOnStart") <> TotalRows Then Range("LastRecordOnStart") = TotalRows
' Stop
End Sub
I started getting the run-time error '32809' with the following line in the code above highlighted in the debugger every time I opened the workbook...
TotalRows = Worksheets("Data").ListObjects(1).ListRows.Count
Rebooting or opening the workbook on another PC still would not fix it. Even opening a previously saved working copy would give the same error - strange. Note:
While coding, I will periodically save the workbook and use windows explorer to copy a snapshot of the workbook where it creates myworkbook copy(1).xlsm, copy(2).xlsm, etc and if I get a corrupted workbook, I rename it myworkbook.bad.xlsm and rename the latest saved working version (e.g myworkbook copy(6).xlsm to MyWorkbook.xlsm with minimal loss.
When this run-time error occurs though, I've had to rebuild the book several times. It only appears to happen when the workbook crashes after a long session of debugging new code with the Userform open and I have to close excel from the Task Manager.
I read the posts here today and was convinced that it was page related when I could list the listrows count on another sheet and if I converted the Listobject on the "Data" worksheet back to a range and recreated the table, I'd still get the run-time error. To cut a long story short, I tried the following:
Open the workbook
When the error popped up, clicked debug
Right clicked on the "End Sub" line of the WorkBook_Open sub and clicked "Set Next statement"
Pressed F5 to allow the code to finish executing
Saved the workbook and it all works now!
I had another crash with the subsequent run-time error and repeated the steps above and it worked a charm again.
I am currently teaching myself VBA. I work with Excel 2010 a lot but only know the basics of VBA.
To help teach myself, I have set a project for myself which is to create a dashboard in Excel where I can click a button and it opens three reports I run each morning at work and runs a macro on each. The process of each report is to
1 - open report
2 - unhide worksheet (worksheet = "Control Sheet")
4 - run macro on the unhidden sheet (macro = "ButtonClick")
5 - hide worksheet
5 - Save and close report
I have managed to get 1 report to open using:
Sub EasierRun()
Dim Location As String
Location = "location/filename.xlsm"
Workbooks.Open(Location).RunAutoMacros (xlAutoOpen)
End Sub
I copied the 2nd to last line from the internet but it doesn't run any macro, it just opens the file. No error messages display. I understand that I need to be specific about the worksheet and macro I want to work with but I'm not sure how to proceed from here. Also, I'm not sure if I need to tell it to unhide the worksheet in the VBA? Finally, would I need to write individual code for each report to open or do I declare the files all at once and then the remainder of the code works universally?
I've googled and read a lot but I can't manage to adapt what I am finding to fit what I need.
Thanks for any guidance.
Unfortunately, for security purposes, you can't force a workbook to enable macros without explicit input from the user.
I just came across a quirk and have no clue why this is happening - I'm using Excel 2010 and have the following situation:
I have 2 sheets - An "input" sheet and a "save" sheet
Save sheet is protected; input sheet is not
I have a VBA Macro that un-protects the save sheet, copies the data across between the 2, then re-protects it
When this macro runs, the input sheet (which was never protected) now has the tab key not working (cannot tab between cells).
To replicate this situation easily, I created a new blank workbook and did the following:
Password protected Sheet2 with the password "Test"
Put in the following simplistic VBA sub:
Sub WeirdQuirk()
Sheets("Sheet2").Unprotect Password:="Test"
Sheets("Sheet1").Range("A1").Copy Sheets("Sheet2").Range("B1")
Sheets("Sheet2").Protect Password:="Test"
End Sub
When I'm in Sheet1 and run this, afterwards the Tab key is no longer working in the sheet.
Any thoughts on why this is happening / how to fix it?
FYI, I did come up with a fix, but it's not what should be done - Hopefully someone else has a better solution that I'll accept, but I do want to post this in case anyone else gets stuck in this situation...
What I ended up putting in to re-enable the tab key was adding in the line of code:
Application.TransitionNavigKeys = False
OR:
Application.TransitionNavigKeys = True
if you want that feature enabled.
Hope it helps other people.
I want to delete the VBA code contained within a sheet through VBA. Currently, I have a code that copies a sheet across to a new workbook and deletes all images from it. However, these images are set to do things on Worksheet_Activate in the code, which then causes an error whenever I flick to that sheet with no images there.
I know I can get rid of modules etc using something along the lines of:
With ActiveWorkbook.VBProject
For x = .VBComponents.Count To 1 Step -1
.VBComponents.Remove .VBComponents(x)
Next x
For x = .VBComponents.Count To 1 Step -1
.VBComponents(x).CodeModule.DeleteLines _
1, .VBComponents(x).CodeModule.CountOfLines
Next x
End With
but that does not delete from the sheet (or the workbook for that matter. Would be interesting to know if that was possible too).
The code itself will need to be valid for Excel versions 2003 through to 2013 (so cannot use the save as xlsx workaround).
Found out the issue. My code works fine, just the computer I was testing it on did not allow access to the VBA project object model (and was running it with an On Error Resume Next earlier on in the code)
Will have to write an exception in to sort that out in such cases.
Thanks to #mehow #nixda #SiddharthRout and #DaveU for the help