Hide saving location in saving dialog VBA - vba

Can anyone tell me how to hide this dialog box I used below code?
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False

Related

Value of ScreenUpdating not changing after being set to False

I made a small vba script in order to turn on or off screenupdating and calculation automatic.
here is my code:
Sub speed_up()
If Application.Calculation = xlAutomatic Then
Application.ScreenUpdating = False
Application.Calculation = -4135
Else
Application.ScreenUpdating = True
Application.Calculation = -4105
End If
End Sub
Regarding calculation, it's working well.
In my immediate window, when I ask
?Application.ScreenUpdating
I always get
True
Is it normal? shouldn't I get false and True alternatively?

Macro to show/hide status bar in excel

I've been googling this and couldn't find an answer, so I post it for the record, this a macro to show or hide the status bar in excel:
Sub status_bar()
' shortcut: ctrl+shift+k
If Application.DisplayStatusBar = "True" Then
Application.DisplayStatusBar = False
Else
Application.DisplayStatusBar = True
End If
End Sub

Word | VBA - How to start Word in Outline view - opened exactly where you left off?

In MsWord, even though the last location of the cursor is saved automatically, that you could recall by Shift+F5 upon re-opening a document,
- You can neither set it to start in Outline view.
- Nor use that or any other bookmark on a collapsed Outline view to jump on.
Bookmark locations for a collapsed outline are invisible.
The closest option one can achieve is to open all levels of the outline and Then jump on the bookmark.
For the several hundred page scientific documents we use daily that is not acceptable, becuse it heavily decreases the usability of the Outline editor.
Web-view has nowdays also a collapsable Heading-system (where ironically also the bookmark goto works properly), but that lacks other important features that the real Outline view has.
It seems as if two sub-project teams had a hard time collaborating in the Office development team.
I haven't found a working solution on the net for days, so finally I sat down to come up with a reliably working solution (after trashing 3 dead-end ideas).
I will post the VBA code snippets in the response.
For my solution I had to create a separate bookmark for each heading level above cursor location, to be able to open them one by one when the document is re-opened.
Note: I had some issues using range.goto, so instead I had to stick with manipulating Selection for now.
There are two sections - one is for saving the location and closing the document, the other is for opening it properly. - Best to place them inside Normal.dot modules.
the DocumentClosing macro:
Sub SaveAndClose()
Application.ScreenUpdating = False
Call IttTartok
ActiveDocument.Close savechanges:=True
Application.ScreenUpdating = True
End Sub
Private Sub IttTartok()
Application.ScreenUpdating = False
Dim Level As Variant
Dim InduloSel As Range, KereSel As Range
Dim myLevel As Long
'Delete all aiding bookmarks from the last save cycle.
If ActiveDocument.Bookmarks.Exists("IttL1") = True Then ActiveDocument.Bookmarks("IttL1").Delete
If ActiveDocument.Bookmarks.Exists("IttL2") = True Then ActiveDocument.Bookmarks("IttL2").Delete
If ActiveDocument.Bookmarks.Exists("IttL3") = True Then ActiveDocument.Bookmarks("IttL3").Delete
If ActiveDocument.Bookmarks.Exists("IttL4") = True Then ActiveDocument.Bookmarks("IttL4").Delete
If ActiveDocument.Bookmarks.Exists("IttL5") = True Then ActiveDocument.Bookmarks("IttL5").Delete
If ActiveDocument.Bookmarks.Exists("IttL6") = True Then ActiveDocument.Bookmarks("IttL6").Delete
If ActiveDocument.Bookmarks.Exists("IttL7") = True Then ActiveDocument.Bookmarks("IttL7").Delete
If ActiveDocument.Bookmarks.Exists("IttL8") = True Then ActiveDocument.Bookmarks("IttL8").Delete
If ActiveDocument.Bookmarks.Exists("IttL9") = True Then ActiveDocument.Bookmarks("IttL9").Delete
If ActiveDocument.Bookmarks.Exists("IttLAll") = True Then ActiveDocument.Bookmarks("IttLAll").Delete
'Save the cursor location in a Bookmark and check if it is a heading or Bodytext
ActiveDocument.Bookmarks.Add Range:=selection.Range, Name:="IttLAll"
myLevel = selection.Paragraphs(1).OutlineLevel
If myLevel = 10 Then
selection.GoTo wdGoToHeading, wdGoToPrevious, 1
myLevel = selection.Paragraphs(1).OutlineLevel
ActiveDocument.Bookmarks.Add Range:=selection.Range, Name:="IttL" & myLevel
End If
'Search for the upline headings of the original cursor location
For Level = myLevel - 1 To 1 Step -1
selection.Find.ClearFormatting
selection.Find.Style = ActiveDocument.Styles(((-(Level + 1))))
With selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
'...and save the location of every upline heading in a separate Bookmark
If selection.Find.Found Then
ActiveDocument.Bookmarks.Add Range:=selection.Range, Name:="IttL" & Level
End If
Next
Application.ScreenUpdating = True
End Sub
...and the Opener macro:
(note: keep the name, that is needed for auto exacution upon starting of new doc.)
Sub AutoOpen()
Application.ScreenUpdating = False
ActiveWindow.View = wdOutlineView
ActiveWindow.View.ShowHeading 1
Call WhereILeftOff
End If
Application.ScreenUpdating = True
End Sub
Private Sub WhereILeftOff()
Dim i As Variant
If ActiveDocument.Bookmarks.Exists("IttLAll") = True Then
For i = 1 To 9
If ActiveDocument.Bookmarks.Exists("IttL" & i) = True Then
ActiveWindow.View.ExpandOutline ActiveDocument.Bookmarks("IttL" & i).Range
Else
selection.GoTo wdGoToBookmark, , , "IttLAll"
selection.EndKey Unit:=wdLine, Extend:=wdMove
Exit For
End If
Next
End If
End Sub

Visual basic macro- changing the colour of specific text

Im very new to visual basic and macros. what i have been trying to do is create a macro that will look through the whole document and check to see if any of the font is red;if it is red then i want to change the red font to a white font.
I know my code is wrong but can anyone tell me what i am doing wrong?
Sub red()
If Font.Color =wdColorRed Then
Font.Color = -603914241
End Sub
You can use the following which is fairly quick (no looping required for each sheet).
Sub Macro1()
Application.ScreenUpdating = False 'disable the screen from updating, i.e, avoid excel redrawing the screen after each color change we make
Application.FindFormat.Font.Color = 255
Application.ReplaceFormat.Font.Color = 16777215
Cells.Select
Selection.Replace What:="", Replacement:="", MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
Application.ScreenUpdating = True 'enable screen updating
End Sub
Font colors can be a little tricky. So to find out what color you want, select a cell and change the color to a color you need to know the number for. Then go to your developer screen and View -> Immediate Window (or hit Ctrl+G). Then in the Immediate window (should now be at the bottom of your screen, with the cell that has you color you want to know still selected, type
? Selection.Font.Color
and this will give you the color you are interested in. Then put those numbers in Application.Find/ReplaceFormat.Font.Color above.
This will work for the sheet selected, you can simply throw this in a loop and iterate over all the sheets in a workbook to change them all.
Is it what you are looking for ?
Copied from here #Todd Main Answer.
Sub ChangeColorWithReplace()
Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorRed
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = -603914241
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

VBA Running when Excel closed

I have a challenge, for me at least, I cannot deal with apparently. Can somebody help me or advise on how to make the macro run when Excel is closed?
How can I make the macro run when the Excel is closed via VBA?
Sub Upload0()
' Upload Webpage content
Application.OnTime Now + TimeValue("00:00:15"), "Upload0"
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://cetatenie.just.ro/ordine/articol-11", Destination:=Range("A1"))
.Name = "CetatenieOrdine"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = True
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 1
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
' Deletes empty cells
Columns("A:A").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
' Adjust column width and delet useless rows
Rows("1:31").Select
Selection.Delete Shift:=xlUp
Range("B28").Select
Selection.End(xlDown).Select
Rows("17:309").Select
Selection.Delete Shift:=xlUp
End Sub
Many Thanks to all!
1: Define a boolean flag in a module Public blnClosedVBA as Boolean and set it to true in your VBA routine before closing the workbook. Then in your Workbook_BeforeClose event handler (under ThisWorkbook), do this:
If blnClosedVBA = True Then
Cancel = True 'Stops the workbook from closing
'Rest of your code here
blnClosedVBA = False ' Set so next time you try to close the workbook, it actually closes
'Workbook.Close or ThisWorkbook.Close - depends on your answer to my question below
That will run the routine only if you have triggered the close event yourself. At the end of the routine, setting the flag to False and triggering another Workbook.Close will close the workbook
2: Which workbook should it work for? Should it be 'ThisWorkbook' (the one from which you're running the code), 'ActiveWorkbook' (the one activated), or another one?
1.How can I make the macro run when the workbook is closed via VBA?
Short answer: you can't do that. Your code is part of the workbook and it can't run unless the workbook is loaded in Excel. You might be able to move the code to a separate add-in workbook that you elect to load by default (using "Excel Options|Add-Ins") when Excel starts. But Excel would still need to be running somewhere on your computer.
You could write a completely separate program that does what you want, writing the results of the web query into an Excel workbook. I'm assuming here that you want the workbook to always contain up-to-date data when it's referenced by yourself (when Excel is running of course) or some other resource. If you can acquire a copy of Visual Basic 6 (it may not be possible to achieve this legally) then that's mostly syntactically the same as VBA. Next closest would be VB.Net. This is going to be technically somewhat complex.
2.Also, I have issue making this macro working ONLY for one workbook but not active ones:
This one we can deal with: this line:
With ActiveSheet.QueryTables.Add(Connection:= _
means that the following code will always run against the worksheet that has the focus (i.e. is "active" - the sheet that would receive keyboard input if you typed something). That's what ActiveSheet does. Try replacing `ActiveSheet with something likeThisWorkbook.Sheet1, whereSheet1` is the name for the sheet that you see in the "Properties" window in the VBA editor where it says "(Name)".