I have Microsoft Office 365 2019.
I asked a question with an answer about "Insert Note" with .Fill.UserPicture.
I still have some error.
See Screencast.
I found the problem and it is SendKeys "+{F2}" (after inserting note it's open Note).
Sub Special_Note2_FillPicture(control As IRibbonControl)
Dim img As FileDialog
Dim i_add As String
Set img = Application.FileDialog(msoFileDialogFilePicker)
img.AllowMultiSelect = False
img.Title = "Select the Image!"
img.Show
If img.SelectedItems.Count < 1 Then
MsgBox "No Image Selected"
Exit Sub
Else
i_add = img.SelectedItems(1)
End If
Dim myComm As Comment
If Not ActiveCell.Comment Is Nothing Then
If MsgBox("The cell already contains a note, delete?", 4) - 7 Then
ActiveCell.Comment.Delete
Else: Exit Sub
End If
End If
Set myComm = ActiveCell.AddComment
With myComm.Shape
.Height = 110
.Width = 200
.AutoShapeType = 1 'form
' .Fill.UserTextured
.Fill.UserPicture i_add
.Line.ForeColor.RGB = RGB(255, 0, 0)
.DrawingObject.Font.Name = "Consolas"
.DrawingObject.Font.FontStyle = "normal"
.DrawingObject.Font.Size = 8
End With
'emulate the choice of "Change note"
SendKeys "+{F2}"
End Sub
0Key i find solution. I rewrote the code a little bit:
Sub Note_FiilPictureDialog(control As IRibbonControl)
Dim img As FileDialog
Dim i_add As String
Dim myComm As Comment
Set img = Application.FileDialog(msoFileDialogFilePicker)
img.AllowMultiSelect = False
img.Title = "Select the Image!"
img.Show
If img.SelectedItems.Count < 1 Then
Exit Sub
Else
i_add = img.SelectedItems(1)
End If
'If the cell contains a `Note` delete!
If Not ActiveCell.Comment Is Nothing Then
ActiveCell.Comment.Delete
End If
On Error GoTo nexterr
ActiveCell.ClearComments
Set myComm = ActiveCell.AddComment
With myComm.Shape
.Height = 110
.Width = 200
.AutoShapeType = 1 'form
' .Fill.UserTextured
.Fill.UserPicture i_add
.Line.ForeColor.RGB = RGB(255, 0, 0)
.DrawingObject.Font.Name = "Consolas"
.DrawingObject.Font.FontStyle = "normal"
.DrawingObject.Font.Size = 8
'emulate the choice of "Change note".
SendKeys "+{F2}"
Exit Sub
nexterr:
MsgBox "You can only select images!", vbCritical, "Error"
ActiveCell.ClearComments
End With
End Sub
Related
I have a userform and a Click button on it. On click of this button a combobox is dynamically created. I want to do something when a particular value is selected from this combobox but the change event is not getting triggered. What could be the reason.
Here is my code which is put in the UserForm1 module.
Private WithEvents ComboBox1 As MSForms.ComboBox
Private Sub ClickButton_Click()
'Create combo box
Dim ComboBox1 As MSForms.ComboBox
Set ComboBox1 = Me.Controls.Add("Forms.ComboBox.1")
With ComboBox1
.Left = 160
.Top = 50
.Width = 70
.Height = 20
.AddItem ("> than")
.AddItem ("< than")
.AddItem ("Max")
.AddItem ("Min")
.Enabled = True
.BackColor = RGB(255, 255, 255)
.ForeColor = RGB(0, 0, 0)
.SpecialEffect = fmSpecialEffectFlat
.Font.Size = 12
.Font.Bold = False
.Font.Name = "Arial"
.TabIndex = 2
End With
DoEvents
ComboBox1.SetFocus
End Sub
Private Sub ComboBox1_Change()
Dim inputNumber As Variant
If ComboBox1.Value = "> than" Then
inputNumber = InputBox("Enter a number:")
'Check if the input is valid number
If IsNumeric(inputNumber) Then
ComboBox1.Value = ComboBox2.Value & " " & inputNumber
Else
MsgBox "Invalid input"
End If
End If
End Sub
The method you need to use is described here: https://stackoverflow.com/a/8986622/9852011 , but for your particular case, here is what you need to do:
This is the code that should be in the module of your UserForm:
Private m_oCollectionOfEventHandlers As Collection
Private Sub UserForm_Initialize()
Set m_oCollectionOfEventHandlers = New Collection
End Sub
Private Sub CommandButton1_Click()
Dim ComboBox1 As MSForms.ComboBox
Set ComboBox1 = Me.Controls.Add("Forms.ComboBox.1")
With ComboBox1
.Left = 160
.Top = 50
.Width = 70
.Height = 20
.AddItem ("> than")
.AddItem ("< than")
.AddItem ("Max")
.AddItem ("Min")
.Enabled = True
.BackColor = RGB(255, 255, 255)
.ForeColor = RGB(0, 0, 0)
.SpecialEffect = fmSpecialEffectFlat
.Font.Size = 12
.Font.Bold = False
.Font.Name = "Arial"
.TabIndex = 2
End With
DoEvents
ComboBox1.SetFocus
Dim cb1EventHandler As comboboxeventhandler
Set cb1EventHandler = New comboboxeventhandler
Set cb1EventHandler.ComboBox = ComboBox1
m_oCollectionOfEventHandlers.Add cb1EventHandler
End Sub
Then, insert a new class module into your project, name it "ComboBoxEventHandler" and put this code into it:
Private WithEvents m_oComboBox As MSForms.ComboBox
Public Property Set ComboBox(ByVal oComboBox As MSForms.ComboBox)
Set m_oComboBox = oComboBox
End Property
Private Sub m_oComboBox_Change()
Dim inputNumber As Variant
With m_oComboBox
If .Value = "> than" Then
inputNumber = InputBox("Enter a number:")
'Check if the input is valid number
If IsNumeric(inputNumber) Then
.Value = .Parent.ComboBox2.Value & " " & inputNumber
Else
MsgBox "Invalid input"
End If
End If
End With
End Sub
I don't know what "ComboBox2" is but for the sake of this example, I just assumed it is a ComboBox which already exists in the UserForm somewhere.
I have Microsoft Office 365 2019.
First of all i want to tell how code works:
Insert Note.
Click on Cell who has inserted Note.
Press Ctrl+N
Then you will see "PopUp-Menu".
I have VBA code (to work put in ThisWorkbook):
Private Sub Workbook_Open()
Application.OnKey "^{n}", CodeName & ".ContextMenu"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^{n}"
End Sub
Private Sub ContextMenu()
If ActiveCell Is Nothing Then Exit Sub
If ActiveCell.Comment Is Nothing Then Exit Sub
On Error Resume Next 'Can be and without inadequate to, but then with brute force(cycle) CommandBars.
Dim cb As CommandBar
Set cb = Application.CommandBars("vbaPopup")
If cb Is Nothing Then CreateContextMenu
Application.CommandBars("vbaPopup").ShowPopup
End Sub
Private Sub CreateContextMenu()
Dim a1_icon, a1_file, a2, a3, i&, m$, p$, f$: m = CodeName & ".": p = Path & "\Image\"
a1_icon = Array(76, 72, 178, 53)
a1_file = Array("NoteZoom_200x110.jpg", "NoteZoom_600x400.jpg", "Full Screen.jpg", "NoteZoom_InputBox.jpg", "Copy Text.jpg")
a2 = Array("NoteZoom 200x110", "NoteZoom 600x400", "Note <Full Screen>", "NoteZoom InputBox", "Скопировать текст примечания")
a3 = Array("NoteZoom1", "NoteZoom2", "NoteZoom3", "NoteZoom_InputBox", "NoteTextToClipboard")
With Application.CommandBars.Add("vbaPopup", msoBarPopup, , True) 'You can also not do to make the context menu temporary.
For i = 0 To UBound(a1_file) 'Ubound(a1_ico)
With .Controls.Add
f = p & a1_file(i)
If Len(Dir(f)) Then
.Picture = LoadPicture(f)
Else
.FaceId = a1_icon(i) 'If the file is not found, the icon. But it's not necessary.
End If
.Caption = a2(i)
.OnAction = m & a3(i)
End With
Next
End With
End Sub
Private Sub NoteZoom1(): NoteChangeSize 200, 110: End Sub
Private Sub NoteZoom2(): NoteChangeSize 600, 400: End Sub
Private Sub NoteZoom3()
With ActiveWindow.VisibleRange
NoteChangeSize .Width, .Height, True
'With .Resize(.Rows.Count - 1, .Columns.Count - 1) 'Without check
' NoteChangeSize .Width, .Height, True
'End With
End With
End Sub
Private Sub NoteChangeSize(w!, h!, Optional scr As Boolean)
With ActiveCell.Comment.Shape
.Width = w: .Height = h
If scr Then .Top = 0: .Left = 0: .Visible = msoTrue
End With
End Sub
'To create a `Note` with `InputBox`.
Private Sub NoteZoom_InputBox()
'Ниже 2 строчки для проверки наличия `Примечания`.
If ActiveCell Is Nothing Then Exit Sub
If ActiveCell.Comment Is Nothing Then Exit Sub
Dim lH As Long 'height
Dim lW As Long 'width
lH = Application.InputBox("Choose the HEIGHT of the notes ")
lW = Application.InputBox("Choose the WIDTH of the notes ")
With ActiveCell.Comment
' .Text Text:="Note:" & Chr(10) & ""
.Shape.Height = lH
.Shape.Width = lW
End With
End Sub
Private Sub NoteTextToClipboard()
With New DataObject
.SetText ActiveCell.Comment.Text
.PutInClipboard
End With
End Sub
For more details you can download my Excel Workbook to see how it's implemented!
Also i find code on this site Ron de Bruin. I wish to add "Submenu" in my "Menu"! Wrote out only those codes which can help to create "Submenu". But how to combine I don't know!?
Dim MenuItem As CommandBarPopup
'Add PopUp menu
With Application.CommandBars.Add(Name:=Mname, Position:=msoBarPopup, _
MenuBar:=False, Temporary:=True)
'Add menu with two buttons
Set MenuItem = .Controls.Add(Type:=msoControlPopup)
With MenuItem
.Caption = "My Special Menu"
With .Controls.Add(Type:=msoControlButton)
.Caption = "Button 1 in menu"
.FaceId = 71
.OnAction = "'" & ThisWorkbook.Name & "'!" & "TestMacro"
End With
In the end I want to get this:
0Key finally i find some solution for this. Right now it's looks like this:
Looks cool right? Download full code link.
is it possible to generate a combobox/dropdownlist inside a cell that is currently active? I tried this but nothing happened:
Programmatically add a drop down list to a specific cell
my client wants any cell that has been clicked from column A (except A1 because it serves as the Column header) to have dropdowns with list of items.
I also tried copying this and see if it runs but it always go to the On Error Resume Next
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
On Error GoTo errHandler
If Target.Count > 1 Then GoTo exitHandler
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End If
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 5
.ListFillRange = ws.Range(str).Address
.LinkedCell = Target.Address
End With
cboTemp.Activate
End If
exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
errHandler:
Resume exitHandler
End Sub
I'm trying to use autofill drop-down list in Excel
The code which i used.I got it from here
http://www.contextures.com/DataValComboboxClick.zip
All the sudden it stopped working (worked for 2 months before)
Now i am getting 438 error
"Object does not support this property or method" in this line: .Value
= ""
The weird thing is that when i try to type the following in the immediate window: ?cbotemp.value, the promt shows me that cbotemp object does not have a Value property at all
Any help will be highly appreciated. I'm trying to work it out all night long and now it becomes desperate.
Here is the source code:
Option Explicit
' Developed by Contextures Inc.
' www.contextures.com
Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'Hide combo box and move to next cell on Enter and Tab
Select Case KeyCode
Case 9
ActiveCell.Offset(0, 1).Activate
Case 13
ActiveCell.Offset(1, 0).Activate
Case Else
'do nothing
End Select
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
Cancel = True
End If
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
With cboTemp
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 5
.ListFillRange = ws.Range(str).Address
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.TempCombo.DropDown
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = "" 'here i get 438 error
End With
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
The error is in this part of code:
...
Dim cboTemp As OLEObject
...
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End If
Since cboTemp is of type OLEObject it really has not a property Value. But the On Error Resume Next should prevent this error from breaking the program.
If this is not (or not more) the case, then the setting in:
VBA Editor - Tools > Options > General > Error Trapping
is set to Break on all errors.
Default is Break on unhandled errors.
Set it back to default or simply do not set .Value="" at all. It is not necessary.
I am trying to create a workaround for the Data Validation Input Message, since my input message is more than 255 chars.
I have tried http://contextures.com/xlDataVal12.html but the text box is fixed. I would need the text box or label to move with the selected cell.
On the image below, you can see the issue. We cannot display the whole message within the input box.
1 http://img5013.photobox.co.uk/42779160c8143d2fcab8c396d411e8b621181c1be9f1a01fb62e272d26debaf4b53f7657.jpg
Using the Contextures code, you need to set the .Top and .Left properties of the shape to the same properties of a cell. Here's a rewrite of that code that moves the textbox near the cell.
' Developed by Contextures Inc.
' www.contextures.com
' modified by Dick Kusleika 7/21/2015
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim sTitle As String
Dim sMsg As String
Dim sMsgAdd As String
Dim tbxTemp As Shape
Dim lDVType As Long
Dim lRowMsg As Long
Dim ws As Worksheet
Application.EnableEvents = False
Set ws = Target.Parent
Set tbxTemp = ws.Shapes("txtInputMsg")
On Error Resume Next
lDVType = 0
lDVType = Target.Validation.Type
On Error GoTo errHandler
If lDVType = 0 Then
tbxTemp.TextFrame.Characters.Text = vbNullString
tbxTemp.Visible = msoFalse
Else
If Len(Target.Validation.InputTitle) > 0 Or Len(Target.Validation.InputMessage) > 0 Then
sTitle = Target.Validation.InputTitle & vbLf
On Error Resume Next
lRowMsg = 0
lRowMsg = Application.WorksheetFunction.Match(Target.Validation.InputTitle, Sheets("MsgText").Columns(1), 0)
If lRowMsg > 0 Then
sMsgAdd = Me.Parent.Sheets("MsgText").Cells(lRowMsg, 2).Value
End If
On Error GoTo errHandler
sMsg = Target.Validation.InputMessage
With tbxTemp.TextFrame
.Characters.Text = sTitle & sMsg & vbLf & sMsgAdd
.Characters.Font.Bold = False
.Characters(1, Len(sTitle)).Font.Bold = True
End With
tbxTemp.Top = Target.Offset(1, 1).Top
tbxTemp.Left = Target.Offset(1, 1).Left
tbxTemp.Visible = msoTrue
tbxTemp.ZOrder msoBringToFront
Else
tbxTemp.TextFrame.Characters.Text = vbNullString
tbxTemp.Visible = msoFalse
End If
End If
errHandler:
Application.EnableEvents = True
End Sub