excel macro calling another excel macro in vba - vba

I have made a macro that calls another macro saved on my shared drive. I have taken the help from the suggestions given in the previous question I asked. It is working on the files that are on my local drive, but as soon as I open it from a file from the shared drive it stops working.
The macro name is mymacro.xla
These are the codes:
Dim i As AddIn
Set i = Application.AddIns.Add("M:\nit\USER\nitin kumar\NQK\macro\run1.2.xla", True)
i.Installed = True
I have created a button whose codes is given below:
Dim CoBa As CommandBar
Dim Ctlconst As CommandBarControl
On Error Resume Next
Set CoBa = Application.CommandBars.Add(Name:="Quote Daily Report", temporary:=True)
With CoBa
Set Ctlconst = .Controls.Add(Type:=msoControlButton)
With Ctlconst
.Caption = "Lexington Macro"
.Style = msoButtonIconAndCaptionBelow
.OnAction = "Accounts_Summary"
.FaceId = 483
.TooltipText = "NITIN"
End With
.Visible = True
.Position = msoBarBottom
End With
It's not working any suggestions would be of great help.

The least effort solution would be to copy the XLA locally before adding it.
The root cause could be one a number of things, locking, a read-only folder or security restrictions. It's better not to mess about with that and also that gets you around a bunch of potential problems such as:- what happens if you update the xla while someone is using it?

Related

Macro on called workbook intermittent with it's function

What I have is a macro that is designed to open other workbooks and refresh the contents within those workbooks. All of these work except one of them is intermittent, and by that I mean most times it works exactly as required, but randomly it will bring up an error stating the macro cannot be found. I haven't found so far a point which I can pinpoint where it does this so it has been difficult to debug.
below is the code:
Call Shell("K:\ASA_Reporting\Audits\MDA\ListCompletedAudits.bat")
' This wait has been added to allow the command to run in Console
Application.Wait (Now() + TimeValue("00:00:10"))
ThisWorkbook.Worksheets("Bits n Pieces").Range("G14").Value = "True"
Dim wb1 As Workbook
'This is to update the 5S Audit Dashboard
Set wb1 = Workbooks.Open("K:\ASA_Reporting\Audits\Audit Dashboard v002.xlsb", True, False, , , "password")
Application.Run "'Audit Dashboard v002.xlsb'!Refresh5S"
wb1.Close savechanges:=True
Stop
The point which has a commented out section "This is to update the 5S Audit Dashboard" is the code which is where the issue is occurring. The section above that in regards to the waiting 10 seconds is to correct a different issue which is unrelated to the 5S.
I know that the Macro is correct as it works on the 5S workbook, and it does work most times through my updater, but as I've said on occasion is brings up an error stating the macro does not exist.
I have checked the code what is being called (shown below) but I cannot any errors in this.
Sub Refresh5S()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim datasheet As Worksheet
Set datasheet = ThisWorkbook.Worksheets("Data Sheet")
Dim wb As Workbook
Dim ws As Worksheet
Set wb=Workbooks.Open("\\bosch.com\dfsrb\dfsuk\loc\wo\dept\service\ASA_Reporting\Audits
\5S Audit Master v2.xlsx", True, True)
Set ws = wb.Worksheets("Counts")
datasheet.Range("W6:Z21").Value = ws.Range("B5:E20").Value
datasheet.Range("AB6:AC400").Value = ws.Range("H5:I399").Value
datasheet.Range("AD6:AG400").Value = ws.Range("K5:N399").Value
datasheet.Range("AH6:AH400").Value = ws.Range("O5:O399").Value
wb.Close False
MsgBox "Thank you for your patience." & vbNewLine & vbNewLine & "The figures
should update in a few seconds.", vbInformation
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I thought I had found the answer on a Microsoft help page titled "Macro in Excel Stops After A Workbook.Open Command" However that was for when you press the shift key, and that is not happening in this instance, it is being left to it's on devices.
https://support.microsoft.com/en-us/help/555263
Does anyone have any idea why it randomly decides the Macro in the 5S workbook doesn't exist?
Edit:
Tried the below and it worked for the day without issue, then today the problem came back with the following error:
The code has not been changed except for the suggestions below, which worked for a day.
The issue was in the end with Excel, as the latest updates of Office have now resolved this issue.

Word VBA Application.Quit issue

If (Documents.count = 1) And (ActiveDocument.Name = ThisDocument.Name) Then
Application.DisplayAlerts = False
Application.Quit (wdDoNotSaveChanges)
Else
Application.DisplayAlerts = False
ActiveDocument.Close (wdDoNotSaveChanges)
End If
I'm having issues with the above code, in that when the code clears that first If statement (meaning it is the only document open and the name of the document is as expected), it still won't close out of the Word Application completely. It instead will only close the document and then leave a "blank" Word window open.
I know it is clearing the first If statement because I wrote a quick check of each element to a debug file, and everything shows up as expected. Additionally, if I step through the code it indeed moves along as it should.
Interestingly/Frustratingly if I step through the code in debug mode and get to the section in the code for Application.Quit, it does indeed quit the entire program! So I'm really not sure why it doesn't work when I just run the code as opposed to stepping through it.
Have tried:
1 - Adding an 'Exit Sub' line after Application.Quit
2 - Setting the Word Application as an object explicitly:
Dim wObj As Object
Set wObj = CreateObject("word.Application")
'Application.Quit (wdDoNotSaveChanges) '
wObj.Quit (wdDoNotSaveChanges)
Set wObj = Nothing
3 - Adding a before close event:
Sub DocumentBeforeClose()
ActiveDocument.Saved = True
End Sub
Any help would be much appreciated!
When having this issue in Excel, I found I had to tell Excel to close all documents after calling Application.Quit.
Because Excel can retain a reference to any workbook after the workbook's window closes (workbook still present in VBA Editor, unreleased object references)…
Because I can't always know that my document is the first document that has been opened by this instance of Excel…
Because I must accept the presence of an Excel add-in that can cause workbooks to appear modified immediately after a save (due to changes to invisible worbook properties)…
Because my usecase requires Excel to quit…
Adapted for Microsoft Word:
'Actual handling of unsaved documents omitted from this answer
If savedAllDocuments Or UserDoesNotCare Then
Application.Quit wdDoNotSaveChanges
Dim doc As Document
For Each doc in Application.Documents
doc.Close SaveChanges:=False
Next doc
End If
I had a simular issue with Application.Quit (quitting Word from within an Excel macro). It turned out, that it was related to the status of Application.ScreenUpdating.
The macro hung when in Excel ScreenUpdating was set to false, but continued correctly with:
Application.ScreenUpdating = True

How to deal with Excel popup when using Workbooks.OpenXML

I'm trying to use vbs to open XML via Excel.
Below is my code:
Set myxl = createobject("excel.application")
myxl.DisplayAlerts = False
myxl.Application.Visible = true
myxl.Workbooks.OpenXML "SampleXML.XML",1,xlXmlLoadImportToList
But when code runs, Excel will pop up an option to ask how to open this file.
I'm sorry that I don't have enough reputation to post images. This popup will ask you whether open XML as "As an XML table" (which I want, but I don't want popup), "As a read-only workbook", "Use the XML Source task pane".
How do I aviod this pop up?
The problem is xlXmlLoadImportToList. It is an Excel Constant and not a VBScript Constant. It has a value 2. You can check that in the Immediate window of Excel as shown below.
Try this :)
Const xlXmlLoadImportToList = 2
Set myxl = createobject("excel.application")
myxl.DisplayAlerts = False
myxl.Application.Visible = true
myxl.Workbooks.OpenXML "C:\Test.XML",1,xlXmlLoadImportToList

How do I resolve a false "spreadsheet locked for editing" issue?

Using the Office 2010 suite, I have a PowerPoint presentation where all the charts are linked to an Excel workbook. In order to move the presentation and/or workbook to another directory, all the links must be updated to point to the new workbook location. To do so, I've written the following code which resides in a standard code module in PowerPoint:
Private Sub RedirectLinks()
Dim Source As String
Dim Dest As String
Dim Action As Integer
If InStr(1, ActivePresentation.Path, "Dev\") > 1 Then
Action = MsgBox("Changing pointers to PRODUCTION", vbOKCancel)
Source = "Dev\"
Dest = vbNull
Else
Action = MsgBox("Changing pointers to DEVELOPMENT", vbOKCancel)
Source = "Templates\"
Dest = "Dev\Templates\"
End If
If Action = vbOK Then
Dim SL As Slide
Dim SH As Shape
Dim Top As Double
Dim Left As Double
Dim Width As Double
Dim Height As Double
For Each SL In ActivePresentation.Slides
SL.Select
For Each SH In SL.Shapes
SH.Select
If SH.Type = msoLinkedOLEObject Then 'when we find a linked one
Top = SH.Top
Left = SH.Left
Width = SH.Width
Height = SH.Height
SH.LinkFormat.SourceFullName = Replace(SH.LinkFormat.SourceFullName, Source, Dest)
SH.Top = Top
SH.Left = Left
SH.Height = Height
SH.Width = Width
End If
Next
Next
End If
If InStr(1, Dest, "dev") > 0 Then
Action = MsgBox("About to OVER WRITE the Dev copy with this one." & vbCrLf & "Click 'Cancel' to prevent this and save manually", vbOKCancel, "OVER WRITE WARNING!!")
Else
Action = MsgBox("About to OVER WRITE the PRODUCTION copy with this one." & vbCrLf & "Click 'Cancel' to prevent this and save manually", vbOKCancel, "OVER WRITE WARNING!!")
End If
If Action = vbOK Then
ActivePresentation.SaveAs Replace(ActivePresentation.Path, Source, Dest) & ActivePresentation.Name
End If
End Sub
The code executes just fine, however, I frequently get this message box popping up from Excel when it is executing the SH.LinkFormat.SourceFullName = Replace(SH.LinkFormat.SourceFullName, Source, Dest) line.
Items of note:
The workbook in question is actually closed - I know that it's not open by anyone else (I'm the only one who usually uses it, and the other person who's in there isn't in the office this morning).
It claims the file is locked by 'another user' which is actually me. I can often get this warning by closing the workbook, then immediately reopening it. I don't know if it's a network latency issue (file resides on a server, not locally), or what, but after a few moments of using the workbook, I'll get the workbook is now available for read-write message.
I don't get this warning every time it tries to execute the line that sets the .SourceFullName. Sometimes I'll get it most times, sometimes I won't get it at all, sometimes I'll get it on occasion.
Despite my thoughts of network lag, it doesn't matter how quickly or slowly I debug through the code, I'll get this message at random times.
Flagging either new or old workbooks as Read-only at the OS level does not seem to improve the situation.
However, flagging both seems to get me 2 warnings for each replacement line execution.
Does anyone have any suggestions on how to resolve this?
I've run into odd behaviors when code in PPT opens a PPTM and my Macro security settings are anything tighter than "Open any fool thing". Try dialing your macros security in PPT and Excel as low as they'll go, just as a test, and see if that eliminates the problem.
If anyone knows of a way to set the security options on the fly and reset them after, that'd be even better. It might be possible to do that via the registry prior to doing anything that'd invoke XL.

Customizing a ribbon with VBA in Excel

I've already learned that creating a custom tab is in Excel not possible in this! post (unlike i.e. MSProject)
Specifically, can I change the paths of the macros to the current location ?
Edit
It seems that this page may lead to the answer, but I still don't know how, though
Some more in-detail description of the situation:
The user will download a file, which contains a list of materials, equipment, labor. Each column contains information about quantities, pricing etc
When the user click on this button I want to create (I have created manually), a macro called 'Main' in another workbook launches and copies the whole sheet (the contents) to itself and performs some things that procedures do, on it.
So the problem I'm facing, is twhen I'm sending a new version to the client, he has to put it in the exact location or it won't work. Since there's a mix of Mac and Windows computers involved, I'd rather see a situation where the button is assigned to the procedure when the user opens WorkBook B (the one that contains the code).
This way, a new version has to be openened only once, and then for continuous use, the user can just open the downloaded file, click the appropriate button and WorkBook B will open itself and execute.
Maybe there's other ways to go about this. I haven't checked if it's not easier to assign a button to the quick access toolbar...
This is some code I use to add a custom toolbar:
Set cbToolbar = Application.CommandBars.Add(csToolbarName, msoBarTop, False, True)
With cbToolbar
Set ctButton1 = .Controls.Add(Type:=msoControlButton, ID:=2950)
Set ctButton2 = .Controls.Add(Type:=msoControlButton, ID:=2950)
Set ctButton3 = .Controls.Add(Type:=msoControlButton, ID:=2950)
End With
With ctButton1
.Style = msoButtonIconAndCaption
.Caption = "Set &Picklists"
.FaceId = 176
.OnAction = "SetPicklist"
End With
With ctButton2
.Style = msoButtonIconAndCaption
.Caption = "Set &Defaults"
.FaceId = 279
.OnAction = "SetDefaults"
End With
With ctButton3
.Style = msoButtonIconAndCaption
.Caption = "&Visibility Settings"
.FaceId = 2174
.OnAction = "VisibilitySettings"
End With
With cbToolbar
.Visible = True
.Protection = msoBarNoChangeVisible
End With
The 'OnAction' controls the macro that runs... If you wanted to expand that to run a macro on a specific workbook, use "whatever.xls!MacroName"