I'm using the latest Office 365 Excel on OS X. I've created a List Box Form Control (ActiveX controls don't seem to be available in OS X), called wb_from:
I'm trying to access this List Box from VBA using this code:
Sub my_Import()
Dim MailStr As String
MailStr = ""
If wb_from.SelectedItems.Count = 0 Then
MsgBox "No User Selected"
Exit Sub
End If
For i = 0 To (wb_from.Items.Count - 1)
If wb_from.Selected(i) Then
MailStr = MailStr & wb_from.Items.Item(i) & "; "
End If
Next i
a = 100
End Sub
Excel is giving me an object required error:
How can I correct this?
What cell did you link that control to via Format Control? That cell address should be used in your code.
Related
I am able to add easily create a SharePoint List using Excel VBA with the following code snippet:
ActiveSheet.ListObjects("Table3").Publish Array( _
"http://sitebuilder2.xyzcompany.com/operations", "test1"), False
However, I get an "Run-time error '3003': Application-defined or object-defined error" if I use the same list name ("test1") because the list already exists.
Is there a straight-forward way to either overwrite list ("test1"), or delete list ("test1") in SharePoint using Excel VBA, so that I can update the entire list without providing a new List name?
I was able to synchronize to the SharePoint list using this code.
Private Sub PublishRW()
Dim listPoint As Range
Dim fullServerName As String
Dim retUrl As String
Set listPoint = ActiveCell.ListObject.Range(1, 1)
fullServerName = ServerName.Value & "_vti_bin"
If ListDescription.Value = "" Then
retUrl = ActiveCell.ListObject.Publish(Array(ServerName.Value, ListName.Value), False)
Else
retUrl = ActiveCell.ListObject.Publish(Array(ServerName.Value, ListName.Value, ListDescription.Value), False)
End If
If retUrl <> "" Then
ActiveCell.ListObject.Delete
ActiveSheet.ListObjects.Add xlSrcExternal, Array(fullServerName, ListName.Value), True, xlYes, listPoint
Else
MsgBox "There was an error during publish, please check the server name"
End If
Unload ExportRWList
End Sub
I have a working Excel sheet in Excel 2010 on Windows 10 which is failing on my client's side on Excel 2011 for Mac because of the "argument not optional" error when they click on the button. Additionally, it isn't working on their end on a different computer running Office 365.
It seems to be highlighting Characters in the 6th line of code:
Sub Rectangle4_Click()
Dim xSelShp As Shape, xSelLst As Variant, I As Integer
Set xSelShp = ActiveSheet.Shapes(Application.Caller)
Set xLstBox = ActiveSheet.ListBox4
If xLstBox.Visible = False Then
xLstBox.Visible = True
xSelShp.TextFrame2.TextRange.Characters.Text = "SAVE"
Else
xLstBox.Visible = False
xSelShp.TextFrame2.TextRange.Characters.Text = "SELECT"
For I = xLstBox.ListCount - 1 To 0 Step -1
If xLstBox.Selected(I) = True Then
xSelLst = xLstBox.List(I) & ";" & xSelLst
End If
Next I
If xSelLst <> "" Then
Range("R4") = Mid(xSelLst, 1, Len(xSelLst) - 1)
Else
Range("R4") = ""
End If
End If
End Sub
The ListBox it is showing/hiding is an ActiveX ListBox.
I doubt it's relevant, but there are 40 of these Rectangle buttons in the sheet, all with near identical code, just switching 4 references.
Can anyone see what I'm missing here? Or know anything about cross-platform issues with code like this?
Thank you.
In case anyone comes across this, it does seem to be that Tim was correct and ActiveX controls do not work on a Mac. But they also did not work in Office 365. I switched this to form controls and got everything working perfectly on my client's side. My advice to my past self is to work with form controls from the beginning and avoid ActiveX at all costs if you are working on something you are sending to someone else.
I am familiar with the VBA routines needed for customising the Excel ribbon of Excel 2013 and below.
When trying to open the file on Excel 365 I get an error message:
Here is the code I use (that works on Excel 2010):
Sub CreateMenu()
' Delete the CommandBar if it exists already
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("My Tool").Delete
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
With cControl
.Caption = "My Tool"
.Style = msoButtonCaption
End With
End Sub
How should I modify the code to run on both Excel 2010 and Excel 365 versions?
Your question is actually two questions IMO.
How to make to code work for each version (differentiate between Office Versions)
How to make the code work for Office 365
I did some research on Office 365 Ribbon Customization and found a few things I hope can help.
It has become much harder to differentiate between Office versions since Office365/2019. You used to be able to just use Select Case Int(Application.Version) coupled with Case 11/14 etc. But now everything 2016 and above just returns Case 16.
I found a function to differentiate between Office Versions and also some information that CommandBars("Worksheet Menu Bar").Controls.Add has been "superseded by the new ribbon component of the Microsoft Office Fluent user interface."
I don't have Office 365 to test how to modify your code, but once you get that part working, this is how you can implement the solution:
Private Sub Workbook_Open()
If CStr(AppVersion) = 365 Then
MsgBox "Office 365" 'Setup new code here for Office365
' See --> https://learn.microsoft.com/en-us/office/vba/api/office.commandbarcontrols.add
' Note: The use of CommandBars in some Microsoft Office applications has been superseded by the new ribbon component of the Microsoft Office Fluent user interface.
' For more information, see Overview of the Office Fluent ribbon.
' https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/overview-of-the-office-fluent-ribbon
Else
MsgBox "Non-Office 365" ' Insert known working code here for older versions of Office/Excel or call seperate sub for Non-Office 365
End If
End Sub
Private Function AppVersion() As Long
'Test the Office application version
'Written by Ken Puls (www.excelguru.ca)
'https://www.excelguru.ca/blog/2019/02/11/check-the-application-version-in-modern-office/
Dim registryObject As Object
Dim rootDirectory As String
Dim keyPath As String
Dim arrEntryNames As Variant
Dim arrValueTypes As Variant
Dim x As Long
Select Case Val(Application.Version)
Case Is = 16
'Check for existence of Licensing key
keyPath = "Software\Microsoft\Office\" & CStr(Application.Version) & "\Common\Licensing\LicensingNext"
rootDirectory = "."
Set registryObject = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & rootDirectory & "\root\default:StdRegProv")
registryObject.EnumValues &H80000001, keyPath, arrEntryNames, arrValueTypes
On Error GoTo ErrorExit
For x = 0 To UBound(arrEntryNames)
If InStr(arrEntryNames(x), "365") > 0 Then
AppVersion = 365
Exit Function
End If
If InStr(arrEntryNames(x), "2019") > 0 Then
AppVersion = 2019
Exit Function
End If
Next x
Case Is = 15
AppVersion = 2013
Case Is = 14
AppVersion = 2010
Case Is = 12
AppVersion = 2007
Case Else
'Too old to bother with
AppVersion = 0
End Select
Exit Function
ErrorExit:
'Version 16, but no licensing key. Must be Office 2016
AppVersion = 2016
End Function
For the purpose of version control for my file, I want to be able to run a script that compares Cell A1 on Sheet VC, to the same cell/sheet of a version stored on Sharepoint when I run my script. Fairly new to using VBA and cant work out how to do it and cant find the answer im looking for on google.
The code I want to use:
Public Sub version_control()
Sheets("VC").Calculate
If Sheets("VC").Range("A1").Value <> (this is where I want it to check cell A1 sheet VC on the Sharepoint file)
MsgBox "Please download the latest version from the Sharepoint"
Application.Quit
End If
End Sub
Guessing, you don't already have the SharePoint file open ... if that's true skip down.
But if it's open you can reference it like any other open workbook ... e.g. both of these should work:
debug.Print Workbooks("MySharePointWorkbook.xlsx").Sheets("VC").Range("A1").Value
debug.Print Workbooks.Item(n).Sheets("VC").Range("A1").Value
Probably not already open right? Without getting into the weeds of external data links, I would just obtain the full URL of the SharePoint file (open it, ? Activeworkbook.FullName in the Immediate Window) and store that string in serverFileName like this:
Public Sub version_control()
Dim serverFileName As String 'obtain url for sharepoint filename, insert below
Dim valuesAreDifferent As Boolean 'so we can do housekeeping below
Dim x As New Excel.Application 'make a new session for the sharepoint version
Dim w As Workbook 'grab-handle for the sharepoint file
Sheets("VC").Calculate
valuesAreDifferent = False 'implicit, being explicit
serverFileName = "http://whatever-domain.com/MySharepointWorkbook.xlsx"
x.Visible = False 'so it doesn't flash up when checking
Set w = x.Workbooks.Open(serverFileName) 'open the sharepoint version
If Sheets("VC").Range("A1").Value <> w.Sheets("VC").Range("A1").Value Then _
valuesAreDifferent = True
'housekeeping in case we don't quit
w.Close
x.Quit
Set w = Nothing
Set x = Nothing
If valuesAreDifferent Then
MsgBox "Please download the latest version from the Sharepoint"
Application.Quit
End If
End Sub
Context: I have 10 text boxes (ID1 To ID10) in a user form. The userform will also have a clear button, which will allow the user to clear all the values previously entered in the text box. For that I have inserted the below mentioned command in the Clear Button.
I have multiple commands with the same nomenclature except the number which varies with every text box. I wish to enter one command which will change number and execute all the commands.
Simple example given below: I wish to only put one command instead of the folloing:
Private Sub btnClear_Click()
'Empty ID1
ID1.Value = ""
'Empty ID2
ID2.Value = ""
'Empty ID3
ID3.Value = ""
.
.
. and so on till
.
'Empty ID10
ID10.Value = ""
End Sub
I know there is a solution to this, but since I am new cannot find on google using the correct key words. Sorry if this already exists. Any help will be appreciated. Thank you.
If these are TextBoxes on a UserForm, you will be able to do the following from a macro in the UserForm's code module:
Private Sub btnClear_Click()
Dim i As Long
For i = 1 To 10
Me.Controls("ID" & i).Value = ""
Next
End Sub
It's not possible to dynamically generate a variable and/or object name but, because the Controls collection can be indexed by the "name" of the control, it gives an alternative way of getting at the objects.
Adding in code for "option boxes" (OptionButtons? `CheckBoxes?) mentioned in comments, and changing the TextBox names to end with a "C":
Private Sub btnClear_Click()
Dim i As Long
For i = 1 To 10
Me.Controls("ID" & i & "C").Value = ""
Me.Controls("OB" & i).Value = False
Next
End Sub
Do you mean something like this?
For i=1 to 4
str="ID" & i & ".Value = ''"
Evaluate(str)
Next