Excel Quirk - Tab stops working when protect another sheet - vba

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.

Related

Excel : Printing all workbook but with refreshing (recalculating) each tab before printing it

I am having some issues with printing an excel file i'm using.
The file is made of a mastersheet, and several sheets that contain individual reports (using data in the mastersheet).
The sheets are generated from a template by a macro, and are automatically named. The sheet name is referenced in the sheet, and I am using Index/match to pull the data I need from the mastersheet. So this means that the tabs need to be recalculated constantly when I switch between them (and there's a little piece of VBA code that does it perfectly).
My issue is that when I want to print the individual reporting sheets, they are all printed with the same data (because their contents are not refreshed individually before printing).
Is there any way of printing them without selecting them one by one and individually printing them each time ?
I thought about some macro/vba code that would copy paste the sheets as values in another workbook, and then printing this workbook, but I'm not sure if its the optimal or quickest way of doing things. I would appreciate it if someone could help me.
Thanks !
Here's a start for you
Sub S
Dim ws as worksheet
For each ws in worksheets
ws.calculate
'or call your recalc routine
ws.printout
next ws
end sub

Stop Excel from changing active sheet on Sheets.Visible = True command

I have a large workbook with many sheets used for background calculation that I hide when they are not in use. The workbook is locked down (no ribbon, sheet tabs, formula bar, etc) so it cannot be tampered with when it is in use.
In Excel 2010, everything went smoothly and the active sheet was not changed, but now in Excel 2016 the following Sub changes the active sheet to the "CompCalc" sheet which leaves the user with no way to return to the sheet they need to be on to use the workbook.
Sub MakeSheetsVisible(Status As Boolean)
Dim VarSubName As String
VarSubName = "MakeSheetsVisible"
'***********************************************************
ProtectSheets (False)
Sheets("DATA_INPUT").Visible = Status
Sheets("RAW_DATA").Visible = Status
Sheets("MASTERHISTORY").Visible = Status
Sheets("CompCalc").Visible = Status
'Sheets("Event_Enter").Visible = Status
Sheets("Raw_Chart_Data").Visible = Status
End Sub
This Sub is called at the end of a Sub that is called from another Sub which can be triggered 1 of 2 ways, on a button press or ListView double click. The active sheet is only changed in the above routine when the button is used to call the initial Sub and when the routine is ran continuously (not stepped through with F8).
I can work around this issue by checking the original active sheet at the beginning of the routine and setting it back to the original at the end, but I would like to know why this is happening and if there is a way to stop it without a workaround. Any help is appreciated
It's an annoying bug in Excel 2013/2016 and as far as I know, there is no fix. A workaround I use is:
Set CurrentSheet = ActiveSheet
'Instructions here
CurrentSheet.Activate
I only ever use active sheet if it's needed for something specific. (as per MatthewD)
But surely when you've set all your sheets to visible, you can add another line to make the sheet you want to be active?
Sheets("The one you want").Activate
would do the job? (or maybe .select?)

Unhide, unprotect and run macro from another spreadsheet

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.

VBA macro crashing Excel 2010

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!!

Where Exactly To Store VBA

We receive Excel files daily from our field offices which I have to clean and re-format (about 110 columns and 500 rows-worth) using VBA.
I need to save my VBA as a macro so we can use it to clean up all the workbook we receive by running the macro and saving the edited sheet as a new worksheet by getting the name from UserForm Combobox items.
Where exactly should I store the VBA snippets? I mean when I open the Visual Basic panel, I have these three options:
Running The Code From Microsoft Excel Object :Sheets1(Sheet1)
Running the Code From An Inserted Module
Running the Code From User Form
If I am supposed to use options 1 or 2, how can I call the UserForm for saving the sheet?
I Recomend you to use modules (Option B)
Option C goes with option B, ill explain, you can create a sub in a module in option B, then you can do:
UserForm1.show
In Option B I would writte this code, but before trying this i recomend you to understand a bit more of vba
sub ClearWBs()
'opening workbook
Workbooks.Open Filename:="c:\book1.xls"
'your code
'your code
'below code for saving and closing the workbook
Workbooks("book1.xls").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close
end sub
Use Module:
If your VBA code focusses on data summarization and manipulation I suggest you use a Module.(example is what you have described in your question).
Use Form:
If what you wan't to do requires a GUI(Graphical User Interface) then you'll have to resort to Form where you can design your GUI. (example is if you have many fields that the user needs to fill-up with distinct values in which you provide the choices)
Use Excel Object:
If what you wan't to do has something to do with events happening on Worksheet and/or Workbook, like for example whenever you add sheet the macro runs, or when you open or close the workbook the macro runs then you will have to write the macro in the Excel Object.
This is basically what i have in mind, hope this helps.
If you receive files that do not contain VBA and you need to apply the same code on those files all the time then I propose that you either save that code in your personal workbook.
You can see how to do that here: http://office.microsoft.com/en-ca/excel-help/copy-your-macros-to-a-personal-macro-workbook-HA102174076.aspx
This is nice because you can also tie it to keyboard shortcut or just have it always ready for you to use.
The disadvantage is that it will only be set up per user session per computer. What you can do is have that code all set up in a module and then import it into your personal workbook if you change session or if someone else has to do it.
Once it's done, you will not have to include the module in your files your receive again.