Why does Delete works for ActiveWindow but not ActiveWorkbook? - vba

I don't know why this doesn't work...
Sheets("SampleSheet").Select
ActiveWorkbook.SelectedSheets.Delete
But this works...
Sheets("SampleSheet").Select
ActiveWindow.SelectedSheets.Delete
In both situations, the sheet "SampleSheet" was selected, right?

As GSerg commented, it doesn't work because there is no ActiveWorkbook.SelectedSheets property. Someone on the Excel team wanted to make a property that returned an array of all the selected sheets. But where to put it? They could have put it at the Workbook level, but what if the user had two windows open for the same workbook? They could have different sheets selected in each window. Which window to use? So they decided to make it a property of the Window object instead.
A fine idea, maybe, but they could have put it in both places. Both the Workbook and Window object have an ActiveSheet property. When you reference ActiveWorkbook.ActiveSheet it appears to be the same as ActiveWorkbook.Windows(1).ActiveSheet and Windows(1) is always the active window. They could have done the same thing for SelectedSheets and I'd argue that they should have.
In the meantime, you can use ActiveWorkbook.Windows(1).SelectedSheets to overcome the Excel team's lack of consistency.

Related

Selecting and switching to veryhidden Sheet not working properly

I have been writing a few macros lately to navigate around sheets / change visibility / import-export data etc. I normally just embed the macros as buttons on the sheets.
Normally this works well, however, I do keep experiencing an issue where with a macro, or a userform I unhide a veryhidden sheet, select it and exit the macro, or form.
I do this though via:
With Sheets("Sheet1")
.visibile = xlsheetvisible
.activate
.Range("A1").select
End With
When I then try to manipulate the sheet - e.g. type in a cell, delete data from cell, or insert / delete rows using the GUI rather than doing it via code, the operations happen on the original sheet with the button that called the form or macro. rather than the new one....
Is anyone else experiencing this? Am I doing something wrong?
Give worksheet object for cells while referring the editing
for ex... sheets().range().paste
or activate the sheet which you want to manipulate before your code (which manipulates the sheet)
Found the Solution!!!
Upon further investigation it seems like this is only broken in Office 2013. I tried it on Office 2010 and it worked fine.
The solution is to invoke the vbModeless command after Userform.show so that would be:
Userform1.Show vbModeless
Not perfect, if you want the user to dismiss the Userform before going back to the worksheet, but hey -it's a workaround :)
Let's hope Office 2016 will fix the bug (I'll be upgrading later this month)

find out windows in side-by-side mode

The "View Side By Side" and "Synchronous Scrolling" made the comparison of 2 spreadsheets easier than ever. However, there isn't a "Synchronous Switching tab" feature, so if I switch to a different tab in one of the workbooks and continue scrolling, the sync'ed scrolling become quite funny.
Well, I shouldn't complain, because it's all done manually, and I should use this feature wisely.
As a lazy developer, I would like to write some code to dig myself out: Can I write a macro to automate the worksheet switching on the peer window in side-by-side mode?
It breaks down to 2 steps:
how do I know if a window, most likely the ActiveWindow, is in side-by-side mode?
if it is, how do I tell which window is its peer?
I did my homework. It seems Excel is not very programming friendly on this feature. There are 3 methods
BreakSideBySide()
CompareSideBySideWith(WindowName)
ResetPositionsSideBySide()
and 1 Boolean property
SyncScrollingSideBySide
on the Windows collection related to this feature, but are insufficient to solve my questions.
Does anyone have any idea how to achieve this? Or, is it indeed impossible? Thank you in advance.
You can achieve this using the Workbook_SheetActivate event:
http://msdn.microsoft.com/en-us/library/office/ff195710.aspx
if you put this code in your ThisWorkbook Object, then each time you change the active worksheet, it will...
Cycle through all open workbooks with a different name
Look for a worksheet with the same name as the sheet you just clicked on
Activate the worksheet in the other Workbook
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
For i = 1 To Application.Workbooks.Count
If Application.Workbooks(i).Name <> ThisWorkbook.Name Then
Dim otherWB As Workbook
Set otherWB = Application.Workbooks(i)
otherWB.Sheets(Sh.Name).Activate
End If
Next
End Sub
Note that this requires the worksheet to exist in all open workbooks. An error will result if it does not. However, you could easily add error handling to ignore workbooks with unfound corresponding worksheets.
Also note that it's probably best to use this when only two workbooks are open. I have not looked into the other methods you mentioned, but there may exist a way to identify the two workbooks that are currently in side-by-side mode, at which point the code could shed its for loop and become more concise.

Adding code to new excel sheet dynamically

I need some help Regarding VBA.
In My code I add an excel sheet and rename it and add a validation list to it.I need to run some code on changing value in that validation list.And that must run only on change of that particular cell.
If I am not clear please let me know.Please Help me solving this.
Instead of trying to create the individual code for each new worksheet with the Visual Basic Extensibility (see this link for further reading), simply use the Workbook wide event Workbook_SheetChange (you need to place it in the ThisWorkbookmodule).
In this event code first check, if the worksheet which caused the event is one of the newly created worksheets. This can be done most easily, be checking the .Name of the worksheet.
you can use SelectionChange Event and Change event or it is also possible to use the event Thisworkbook module. SheetChange or SheetSelectionChange.

Excel won't show the workbooks worksheets

I have an Excel Workbook containing three worksheets. However all sheets seem to be hidden dispite that their visibility is set to xlSheetVisible (Found by looking at the worksheet properties in the VBA editor).
This is what I have tried so far to solve the problem.
Searched Google for help, but all similar problems have been solved by changing the Visibility from xlSheetHidden to xlSheetVisible. But in my case that setting is already set to xlVisible.
By using VBA I have tried to activate a sheet without any result.
By reading this you might have guessed that I am able to see and access the worksheets from the VBA editor.
Despite that looking at the normal Excel userinterface, all buttons are disabled and clicking at the office/file button in the topleft corner doesn't allow me to save the workbook. However if I make any changes to the VBA itself and tries to close the workbook then Excel asks whether or not I would like to save the workbook.
Everything you describe leads me to believe you're hiding the workbook window, not the worksheets. When a workbook window is hidden, all the sheets become hidden and much of the functionality from the toolbar greys out.
In the Visual Basic Editor, you'd also be able to see all of the worksheets and their visibility would be xlVisible, yet they cannot be seen on the screen. This is because they ARE visible, but the window which displays them is not.
Please try clicking the View tab in the toolbar, and then under the Window pane, click Unhide.
If this doesn't work, then try toggling visibility of the workbook.
It sounds like this may really be your problem...
http://vbadud.blogspot.co.uk/2010/08/hide-sheet-tabs-using-vba-hide-excel.html

Multiple instances of a single Userform / the need for pointers

I have created a Userform that lets the user pick a worksheet from a list of open worksheets. I need this Userform in various places of my macro, for various purposes; for example, to specify the worksheet containing "sales" data, and to specify the worksheet containing "stock" data. The objects wsSales and wsStock should point to the appropriate worksheets, so that my macro can later work on these.
How can I give each instance of my Userform a different behavior? One instance should set wsSales to the user-selected worksheet, the other instance should set wsStock to the user-selected worksheet.
I have thought about putting several "OK" buttons on my Useform, and making only one visible per instance. Each OK button would have different code. But that's not very elegant, because I would have to put as much "OK" buttons as there are possible uses for my Userform and it would become quite a mess in more complex situations. So, ideally, I would use a property of my Userform as a pointer to either wsSales or wsStock, but I've read that pointers don't exist in VBA. Then, what's the best way to have two instances of a Userform behave differently?
Are you actually declaring variables as instances of the userform? If not, you should. You might want to look at this post: yoursumbuddy.com/a-flexible-vba-chooser-form.
I am using a kind of common approach by means one can practically use any kind of information to create and uniquely identify one of multiple instances of a UserForm - which doesn't even require a variable for an instance. See https://warbe-maker.github.io/vba/common/2021/11/09/UserForm-Instances.html.