Activate a worksheet to a specific section - vb.net

I would like to activate a worksheet so that it always open's looking at specific cells.
The worksheet I have has some other bits out of view that the user doesn't need to see, so I wanted to make sure when the sheet activated it shows the area that the user needs to interact with.
If you need anymore calrification please ask.Thank you

Creating an event within the sheet in question would cause some code to run each time the sheet is activated like so:
Private Sub Worksheet_Activate()
Me.Cells(4, 6).Activate
End Sub
In this case, the cell F4 (4th row, 6th column) would be activated - but you can adjust this as you need.

Related

Resetting Filters using VBA

I have a short piece of code to reset the filters applied to the columns on my worksheet.
Sub ResetFilters()
If ActiveSheet.FilterMode Then
Cells.AutoFilter
End If
End Sub
When I click the command button to run the macro, it works but the 'Reset Filters' command button disappears and the rest that I have on the sheet all stack up in the top left hand corner of the worksheet. They all have 'Don't move or size with cells' set as their property but they still move...
Command Buttons - Before resetting filters
Command Buttons - After resetting
When I enter Design Mode, the 'Reset Filters' button reappears in the correct place... I am very confused - does anyone have any advice they can offer?
EDIT (14/06 16:41) - I have discovered that if I sort the data into ascending or descending order, the buttons stay where they are when I reset the filters. They only move if I have filtered out some of the data.
EDIT (14/06 17:58) - I have included a screenshot of what I am working with so that my filtering needs are clearer! I work in a school and teachers will analyse their data with this tool. They can filter any of the columns to analyse results and progress of specific children based on their characteristics or assessment results. As they can filter or sort many columns, it can be hard to reset the data without missing a column. I would like to add a 'Reset' button to make this job easier. (I will now likely add these to the ribbon as I have just created a bespoke school tab for this tool so that will remove the issue of the moving buttons. However, if there is a better method of resetting the filters, I would love to learn it.)
Screenshot of the data analysis tool
Many thanks.
Don't use ActiveSheet. It is prone to multiple errors.
Use Dim ws As Worksheet: Set ws = Sheets("Sheet1") instead (change Sheet1 to the actual Sheet name that you are using)
If you are willing to remove literally all filters (and not only over a specific range), this will do
Private Sub clearfilters()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
ws.Cells.AutoFilter
End Sub

I need to be able to sort a protected worksheet that has locked column headers in excel (VBA)

I am working on an excel document at the moment and everything is turning out quite nicely. However, I am coming down to the very last issue that I am having a problem resolving: being able to sort my columns in ascending/descending order while the worksheet is protected and the column headers are locked. I will state some facts about my worksheet first, the goal that I am trying to accomplish, then possible solutions that I have researched and why those solutions do not seem to apply to my situation.
First, I am NOT using an excel table object (just plain
rows/columns).
The top row has AutoFilter applied (to work as column headers).
All of the cells in the worksheet are unlocked, EXCEPT for the
entire first row which is locked (aka, the column headers).
The worksheet will be protected.
I do NOT want users to be able to edit data in the first row (this
is important, these must not be editable no matter what).
For my protected sheet settings, I have "Select locked
cells","Select unlocked cells","Insert rows","Delete rows","Sort",
and "AutoFilter" checked.
I am using VBA for my worksheet.
I am using Excel 2013
Now, assuming the worksheet is protected, users are currently able to use the AutoFilter at the top to actually "filter" the data as intended. The issue is whenever they try to "sort" the data in ascending/descending order that I get an error saying that you must unprotect the sheet first.
After researching I have seen that this is due to the fact that when you sort, the AutoFilter automatically counts the column header as part of the range being sorted... but because this column header (row 1) is locked, it is causing this error. However, this row HAS to be locked, my VBA code specifically reads the values in these column headers and under no circumstances can they be changed.
So filtering works just fine, it is just the sorting I am trying to figure out now. My "ideal" solution would be to somehow capture an event when a user clicks on the AutoFilter arrow and selects "Sort" where I can then, in VBA, unprotect the sheet, sort according to their selection, then protect the sheet again. However, again upon research, it seems that there really isn't an option when it comes to an event for this AutoFilter button (I could be wrong, sometimes it can be confusing reading other's suggestions).
I am hoping someone out there can help me out with this situation, I would also LIKE to avoid using an excel table object, however if it is the only solution that works that meets all my above criteria then so-be-it.
Thanks in advance for your help.
If you already use VBA, you can plug this code into the Sheet module. This assumes the headers are in row 1. Change to suit.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("1:1")) Is Nothing Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
End If
End Sub
The sheet now does not need to be protected.
Borrowing a bit from #Teylyn's and adapted it to disallow selection of the header row, when it is selected, it selects the cell below it directly and displays an error message. The code sits in the sheet module, if it needs to be applied to more sheets, it may be better to use Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range), in that case, replace all instances of Me with Sh in the code below.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Me.Range("1:1")) Is Nothing Then
Target.Offset(1, 0).Select
MsgBox "You cannot select or edit the headerrow, please use the autofilter button to sort and filter.", vbCritical, "Invalid Selection"
End If
End Sub
Note
Unlocking the cells which is required for the sorting and then allowing unlocked cells to be selected when protecting the worksheet, which is needed to be able to edit the data, makes the worksheet protection effectively useless.
Also wit the code above, there is still a loophole through which cells can be edited. If you select an allowed cell, drag it to the header row, and choose to replace the data in the cell then the header cell to which the allowed cell was moved is selected and editable.
End of note
Select the data range which needs to be sorted and unlock the cells.
When protecting the worksheet, be sure to allow
sort
autofilter
To prevent users from changing the cell contents, disallow the selection of unlocked cells.

Auto-update (calculate) the Excel sheet when a cell is changed

I am using Excel 2010 and I want that the Excel sheet will be updated (calculated) automatically when the value of a cell (Range("B7")) is changed (The Range("B7") is a drop-down list). I am trying to use the intersect-target method like:
Private Sub Worksheet_Change(ByVal target As Range)
If Not Intersect(target, Range("B7")) Is Nothing Then
ActiveSheet.Calculate
End If
End Sub
I have two questions about this:
Does it matter if I put this Private Sub under any module? Or am I supposed to write it in the exact sheet under the Microsoft Excel -object menu in VBA?
The second question is how I can make this work? The sheet doesn't update (calculate) automatically when I change the Range("B7"). It only updates when I save the Excel file.
The function should be in the sheet which B7 belongs to, e.g. if the B7 you are updating is in "Sheet1" then the code for the Worksheet_Change event should be under Sheet1 too. You can right click the tab for the sheet, and select show code to get to the right place in VBE.
Excel should be aware that any cell using B7 needs to be recalculated when changed, so that should be handled automatically.
Your code will work if you place it in the sheets event not in Modlue please find the below image for your reference.
Select the respective sheet in VB window and double click on it, it will open the worksheet event window (code window) where you need to copy paste the codes. Now try changing the validation cell it should work :)..

Excel VBA - how to prevent a user changing worksheets conditional on certain criteria

I'm currently creating a workbook which has an input tab, all of it's data flowing through into later tabs. I want to prevent a user from moving onto any other tabs until all of the relavent information is filled in on the input sheet.
I'm currently trying to use the workbook_sheetactivate event but have spent a lot of time going between this and worksheet_change event, neither of which I can get working properly. Any help would be greatly appreciated.
If you don't want to use forms then I would suggest adding code similar to the following to each sheet:
Private Sub Worksheet_Activate()
If Worksheets("Sheet1").Range("A1").Value = "" Then
Worksheets("Sheet1").Select
End If
End Sub
Obviously you will need to change the sheet names, range and value but I'm sure you get the idea.

How do I run VBA code on ListObject Change?

I know that I can write the following code in a sheets' VBA-object to run code on a worksheet change.
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Is there anything similar that I can write to run code whenever I filter a certain ListObject?
Only in some cases. Say we include a new column in the table that we fill with the value 1. Elsewhere we insert an
=SUBTOTAL()
formula to sum that column. As the filter is operated the number of visible rows will vary. The SUBTOTAL() function will re-calculate.
At this point a Calculate Event macro would catch the re-calculation!
Try this...
I added a ListBox Form Control to my Excel sheet..
Next, I assign a macro to this object by Right Clicking and choosing "Assign Macro". (you may need to be in "Design Mode" to make this happen -- check the Developer Ribbon)
By default - the Macro Name is populated with a change event macro name. Click "New" to create the macro.
Your macro is added to a Module. Hope this helps!