I'm getting a frustrating error in my macro code. I have a small bit of code in two subs one opens all data groups and changes some of the print options and the other closes the groups and changes some of the print options.
This works fine but when I save and close on re-open if I click the macro buttons I get a 400 error message. To fix this I have comment out all the .pagesetup code and then run the macros again then uncomment and run the macro line by line and then the 400 error will disappear. However once I save and close this will come back and I have to repeat everything. Any help would be greatly appreciated.
Sub PropertyStrat_Click()
'
' PropertyStrat_Click Macro
'
ActiveSheet.Outline.ShowLevels RowLevels:=2 ' to expand the rows
Worksheets("Sheet1").PageSetup.Orientation = xlPortrait
Worksheets("Sheet1").PageSetup.PaperSize = xlPaperA3
Worksheets("Sheet1").PageSetup.Zoom = False
Worksheets("Sheet1").PageSetup.FitToPagesTall = 1
Worksheets("Sheet1").PageSetup.FitToPagesWide = 1
Worksheets("Sheet1").PageSetup.CenterHorizontally = True
Worksheets("Sheet1").PageSetup.CenterVertically = True
End Sub
Sub SimpleView_Click()
ActiveSheet.Outline.ShowLevels RowLevels:=1 ' to collapse the rows
Worksheets("Sheet1").PageSetup.PrintArea = "$A$1:$J$29,$A$30:$J$50"
Worksheets("Sheet1").PageSetup.Orientation = xlLandscape
Worksheets("Sheet1").PageSetup.PaperSize = xlPaperA4
Worksheets("Sheet1").PageSetup.Zoom = 100
Worksheets("Sheet1").PageSetup.CenterHorizontally = False
Worksheets("Sheet1").PageSetup.CenterVertically = False
End Sub
Related
I've been learning today some VBA basics to apply in powerpoint, but I have some experience in some other languages. As title says, I want a picture to be shown after I click on 3 other pictures before that. When any of this 3 pictures are clicked, they trigger a tick to be shown above that image, and I'm using those as a refer to code my macro. I have the following:
Sub Condicion()
Set Diapo14 = ActivePresentation.Slides(14)
If Diapo14.Shapes("tick1").Visible = True And _
Diapo14.Shapes("tick2").Visible = True And _
Diapo14.Shapes("tick3").Visible = True Then
Diapo14.Shapes("FlechaDer").Visible = True
End If
End Sub
I have the picture I want to show (FlechaDer) with a disappear effect as soon as the slide starts, but no matter what I do, when I test the slide, the picture is always there. Maybe I'm not applying the correct approach, hope someone can help me. I'm not even sure if this can be done in PowerPoint.
I got it working with the code below. I had to replace the "ticks" in the If cycle, and instead of them I used the pictures that once clicked popped up the ticks:
Sub Can12()
Set Diapo12 = ActivePresentation.Slides(12)
Diapo12.Shapes("Can").Visible = False
If Diapo12.Shapes("Can").Visible = False And Diapo12.Shapes("Wool").Visible = False And Diapo12.Shapes("Pencil").Visible = False Then
Diapo12.Shapes("FlechaDer").Visible = True
End If
End Sub
Sub Wool12()
Set Diapo12 = ActivePresentation.Slides(12)
Diapo12.Shapes("Wool").Visible = False
If Diapo12.Shapes("Can").Visible = False And Diapo12.Shapes("Wool").Visible = False And Diapo12.Shapes("Pencil").Visible = False Then
Diapo12.Shapes("FlechaDer").Visible = True
End If
End Sub
Sub Pencil12()
Set Diapo12 = ActivePresentation.Slides(12)
Diapo12.Shapes("Pencil").Visible = False
If Diapo12.Shapes("Can").Visible = False And Diapo12.Shapes("Wool").Visible = False And Diapo12.Shapes("Pencil").Visible = False Then
Diapo12.Shapes("FlechaDer").Visible = True
End If
End Sub
It worked fine on activating an animation (Right Arrow popping up) when all 3 items were clicked. I had to add the corresponding macro to the picture. (Can, wool and pencil)
I have written a simple macro to force end users to view an EULA and then click a command button to accept the terms. On clicking the tabs for the workbook open allowing the user to work through the book. This works fine in Excel 2010 and all previous version but not in Excel 2013.
I get this error:
This is the code:
Sub OpenSheets()
'
' OpenSheets Macro
'
'
Sheets("EULA").Select
Sheets("Infection_Worksheet").Visible = True
Sheets("Infection_Worksheet").Select
Sheets("Exit_Site_Infection_Chart").Visible = True
Sheets("Exit_Site_Infection_Chart").Select
Sheets("Peritonitis_Chart").Visible = True
Sheets("Exit_Site_Infection_Chart").Select
Sheets("%_Pts_peritonitis_free").Visible = True
Sheets("%_Pts_peritonitis_free").Select
Sheets("Pt_numbers").Visible = True
Sheets("Pt_numbers").Select
Sheets("Results").Visible = True
Sheets("Results").Select
Sheets("Instructions").Visible = True
End Sub
Does anyone have any suggestions?
Underscores represent continuation to the next line in VBA.
Try this Sheets("Infection" & Chr(97) & "Worksheet").Visible = True
etc etc etc
Before you go for the obvious: Application.DisplayAlerts = False has not solved my problem.
I have written a VBA procedure (initiated in Excel 2010) which loops around an array containing different Excel files. The loop opens the file, refreshes the data, saves and closes the file for each item in the array. I have written an error catch sub routine so I log which excel files have failed to open/refresh/save etc so a user can manually check them.
Some files are quite large and involve a large amount of data moving across the network; sometimes I get a dialog box with: Excel is waiting for another application to complete an OLE action.
I could use Application.DisplayAlerts = False to disable the message but this would presumably disable all alerts so I couldn't catch the errors?
Further I have tested using the line and it doesn't stop the dialog box pop-up. If I press enter it carries on but will likely pop-up again a few minutes later.
Is there a way to stop is message specifically without stopping other alerts?
NB. My process has a control instance of Excel which runs the VBA and opens the workbooks to be refreshed in a separate instance.
Thanks for your help
An extract of my code is below which contains the refresh elements
Sub Refresh_BoardPivots_Standard()
' On Error GoTo Errorhandler
Dim i
Dim errorText As String
Dim x
Dim objXL As Excel.Application
Set objXL = CreateObject("Excel.Application")
GetPivotsToRefresh ' populate array from SQL
For Each i In StandardBoardPiv
DoEvents
'If File_Exists(i) Then
If isFileOpen(i) = True Then
errorText = i
Failed(failedIndex) = errorText
failedIndex = failedIndex + 1
Else
objXL.Visible = True 'False
objXL.Workbooks.Open FileName:=i
If objXL.ActiveWorkbook.ReadOnly = False Then
BackgroundQuery = False
Application.DisplayAlerts = False
objXL.ActiveWorkbook.RefreshAll
objXL.Application.CalculateFull
objXL.Application.DisplayAlerts = False
objXL.ActiveWorkbook.Save
objXL.Application.DisplayAlerts = True
objXL.Quit
Else
errorText = i
Failed(failedIndex) = errorText
failedIndex = failedIndex + 1
objXL.Application.DisplayAlerts = False
objXL.Quit
Application.DisplayAlerts = True
End If
End If
' Else
' errorText = i
' Failed(failedIndex) = errorText
' failedIndex = failedIndex + 1
' End If
DoEvents
If Ref = False Then
Exit For
End If
Next i
Exit Sub
'Errorhandler:
'
'errorText = i
'Failed(failedIndex) = errorText
'failedIndex = failedIndex + 1
'Resume Next
End Sub
"Waiting for another application to complete an OLE action" isn't an alert message you can just turn off and forget, sometimes the macro will be able to continue on after, but in my experience if you are getting that error its only a matter of time until the problem crashes/freezes your whole macro so it should definitely be troubleshot and corrected.
I only get that error when I am using additional Microsoft Office Applications (other than the Excel that is running the code) as objects and one of them has an error- the Excel running the code doesn't know that an error occurred in one of the other applications so it waits and waits and waits and eventually you get the "Waiting for another application to complete an OLE action" message...
So to troubleshoot this sort of problem you got to look for the places you use other MSO apps... In your example, you have an additional instance of Excel and you are pulling data from Access, so its most likely one of those two that is causing the problems...
Below is how I would re-write this code, being more careful with where the code interacts with the other MSO apps, explicitly controlling what is happening in them.. The only piece I couldn't really do much is GetPivotsToRefresh because I cant see what exactly youre doing here, but in my code I just assumed it returned an array with a list of the excel files you want to update. See code below:
Sub Refresh_BoardPivots_Standard()
Dim pivotWB As Workbook
Dim fileList() As Variant
Dim fileCounter As Long
Application.DisplayAlerts = False
fileList = GetPivotsToRefresh 'populate array from SQL
For fileCounter = 1 To UBound(fileList, 1)
Set pivotWB = Workbooks.Open(fileList(fileCounter, 1), False, False)
If pivotWB.ReadOnly = False Then
Call refreshPivotTables(pivotWB)
pivotWB.Close (True)
Else
'... Error handler ...
pivotWB.Close (False)
End If
Next
End Sub
Public Sub refreshPivotTables(targetWB As Workbook)
Dim wsCounter As Long
Dim ptCounter As Long
For wsCounter = 1 To targetWB.Sheets.Count
With targetWB.Sheets(wsCounter)
If .PivotTables.Count > 0 Then
For ptCounter = 1 To .PivotTables.Count
.PivotTables(ptCounter).RefreshDataSourceValues
Next
.Calculate
End If
End With
Next
End Sub
So I created my own 'refreshPivotTables' but you could have embedded that into the master sub, I just thought the loops and loop counters might get a little messy at that point...
Hope this helps,
TheSilkCode
I am creating an Excel Spreadsheet graph using VBA like so:-
Private Sub Chart_Calculate()
Title$ = Range("charttitle")
ActiveChart.Axes(xlCategory).Select
With ActiveChart.Axes(xlCategory)
.MinimumScale = 0
.MaximumScale = 300
.MinorUnit = 10
.MajorUnit = 50
.Crosses = xlCustom
.CrossesAt = 0
.ReversePlotOrder = True
.ScaleType = xlLinear
.DisplayUnit = xlNone
End With
End Sub
I then attempt to set the series name, like this:-
ActiveChart.SeriesCollection(1).Name = CStr(some_value) & " Some Text"
When I add this line of code to the sub, it causes the Chart_Calculate function to be activated numerous times in rapid succession, causing the graph to flicker for a few seconds before stabilising.
Does anyone know what the matter is, and what I should do about it?
(This is Excel 2003 running on Windows XP.)
EDIT: PortlandRunner's suggestion cleared up the problem during the graph redrawing, but it still exists if the Graph is visible at startup. See also this.
Try inserting Application.Calculation = xlCalculationManual at the beginning of your code, and Application.Calculation = xlCalculationAutomatic at/near the end of your code.
Trying to print the same excel sheet a number of times (e.g 100) with a cell incremented each time (e.g cell 4F).
I tried using
Workbook_BeforePrint
to increment the cell, but it requires interaction with the "select printer" dialog for each printed sheet.
Would it be possible to make something like:
a = getIntegerUserInput()
for i in 1..a
increment 4F with one
print the sheet suppressing the "select printer" dialog
end for
Cheers
Have you selected a default printer?
I used this:
Sub printinc()
For i = 0 To 3
Range("A1").Value = Range("A1").Value + 1
Sheets("Sheet1").PrintOut
Next
End Sub
It printed 4 copies incrementing the value in cell A1 each time without prompting me for settings or printer selection.
To print a sheet, you can use this kind of code (assuming you know on which printer you want to print) using PrintOut:
Sub PrintFile()
Dim curPrinter As String
curPrinter = Application.ActivePrinter
Application.ActivePrinter = "Myprinter"
ActiveWindow.SelectedSheets.PrintOut
Application.ActivePrinter = curPrinter
End Sub
Hence, you can create a loop to increase a cell and print your worksheet with the increment.
By the way, you could do it using Before_print and if you don't want to display the print dialog, you can set Cancel to False while calling the procedure Private Sub Workbook_BeforePrint( Cancel As Boolean) (ref on MSDN)
You can also read this SO thread to prevent displaying the printing dialog: How do you prevent printing dialog when using Excel PrintOut method.
[EDIT] see Seyren's answer for a working solution on what you want. Yet, take care about the performance if you really wanted to loop 100 times.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
'//supress recursion into this event when we print
Application.EnableEvents = False
'//increment
If Not IsNumeric(ActiveSheet.Range("A1").Value) Then ActiveSheet.Range("A1").Value = 0
ActiveSheet.Range("A1").Value = ActiveSheet.Range("A1").Value + 1
'//do a default print
ActiveSheet.PrintOut
Application.EnableEvents = True
'//prevent the default print
Cancel = True
End Sub