Im working on a project for a VBA program for which the the entire program is to be viewed in maximized/fullscreen view. No Tabs, Formula bars etc. The user will navigate the worksheets with custom buttons and commands. The issue im having is that most of my design is coming from my laptop and (with res 1366/768) and when it is viewed on a bigger monitor (res 2880/1620) the view is not aesthetically pleasing.
Is there a way to code VBA to adjust the view based on the users screen size? Im aware of using range.zoom but that will just cause the images to be absurdly massive on a larger screen even though it would be to scale it would be more pleasing to the eye for the zoom to be set back when the screen gets to a certain size. Even if the process coudn't be automatic. im not opposed an input box opening at the start of the program for the user to select small, medium or large before it runs.
Private Sub Workbook_Open()
Dim wsh As Worksheet
Dim CarryOn As Integer
Set wbPB = PokerBros
Set wsh = wbPB.Worksheets("Home")
CarryOn = MsgBox("Do you want to save a copy of this original file?", vbQuestion + vbYesNo, "Save Copy Recommended")
If CarryOn = vbYes Then
Call CopyToNewBook
End If
wsh.Activate
Call ZoomToFitHome
Call ScreenDisplayMax
End Sub
Sub ZoomToFitHome()
Dim wbh As Worksheet
Set wbPB = PokerBros
Set wbh = wbPB.Worksheets("Home")
wbh.Range("A1:AA30").Select
ActiveWindow.Zoom = True 'set range zoom
End Sub
Sub ScreenDisplayMax()
' Call UnProtectAllSheets
With Application
.DisplayStatusBar = False
.DisplayFullScreen = True
With ActiveWindow
.WindowState = xlMaximized
.DisplayHeadings = False
.DisplayWorkbookTabs = False
.DisplayGridlines = False
.DisplayHorizontalScrollBar = True
.DisplayVerticalScrollBar = True
End With
.DisplayFormulaBar = False
End With
End Sub
Related
Dim ws As Worksheets
Application.ScreenUpdating = False
On Error Resume Next
Set ws = Sheets("Details")
Range("DetailsOutput").ClearContents
Sheets("WorkSh1").Visible = True
Sheets("WorkSh1").Select
Sheets("Details").Visible = False
Application.Wait (Now + TimeValue("00:00:05"))
Application.ScreenUpdating = True
After this code control goes to "WorkSh1", the buttons in a row becomes invisible but still can be selected. If i debug, buttons seems to be appearing normally. Issue appear while running normally
Along with it, a button from "Details" worksheet too appears in "WorkSh1" but can't select it. What can try?
My Excel application requires a pop-up screen while it is loading and saving files. I have created a macro, shown below to pop-up a shape. Initially, the oShape.Top location is 300, below the current screen.
I have tried all combinations of the macro & cannot get this oval shape to be visible on the current screen. Oddly, if I create a debug-toggle breakpoint on the last "DoEvents" in this macro, the pop-up will be visible.
Any assistance would be appreciated. Macro is below:
Public Sub TestUP()
Dim oShape As Shape
Set oShape = ActiveSheet.Shape("Oal42")
Application.ScreenUpdating = True
DoEvents
NextTime = Now + TimeValue("00:00:05")
oShape.Visible = True
oShape.Top = 80
DoEvents
NextTime = Now + TimeValue("00"00"05")
DoEvents
End Sub
Your main problem is that a shape is not repainted in the moment you show it, and it seems it is even not repainted when you issue a DoEvent. For a UserForm, there is a Repaint method to force VBA to re-show it, but not for a sheet or a shape.
However, there are tricks to do so. This answer shows 3 possible hacks. I tried Application.WindowState = Application.WindowState and it worked for me. The following code gives an example how you could use it - you can modify the text during runtime.
Option Explicit
Const ShapeName = "Oal42"
Public Sub ShowMsg(msg As String)
With ActiveSheet.Shapes(ShapeName)
If .TextFrame2.TextRange.Characters <> msg Then
.TextFrame2.TextRange.Delete
.TextFrame2.TextRange.Characters = msg
End If
.Visible = True
.Top = 80
DoEvents
Application.WindowState = Application.WindowState
End With
End Sub
Public Sub HideMsg()
ActiveSheet.Shapes(ShapeName).Visible = False
End Sub
This shows the usage:
Sub testSub()
ShowMsg "Start"
Dim i As Long
For i = 1 To 100 Step 8
ShowMsg "Working, " & i & "% done."
Application.Wait Now + TimeSerial(0, 0, 1)
Next i
HideMsg
End Sub
In a large project I add a large number of shapes at different positions in a Worksheet. Furthermore, I insert a number of rows.
I want the shapes to move with the inserted rows. However, they only do so with Application.ScreenUpdating = True. As soon as ScreenUpdating is set False, the shapes stop moving. This of course messes up the results completely.
I cannot reproduce the problem. In this minimal example, the inserted shapes move as expected with the inserted row, although I use Application.ScreenUpdating = False. In my larger program the basically identical procedure fails without ScreenUdating.
Sub ShapeTest()
Dim ActiveShape As Shape
Dim ShapeCell As Range
Application.ScreenUpdating = False
Set ShapeCell = ActiveSheet.Range("A1")
Set ActiveShape = ActiveSheet.Shapes.AddShape(msoShapeRectangle, ShapeCell.Left, ShapeCell.Top, ShapeCell.Width, ShapeCell.Height)
ActiveSheet.Rows(1).Insert shift:=xlShiftDown
Application.ScreenUpdating = True
End Sub
Update
I have tried DoEvents before and after inserting the row, but it didn't change anything. Currently I am using this workaround:
Application.ScreenUpdating = True
Worksheets("Gantt").Rows(ThisRowGTT).Insert shift:=xlShiftDown
Application.ScreenUpdating = False
This slows down the execution alot - almost as if I would use ScreenUpdating for the entire program.
Help!
Below is my code for a start/stop button in Excel and now I'm getting a Compile error that is highlighting btnStart on the first line.
Sub btnStart()
ActiveSheet.Unprotect
Cells(Rows.Count, 5).End(xlUp).Offset(1) = Date
Cells(Rows.Count, 6).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Cells(Rows.Count, 8).End(xlUp).Offset(1) = Environ("username")
Me.btnStart().Enabled = False
Me.btnStop.Enabled = True
End Sub
Sub btnStop()
ActiveSheet.Unprotect
Cells(Rows.Count, 7).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
End Sub
Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
If I understand correctly the buttons are on an excel sheet (not in a user form) so your issue because you are calling the buttons incorrectly. you can't simply call the named buttons you must look into the sheet.buttons property like this
ActiveSheet.Buttons("btnStart").
Also if you have named sheets I would use the sheetname rather than ActiveSheet
Sheets("sheetname").Buttons("btnStart").
One more note, that enabling/disabling a button works BUT it doesn't make the button appear enabled/disabled. To do this you also have to change the font color.
ActiveSheet.Buttons("btnStart").Font.ColorIndex = 15 '15 is grey, 1 is black
---- edit: code changed ---
REASON: After doing some more research it seems there are problems with my original solution. The most important is that the "enabled" property has no effect in excel 2010. Another route would be using activeX controls, BUT, a recent windows update (dec 2014) prevents activeX controls from running without deleting some system files (which would have to be done for each user, on every computer this code may run on -_- good job MS SOURCE)
This new solution should avoid all those problems. It uses two global variables start_btn_disabled and stop_btn_disabled I assume each button in your form (btnStart and btnStop) have a macro assigned to them? Simply check the global variable at the very beginning of the the sub if the button is disabled then quit the sub, so even though the click is still processed (it will always be processed in excel 2010 as stated before) the code doesn't run. So it behaves as though it was disabled. In my code I made a sub called btnStopClicked that would run when you click 'btnStop' In order to assign the macro to a button, right click the button, select "assign macro" and select the appropriate macro. I also created a similar sub for when you click the start button
'these are global variables and should be declared at the top of the module
'outside of any sub/function
'
'Note we use DISabled rather than enabled, because by default
'booleans = False. This means as soon as the form opens both these buttons
'will be enabled without any extra work.
'If you want to change this (make start button enabled and stop disabled,
'when the workbook opens simply change all the "stop_btn_disabled" to
'"stop_btn_enabled" and uncomment the following line) make sure you change the
'variable names so they make sense
'Dim stop_btn_enabled As Boolean 'initializes to false
Dim start_btn_disabled As Boolean 'intializes to false
Dim stop_btn_disabled As Boolean 'intializes to false
'Most of this code remains the same as before
Sub btnStart()
ActiveSheet.Unprotect
Cells(Rows.Count, 5).End(xlUp).Offset(1) = Date
Cells(Rows.Count, 6).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Cells(Rows.Count, 8).End(xlUp).Offset(1) = Environ("username")
'now we set the state of the global variable
start_btn_disabled = True
'makes the button appear greyed out
ActiveSheet.Buttons("btnStart").Font.ColorIndex = 15
'now we set the state of the global variable
stop_btn_disabled = False
'makes the button black like normal
ActiveSheet.Buttons("btnStop").Font.ColorIndex = 1
End Sub
Sub btnStop()
ActiveSheet.Unprotect
Cells(Rows.Count, 7).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
'now we set the state of the global variable
stop_btn_disabled = True
'makes the button appear greyed out
ActiveSheet.Buttons("btnStop").Font.ColorIndex = 15
'now we set the state of the global variable
start_btn_disabled = False
'makes the button black like normal
ActiveSheet.Buttons("btnStart").Font.ColorIndex = 1
End Sub
'and now the real key is checking the globals before running the code
'when you click "btnStop" button this is the code that runs, you may have
'named the sub something different, I just named it this way so it's clear
'what the sub does
Sub StopBtnClicked()
'must be the first bit of code in the btn click sub
If (stop_btn_disabled) Then
Exit Sub
End If
'the rest of the code when you click stop button goes here
'the only way to get to this point is if the but is enabled
End Sub
Sub StartBtnClicked()
'must be the first bit of code in the btn click sub
If (start_btn_disabled) Then
Exit Sub
End If
'the rest of the code when you click start
End Sub
if this solves your problem, please mark it as the answer
Maybe try the following, I corrected one line
Sub btnStart()
ActiveSheet.Unprotect
Cells(Rows.Count, 5).End(xlUp).Offset(1) = Date
Cells(Rows.Count, 6).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Cells(Rows.Count, 8).End(xlUp).Offset(1) = Environ("username")
'Corrected line below
Me.btnStart.Enabled = False
Me.btnStop.Enabled = True
End Sub
Sub btnStop()
ActiveSheet.Unprotect
Cells(Rows.Count, 7).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
End Sub
Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
You need to use ActiveX buttons if you want to use the Enabled property:
Private Sub btnStart_Click()
Me.btnStart.Enabled = False
Me.btnStop.Enabled = True
End Sub
Private Sub btnStop_Click()
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
End Sub
I have 3 input boxes that when a document is selected it opens the document and fills in a select box. With Excel 2013 these windows are opened in front of the document and have to be minimized in order to select the next one. Is there a way to have it automatically open documents in the background?
Workbooks.Open (file_path)
If Application.Version >= 15# Then
ActiveWindow.WindowState = xlMinimized
End If
This what I currently have, however I would rather it open in the background in the first place over having to minimize it. I have tried to turn off screen updating for the part, however that did not work.
Application.ScreenUpdating = False
Workbooks.Open (file_path)
Application.ScreenUpdating = True
Does Application.ScreenUpdating affect WorkBooks.open in Excel 2013?
Application.ScreenUpdating will not work with excel 2013.
You may try creating function:
Sub myScreenUpdate(screenUpdateRequest As Boolean, previousScreenUpdate As Boolean)
If screenUpdateRequest Then
Application.ScreenUpdating = previousScreenUpdate
Else
previousScreenUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
End If
End Sub
call req:
Call myScreenUpdate(False, previousScreenUpdate)
Call myScreenUpdate(True, previousScreenUpdate)
Dim previousScreenUpdate as boolean
Call myScreenUpdate(False, previousScreenUpdate) 'to get the current setting into previousScreenUpdate
Call myScreenUpdate(True, false) 'to set updating to false
'do your heavy code here and when finished:
Call myScreenUpdate(True, previousScreenUpdate)