What is the correct code in order for the workbook to save to the referenced file path in cell B19? The file path looks like this C:Desktop\ExcelFiles\Data Table.xlsb
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
ThisWorkbook.SaveAs Filename:=Sheets("Sheet1").Range("B19").Value
'^^^Need help understanding the correct syntax of this line ^^^
This worked for me.
Sub SaveMe()
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
ThisWorkbook.SaveAs Filename:=Sheets("Sheet1").Range("B19").Value, _
FileFormat:=xlExcel12, CreateBackup:=False
End Sub
Basically,just turn on the Macro Recorder and modify the code, slightly, to suit your needs.
Related
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?
I have code like the following:
Sub RMS()
Application.Calculation = xlCalculationManual
Sheets("m1").Range("A3").FormulaR1C1 = "=LEN(LEFT(m!R[2]C,FIND(""x"",m!R[2]C & "","")-1))"
Range("A1:A3").Select
Selection.AutoFill Destination:=Range("A1:EZ3"), Type:=xlFillDefault
Range("A1:EZ3").Select
Selection.AutoFill Destination:=Range("A1:EZ600"), Type:=xlFillDefault
Range("A1:EZ600").Select
End Sub
This code is running very slow. Is there any help you can give so that code like this could run much faster because I run this code in multiple sheets?
This will be faster :
Sub RMS()
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
With Sheets("m1")
.Range("A3").FormulaR1C1 = "=LEN(LEFT(m!R[2]C,FIND(""x"",m!R[2]C & "","")-1))"
.Range("A1:A3").AutoFill Destination:=.Range("A1:EZ3"), Type:=xlFillDefault
.Range("A1:EZ3").AutoFill Destination:=.Range("A1:EZ600"), Type:=xlFillDefault
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub
I've been researching how to speed up my code in Excel VBA and I've come across the following settings which have been helpful. My question is: is it possible to set the following lines of code into one variable that I can set to On or Off to activate the entire list? I.e. something like
speedUpCode = On
would set all of the below settings and if it were set to Off it would reverse all of the below to True/xlCalculationAutomatic
With Application
.ScreenUpdating = False
.DisplayStatusBar = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
ActiveSheet.DisplayPageBreaks = False 'note this is a sheet-level setting
I use this (very basic):
Sub GoFast(Optional bYesNo As Boolean = True)
With Application
.ScreenUpdating = Not bYesNo
.Calculation = IIf(bYesNo, xlCalculationManual, xlCalculationAutomatic)
End With
End Sub
Call with True or no parameter to speed things up, then with False to reset.
The comments above about about possibly capturing the current state of the various settings so you can get back to the "original" state, and that not all settings are always appropriate to update depending on exactly what you're doing are all worth considering.
You can use a function to do this like so ...
Function speedUpCode(sStatus As String)
If sStatus = "On" Then
With Application
.ScreenUpdating = False
.DisplayStatusBar = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
ActiveSheet.DisplayPageBreaks = False 'note this is a sheet-level setting
Else if sStatus = "Off" then
With Application
.ScreenUpdating = True
.DisplayStatusBar = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
ActiveSheet.DisplayPageBreaks = True 'note this is a sheet-level setting
End Function
you can then use these to turn on and off
speedUpCode "On"
speedUpCode "Off"
However, keep in mind that you are turning settings on and off - you should probably check the status of these before changing them so that you can reset them to the original setting rather then just turning them all off again
you could does this with static variables
this is my code for my Macro. What is does is copy a specific sheet to another workbook. My question is how to paste only values and the format must be read type only.
Sub NewReport()
Dim PRICE_REV_TEMPLATE As Workbook
Dim PRICE_REV_TEMPLATE_FC As Workbook
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
Set PRICE_REV_TEMPLATE = ActiveWorkbook
Set PRICE_REV_TEMPLATE_FC = Application.Workbooks.Add(1)
PRICE_REV_TEMPLATE.Sheets(Array(PRICE_REV_TEMPLATE.Sheets(6).Name)).Copy _
Before:=PRICE_REV_TEMPLATE_FC.Sheets(1)
PRICE_REV_TEMPLATE_FC.SaveAs FileName:="C:\Users\A3RBJZZ\Desktop\PRICE_REV_TEMPLATE_FC"
PRICE_REV_TEMPLATE_FC.Close
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
Try this - Hope this Helps
Sub NewReport()
Dim PRICE_REV_TEMPLATE As Workbook
Dim PRICE_REV_TEMPLATE_FC As Workbook
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
Set PRICE_REV_TEMPLATE = ActiveWorkbook
Set PRICE_REV_TEMPLATE_FC = Application.Workbooks.Add(1)
PRICE_REV_TEMPLATE.Sheets(Array(PRICE_REV_TEMPLATE.Sheets(1).Name)).Copy Before:=PRICE_REV_TEMPLATE_FC.Sheets(1)
'selecting all cells and pasting as only values
PRICE_REV_TEMPLATE_FC.ActiveSheet.Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
PRICE_REV_TEMPLATE_FC.ActiveSheet.Paste
Application.CutCopyMode = False
'Added the Readonlyrecommended attrib.
PRICE_REV_TEMPLATE_FC.SaveAs Filename:="C:\Users\A3RBJZZ\Desktop\PRICE_REV_TEMPLATE_FC", ReadOnlyRecommended:=True
PRICE_REV_TEMPLATE_FC.Close
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
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)".