Specifying sheet to freeze panein excel - excel

I am trying to freeze panes using code in excel. i have seen several examples on how to do it with the activewindow, im trying to
sepcify it from a button on a different sheet.
I have 2 sheets, "Time" and "Time_and_Cost", i have a button on the "Time" sheet which i want to freeze the top row of
"Time_and_cost".
I have this code currently for freezing panes of the active window.
Rows("1:1").Select
ActiveWindow.FreezePanes = True
However, how can i adapt this to specify the worksheet, i assume i have to select the worksheet to freeze, freeze it, then
select previous worksheet? im having trouble find the exact code to use.
Thanks in advance

How about something like this:
Public Sub FreezePane()
Dim shName As String
shName = ActiveWindow.ActiveSheet.Name
ActiveWorkbook.Worksheets("Time_and_Cost").Activate
Range("A2").Activate
ActiveWindow.FreezePanes = True
ActiveWorkbook.Worksheets(shName).Activate
End Sub
Note that I'm activating A2 on "Time_and_Cost" prior to freezing the panes so that the top row will be frozen.

You can try the below.
Sub FreezePanes()
Worksheets("Time_And_Cost").Activate
Rows("2:2").Select
ActiveWindow.FreezePanes = True
End Sub

Related

Hide sheet on click?

I'm a VERY new learner to VBA trying to decypher functions and build an interactive file. With help from one user here (who will certainly know who he is ;)), I could learn quite a few things.
Now I'm stuck at one ridiculous piece of code : I want to 1/unhide a sheet, 2/go to that sheet and 3/re-hide the sheet when the user selects the "back to the start page-button".
So I made this code :
Sub FRtoEN()
'
' FRtoEN Macro
' Emmène au Glossaire FR ==> EN
'
Sheets("synthèse_FR&EN").Visible = True
Sheets("Synthèse_FR&EN").Select
End Sub
and it works well. But I cannot find out how to tell excel in VBA-language that I want it to re-hide the tab once the user is done and clicks the exit button.
Could you help me?
Ferndiando's answer is brilliant when you want to have one button where you first show the hidden sheet and next time you click on the same button it hide the same sheet.
Making one button to show a sheet and another button to hide the same sheet, do the following;
In the first button you will make the code visible:
Sub FRtoEN()
'
' FRtoEN Macro
' Emmène au Glossaire FR ==> EN
'
Sheets("synthèse_FR&EN").Visible = True
Sheets("Synthèse_FR&EN").Activate
End Sub
In the second button that which will take the user back to the "Main Page" you can add this code:
Sub StartPage()
Sheets("Start Page").Activate 'First go to Start page
Sheets("synthèse_FR&EN").Visible = False 'Then hide the sheet they currently visited, that makes the experience a little bit more "working in background"
End Sub
If I assume you use this "back to the start page-button" for several sheets, you could hide other sheets too every time someone goes to start page.
Sub StartPage()
Sheets("Start Page").Activate 'First go to Start page
Sheets("synthèse_FR&EN").Visible = False
Sheets("synthèse_FR&DE").Visible = False 'Example 1 - No matter which sheet you visit, it will hide this sheets.
Sheets("synthèse_FR&SP").Visible = False 'Example 2 - No matter which sheet you visit, it will hide this sheets.
End Sub
If you want the code to perform things on hidden sheets, while they still are hidden for the user (for example background filtering/calculations/copy data etc..) this will give the user a smooth experience:
Sub StartPage()
Application.ScreenUpdating = False 'Turn of all visual updates the macro does. Macro works in background without showing every step visually in Excel.
Sheets("synthèse_FR&EN").Visible = True 'Unhide the sheet you want to work at.
'Do some filtering stuff // copy stuff
Sheets("synthèse_FR&EN").Visible = False 'Re-hide the sheet again.
Application.ScreenUpdating = False 'Turn ON all visual updates the macro does. Macro now works and shows every step visually in Excel.
End Sub
:)
If i understood your question you can use this code:
Sub myButton()
'Hide and Show Sheet2 with same button
' you can change the sheet name as you prefer
If (Sheets("Sheet2").Visible) Then ' control if the sheet is visible
Sheets("Sheet2").Visible = False ' hide sheet2 because before was showed
Sheets("Sheet1").Select ' select sheet1
Else
Sheets("Sheet2").Visible = True ' show sheet2 because before was hidden
Sheets("Sheet2").Select 'select sheet2
End If
End Sub
I hope this help you.

Excel VBA - random runtime errors

I’m very new to Excel VBA but managed to create three buttons in a staff timesheet. All buttons work as needed, however, one particular button is causing random issues – about 90% of the time it works, but from time to time it will crash Excel or give an error such as runtime error '-2147417848 (800 10 108)': Automation error The object invoked has disconnected from its clients. Other times it’s a similar message, saying Method ‘Insert’ of object ‘Range’ failed.
It’s happening in different versions of Excel on different computers. The task is not complex but I’m stumbling with my VBA knowledge.
The user clicks the button to set up each formatted row in the sheet called “Timesheet”, i.e. clicking the button in “Timesheet” copies a row from sheet4 (formatted and containing formulae) and inserts it into the “Timesheet” above the button.
I’d be very grateful if someone could suggest alternative code that won’t crash Excel - many thanks in advance!
Sub NewSlot()
' NewSlot Macro used in Timesheet
'
'turn protection off
Worksheets("Sheet4").Unprotect Password:="mypasswd"
Worksheets("Timesheet").Unprotect Password:=" mypasswd "
' select row 8 in sheet4
Sheets("Sheet4").Select
Rows("8").Select
Selection.Copy
' go back to timesheet
Sheets("Timesheet").Select
' insert copied row
Dim r As Range
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Offset(0, 0).Select
Selection.Insert shift:=xlDown
Application.CutCopyMode = False
'turn protection on
Worksheets("Sheet4").Protect Password:=" mypasswd "
Worksheets("Timesheet").Protect Password:=" mypasswd"
End Sub
If you are going to use VBA to repeatedly modify a protected worksheet, unprotect it then protect it once with UserInterfaceOnly:=True.
sub protectOnce()
worksheets("Timesheet").unprotect password:="mypasswd"
worksheets("sheet4").unprotect password:="mypasswd"
worksheets("Timesheet").protect password:="mypasswd", UserInterfaceOnly:=True
worksheets("sheet4").protect password:="mypasswd", UserInterfaceOnly:=True
end sub
After that has been done once you will not have to unprotect to modify with VBA. If you have to unprotect it for another reason, reprotect it with with UserInterfaceOnly:=True.
This cuts your NewSlot code down significantly. It is considered 'best practise' to avoid using Select and Activate, particularly across worksheets.
Sub NewSlot()
' select row 8 in sheet4
workSheets("Sheet4").Rows("8").Copy
' go back to timesheet
with workSheets("Timesheet")
' insert copied row
Dim r As Range
Set r = .Buttons(Application.Caller).TopLeftCell
.Cells(r.Row, "A").entirerow.Insert shift:=xlDown
end with
End Sub

VBA .ShowAllData fails when a Chart Sheet is active

I have the code below attached to a button on a userform in Excel. There are other parts of the code that perform advanced filtering and I use the code below to clear the filter before re-applying the filter, to make it run much faster. It works just fine as long as wsDataSchool is the active sheet or some other sheet is active. However, if there is a CHART sheet active before I click the button then I get an error saying ShowAllData failed. This is super weird and I can't find anything about it. Any ideas as to why this is happening or how to fix it?
If wsDataSchool.FilterMode = True Then
wsDataSchool.ShowAllData
End If
Add a test maybe then. This tests if current sheet is a chart sheet and if so then activates the target sheet by codename.
Option Explicit
Public Sub test()
If wsDataSchool.FilterMode Then
Select Case ActiveSheet.Type
Case -4167
wsDataSchool.ShowAllData
Case Else
wsDataSchool.Activate
wsDataSchool.ShowAllData
End Select
End If
End Sub
Although ugly, if you then need to return to a Chart sheet if in a chart sheet you could as follows:
Public Sub test1()
Application.ScreenUpdating = True
Dim ws As Chart
If wsDataSchool.FilterMode Then
Select Case ActiveSheet.Type
Case -4167
wsDataSchool.ShowAllData
Case Else
Set ws = ActiveSheet
wsDataSchool.Activate
wsDataSchool.ShowAllData
ws.Activate
End Select
End If
Application.ScreenUpdating = False
End Sub

clock in and clock out system

hi there im creating a spreadsheet to use for clocking in and out of work and have a simple wee GUI set up already. along with this i have the current time showing as soon as i start up the worksheet but it shows on every page i was wondering how to stop this?? sample code below:
Global clockOn As Boolean
Sub runClock()
Range("M15").Value = Now()
If clockOn = True Then
Application.OnTime Now + TimeValue("00:00:01"), "runClock"
End If
End Sub
Sub Auto_Open()
clockOn = True
runClock
End Sub
Sub stopClock()
clockOn = False
End Sub
on top of this i will be doing a macro to put the information onto a specific page all going well as this will be depending on the day selected from the drop down menu any help with this would be greatly appriciated :) as in maybe an if statement in the VBA code to select the correct page and leave the data there.
You need to specify the sheet that you are putting the value on; currently you only specify the range, but you don't specify the sheet. Can be done multiple ways, such as:
Sheets(1).Range("M15").Value = Now()
This selects the first sheet in the workbook. If you have multiple workbooks, you will need to specify that somehow as well (by referring to the active workbook, or the name of a workbook, etc.). For example:
Activeworkbook.sheets(1).Range("M15").Value = Now()

Textbox to a cell in hidden sheet

Im trying to make a macro for userform to input data in to hiden sheet. I have tryied application screening then visible the sheet and then input the data in a specific cell but ints not working like that.
Anyone know how it works?
You don't need to make a sheet visible to populate it.
For example, create a new workbook and make sure there is a sheet in there called Sheet2
Hide Sheet2
Put this into the debug window (ctrl-g) in the VB Editor
Sheets("Sheet2").range("A1").Formula = "Text in a hidden sheet"
press enter
Unhide Sheet2 and look at cell A1
Looks like thats not the only problem i have got. I will try to explain it. When a press a button a userform pop up and there is a text box in it. You write a name in the box, click a button and the name appears is a cell in one of the sheets that is hidden. That we already did.
Then i want to move to another sub which is located in one of the sheets. The other sub is for printing out a hidden sheet.
I have an Option Explicit there and
A sub called PrintFile.
Application.ScreenUpdating = False
With Sheets("Opis")
.Visible = True
.PrintOut
.Visible = False
End With
Application.ScreenUpdating = True
The problem is that it's giving me an error and i can't switch to the other sub.
Where im doing it wrong?

Resources