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?
Related
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
I'm going through a peculiar failure in my excel vba macro wherein the excel sheet crashes on the 2nd run of the macro (Yes, 1st run in perfect).
This macro is to delete all data labels except the recent one for a particular series in all graphs in a sheet. Common graph is used with slicer and Data table to select which line item is needed in graph. on every change in slicer (new line items is selected in data table filter), i want the macro to run and do the label work.
Below is source code for the Graph data label work,
Sub Update_labels_Only_To_Last_Data()
Dim myChartObject As ChartObject
Dim mySrs As Series
Dim myPts As Points
Dim iPts As Long
Dim bLabeled As Boolean
Application.EnableEvents = False
Name = ThisWorkbook.Name
Set Wb = Workbooks(Name)
Set WsGraph = Wb.Worksheets("Graph")
WsGraph.Activate
With ActiveSheet
For Each myChartObject In .ChartObjects
For Each mySrs In myChartObject.Chart.SeriesCollection
If mySrs.Name = "TAR" Then
bLabeled = False
With mySrs
For iPts = .Points.Count To 1 Step -1
If bLabeled Then
' handle error if point isn't plotted
On Error Resume Next
' remove existing label if it's not the last point
mySrs.Points(iPts).HasDataLabel = False
On Error GoTo 0
Else
' handle error if point isn't plotted
On Error Resume Next
' add label
mySrs.Points(iPts).ApplyDataLabels _
ShowSeriesName:=False, _
ShowCategoryName:=False, ShowValue:=True, _
AutoText:=True, LegendKey:=False
bLabeled = (Err.Number = 0)
On Error GoTo 0
End If
Next
End With
Set myPts = mySrs.Points
myPts(myPts.Count).ApplyDataLabels Type:=xlShowValue 'Add only to last data
End If
Next
Next
End With
'End If
Application.EnableEvents = True
End Sub
I've tried running macro using a separate button it works perfectly. Then linked this with event macro (Calculation) using below code in sheet (Donotdelete)
Option Explicit
Private Sub Worksheet_Calculate()
Dim Act_Sheet As Worksheet
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculate
If Not Application.CalculationState = xlDone Then 'wait till previous calculation gets over
DoEvents
End If
Set Act_Sheet = ActiveSheet
Application.Wait (Now + TimeValue("0:00:01"))
Call Update_labels_Only_To_Last_Data
Act_Sheet.Activate
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
so once a slicer filter is changed, calculate event is triggered using "Count" function in "Donotdelete" sheet and macro gets run. when first time slicer us changed, macro works perfectly. but when 2nd time the slicer is changed to call different graph, macro gets initiated and all of a sudden, completely sheet gets crashed/closed. Tried in break mode and the code works perfectly. tried giving doevents / wait options and still the result is same. To confirm the crash source, tried skipping the "macro" using comment block in eventmacro sheet & the code works perfectly. on any selection change in slicer, macro goes thro the event macro and works perfectly. with this, source is confirmed to be the macro "Update_labels_Only_To_Last_Data()".
i use the same macro in main module and there it works perfectly any number of consecutive times. could not find why it crashes only when used through even trigger and only on the 2nd time !!!. did all my google help thing for couple of weeks now but no result. see if you guys can show me a wayout.
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
I would like to write a code which, before closing the Workbook, sets all the sheets except one cover as very hidden.
I click on the "X" to close the Workbook, the Macro is fired and everything fine.
Then I receive the classic saving form of Excel and, if I click cancel, I receive error 91 - Object variable or With block variable not set.
Could someone explain me why is happening? I used the same code in the past and I did not have this issue
It is interesting because, if there is another excel workbook open at the same time, it works everything fine.
In Tab ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = True
Call my_macro 'defined in a separate module
Application.EnableEvents = False
End Sub
For the sake of clarity in Module 1 the code is following:
Public Sub my_macro()
Application.ScreenUpdating = False
On Error GoTo skip
Dim ws As Worksheet
Sheet8.Visible = True
For Each ws In Worksheets
If ws.Name = "Cover" Then
Else
ws.Visible = xlSheetVeryHidden
End If
Next ws
Sheet8.Select
Range("A1").Select
Application.ScreenUpdating = True
ActiveSheet.EnableSelection = xlNoRestrictions
Application.EnableEvents = True
skip:
Application.EnableEvents = True
End Sub
I am having an issue regarding setting a range of cells to be editable by a user after my code runs when the file opens. The file gets a runtime error "Unable to set the locked property of the range class".
I have done some digging online and it appears other people have the same issue when their cells are merged. My cells aren't merged.
What I am trying to do:
Lock all cells on all sheets apart from specific ones
Still allow user to group and ungroup data
The code runs fine without selecting specific cells
Sub Workbook_Open()
Application.StatusBar = "Loading Please Wait..." 'change status bar text
Application.ScreenUpdating = False 'freeze screen
Application.Cursor = xlWait 'change cursor
UserForm1.Show vbModeless 'show loading form
UserForm1.Repaint 'update form
For Each ws In Sheets 'loop for every sheet
With ws
.Unprotect Password:="11Oceans" 'unprotect sheet
.Range("D6:J6").Locked = False 'format certain cells to unprotect -ERROR HERE
.Protect Password:="11Oceans", UserInterfaceOnly:=True 'protect sheet but leave data grouping
.EnableOutlining = True
End With
Next ws 'next worksheet
Application.ScreenUpdating = True 'return control to user
Application.StatusBar = "" 'return status bar to default
Application.Cursor = xlDefault 'return cursor to default
Unload UserForm1 'close loading form
End Sub