Excel vba Make a text appear temporarily - vba

So I've been searching everywhere for a way to temporarily show a text box in excel. Basically, what I'm trying to show a text for 5 seconds after the user clicks the button. I don't want anyone to "do the code for me" but instead give me pointers. The user clicks a button to switch language. When that button is pressed, I want a message to appear saying: "All values have been reset". My question is the following: Is there a function in excel-vba that show a textbox for a certain amount of time before disappearing or turning his visibility value to false?
All the rest of the code to switch the language has already been done I'm really only looking to find the function that turns off the visibility after time. (a timer or i don't know)
I doubt showing the code I have so far would help but if you wish to see it, indicate it in the comments.
Thank you SO
Here is my code so far:
Private Sub Ok_Click()
startTimer
Unload Me
End Sub
Sub startTimer()
Application.OnTime Now + TimeValue("00:00:01"), "NextTime"
End Sub
Sub NextTime()
If Sheet3.Range("B5") = 0 Then reset
If Sheet3.Range("B5") = 0 Then Exit Sub
Sheet3.Range("B5").Value = Sheet3.Range("B5").Value - TimeValue("00:00:01")
startTimer
End Sub
Sub reset()
Sheet3.Range("B5") = ("00:00:05")
End Sub

Consider:
Sub MAIN()
Call BoxMaker
DoEvents
DoEvents
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 5
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
ActiveSheet.Shapes("SPLASH").Delete
End Sub
Sub BoxMaker()
ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 217.5, 51#, _
482.25, 278.25).Select
Selection.Name = "SPLASH"
Selection.Characters.Text = "Please Wait for Macro"
With Selection.Characters(Start:=1, Length:=21).Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 36
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
End Sub

Related

Attempting to delete a page in Microsoft Word (VBA) that contains specific text in a textbox

I have been developing a macro for Microsoft Word using VBA which is supposed to find certain text within a textbox (shape) and then delete the page that the textbox with that text is found on. Here is my macro:
Sub DeletePagesWithSpecificTextBoxText()
Dim shp As Shape
Dim FoundOnPageNumber As Integer
For Each shp In ActiveDocument.Shapes
If shp.Type = msoTextBox Then
shp.Select
With Selection.Find
.ClearFormatting
.Text = "delete this page"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .Found Then
FoundOnPageNumber = Selection.ShapeRange.Anchor.Information(wdActiveEndPageNumber)
Selection.GoTo wdGoToPage, wdGoToAbsolute, FoundOnPageNumber
ActiveDocument.Bookmarks("\Page").Range.Delete
End If
End With
End If
Next
End Sub
In order to test this macro - I have a basic ten page document where I have labelled each page in sequence from 1 to 10. Each page has a TextBox which contains the text "delete this page" (this is the text the macro is looking for).
After the macro has been run, the document contains all of the even pages (i.e. 2, 4, 6, 8 and 10) however the odd pages (1, 3, 5, 7 and 9) have been deleted.
Can anyone provide any insight into why it would only be deleting the odd pages?
EDIT:
User macropod was a huge help in getting this to work correctly. The complete working macro is shown below:
Sub DeletePagesWithSpecificTextBoxText()
Dim TextFoundOnThisPage As Integer
Dim DeleteLastPage As Boolean
Application.ScreenUpdating = False
Dim s As Long
With ActiveDocument
For s = .Shapes.Count To 1 Step -1
With .Shapes(s)
If .Type = msoTextBox Then
If InStr(.TextFrame.TextRange.Text, "delete this page") > 0 Then
TextFoundOnThisPage = .Anchor.Information(wdActiveEndPageNumber)
If TextFoundOnThisPage = ActiveDocument.ActiveWindow.Panes(1).Pages.Count And DeleteLastPage = False Then
DeleteLastPage = True
End If
.Delete
Selection.GoTo wdGoToPage, wdGoToAbsolute, TextFoundOnThisPage
ActiveDocument.Bookmarks("\Page").Range.Delete
End If
End If
End With
Next
End With
If DeleteLastPage Then
Selection.GoTo wdGoToPage, wdGoToAbsolute, ActiveDocument.ActiveWindow.Panes(1).Pages.Count
Selection.TypeBackspace
Selection.TypeBackspace
End If
Application.ScreenUpdating = True
End Sub
The DeleteLastPage flag is required to ensure there isn't a blank page left at the end of the document if a textbox was found on that last page.
You should be looping though the shapes backwards; otherwise the loop skips the next shape after a deletion. There is also no need to select anything:
Sub Demo()
Application.ScreenUpdating = False
Dim s As Long
With ActiveDocument
For s = .Shapes.Count To 1 Step -1
With .Shapes(s)
If .Type = msoTextBox Then
If InStr(.TextFrame.TextRange.Text, "delete this page") > 0 Then
.Anchor.Bookmarks("\Page").Range.Delete
End If
End If
End With
Next
End With
Application.ScreenUpdating = True
End Sub

Unselect an entire optionbutton group if another optionbutton outside the group is selected

I have optionbutton1 and optionbutton2.
I also have a 8 others optionbuttons grouped under the groupname "category".
I want to deselect any optionbutton from the groupname "category" If OptionButton1.Value = True Or Optionbutton2.Value = True.
I tried this, but it's doesn't work and it's way too long. There must be another way
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Or OptionButton2.Value = True Then OptionButton4.Value = False And OptionButton5.Value = False And OptionButton6.Value = False And _
OptionButton7.Value = False And OptionButton8.Value = False And OptionButton9.Value = False And OptionButton10.Value = False And OptionButton11.Value = False
End Sub
First, there's no need for your If statement, since the first condition will always evaluate to True when OptionButton1 is clicked. Secondly, each statement that sets an option button to False should be on a separate line, and without the And operator. Try the following instead...
Private Sub OptionButton1_Click()
OptionButton4.Value = False
OptionButton5.Value = False
OptionButton6.Value = False
OptionButton7.Value = False
OptionButton8.Value = False
OptionButton9.Value = False
OptionButton10.Value = False
OptionButton11.Value = False
End Sub
However, here's an alternative...
Private Sub OptionButton1_Click()
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "OptionButton" Then
If ctrl.GroupName = "Category" Then
ctrl.Value = False
End If
End If
Next ctrl
End Sub
Actually, since you want to reset the "Category" option buttons when OptionButton1 or OptionButton2 is clicked, try the following instead...
Private Sub OptionButton1_Click()
Call ResetCategoryOptionButtons
End Sub
Private Sub OptionButton2_Click()
Call ResetCategoryOptionButtons
End Sub
Private Sub ResetCategoryOptionButtons()
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "OptionButton" Then
If ctrl.GroupName = "Category" Then
ctrl.Value = False
End If
End If
Next ctrl
End Sub
Hope this helps!

Activate and deactive ribbon by 1 button VBA EXCEL

I have excel worksheet with 2 buttons. First button deactivates ribbon, second activates it. I need to combine both functions into one button.
Code sample:
Private Sub CommandButton1_Click()
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
Application.DisplayFormulaBar = False
Application.DisplayStatusBar = Not Application.DisplayStatusBar
ActiveWindow.DisplayWorkbookTabs = False
End Sub
Private Sub CommandButton2_Click()
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
Application.DisplayFormulaBar = True
Application.DisplayStatusBar = True
ActiveWindow.DisplayWorkbookTabs = True
End Sub
Hope for help.
You need to get the actual visibility state of the ribbon first.
Private Sub cmdToggleRibbon_Click()
Dim isRibbonVisible As Boolean
isRibbonVisible = Application.ExecuteExcel4Macro("Get.ToolBar(7,""Ribbon"")")
If isRibbonVisible Then
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
Else
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
End If
Application.DisplayFormulaBar = Not isRibbonVisible
Application.DisplayStatusBar = Not isRibbonVisible
ActiveWindow.DisplayWorkbookTabs = Not isRibbonVisible
End Sub

Create a conditional formating macro for text value on Excel

Very often I need to create conditional formating rules on my excel worksheets, not always on the same range, to format the text color depending on what's written.
The most common situation is turning all the cells in the range that have the text "Effective" green and bold, and "Not effective" red and bold.
I tried to create this macro using the Record Macro function on the Developer tab, but it didn't work, the code was blank.
As I have zero knowledge on VBA, I was wondering if somebody could give me a help creating this macro.
Definitions:
There's no fixed range, it needs to capture the selected range;
Format based on text, if "Effective" green and bold, if "Not effective" red and bold.
Only for one sheet.
[Solved]
Sub EffectiveNot()
'
' EffectiveNot Macro
'
Dim rStart As Range
Set rStart = Selection
Selection.FormatConditions.Add Type:=xlTextString, String:="Effective", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.Color = -11489280
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:="Not effective", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Any chance you checked your ThisWorkbook-Module? The macro recorder adds a new empty module each day you run it, then dumps the code in there...
This is basically what the macro recorder comes up with, after I cleaned it up a bit. Feel free to swap Selection to a range-object more appropriate for your use.
Option Explicit
Sub format()
With Selection
With .FormatConditions
.Delete
With .Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""Not Effective""")
With .Font
.Color = vbRed
.Bold = True
End With
.StopIfTrue = False
End With
With .Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""Effective""")
With .Font
.Color = vbGreen
.Bold = True
End With
.StopIfTrue = False
End With
End With
End With
End Sub

Hide Columns Based on Combobox (Form) In Excel

I have everything setup with different options for forecasts. I have done this in the past at a different job and I can't figure out where I am going wrong.
Private Sub ComboBox1_Change()
If ComboBox1.Value = "2 Weeks" Then
Columns("J:L").Select
Selection.EntireColumn.Hidden = False
Columns("M:R").Select
Selection.EntireColumn.Hidden = True
End If
If ComboBox1.Value = "6 Weeks" Then
Columns("M:O").Select
Selection.EntireColumn.Hidden = False
Columns("J:L").Select
Selection.EntireColumn.Hidden = True
Columns("P:R").Select
Selection.EntireColumn.Hidden = True
End If
If ComboBox1.Value = "12 Weeks" Then
Columns("P:R").Select
Selection.EntireColumn.Hidden = False
Columns("J:O").Select
Selection.EntireColumn.Hidden = True
End If
End Sub
It seems like your code should work just fine. I rewrote it to be more concise and it worked perfectly (with an Active-X control).
Private Sub ComboBox1_Change()
Select Case ComboBox1.Value
Case "2 Weeks"
Columns("J:L").Hidden = False
Columns("M:R").Hidden = True
Case "6 Weeks"
Columns("J:L").Hidden = True
Columns("M:O").Hidden = False
Columns("P:R").Hidden = True
Case "12 Weeks"
Columns("J:O").Hidden = True
Columns("P:R").Hidden = False
End Select
End Sub
In the title I included it was a form control. – phentrus 8 mins ago
The reason why I was confused and asked you in the comments above whether you are using a form control or an ActiveX control is because your quesiton title says "Form" but the code is for ActiveX.
For Form Control, paste this code in a module.
Option Explicit
Sub DropDown1_Change()
Dim DDown As Shape
Set DDown = ActiveSheet.Shapes(Application.Caller)
Select Case DDown.ControlFormat.List(DDown.ControlFormat.ListIndex)
Case "2 Weeks"
Columns("J:L").Hidden = False
Columns("M:R").Hidden = True
Case "6 Weeks"
Columns("J:L").Hidden = True
Columns("M:O").Hidden = False
Columns("P:R").Hidden = True
Case "12 Weeks"
Columns("J:O").Hidden = True
Columns("P:R").Hidden = False
End Select
End Sub
Next right click your Form Combobox and assign the above macro to it :)