How to create Excel Controls (Combo boxes etc) using Visual Basic - vb.net

I'm working on a project to create and email a spreadsheet that is created by an ASP.NET/Visual Basic web application.
The top of this spreadsheet is one large cell, in which sits a Drop Down List, two Text Boxes, a few Labels and a Checkbox.
How do I create and organise these controls using Visual Basic? The spreadsheet is being physically created and I can manipulate cells at the moment, but I do not know how to create those controls specifically.

If you want to create these controls programmatically then this code should get you going, Essentially add a control, position it and set those properties that you require.
Sub addControls()
With Sheets("Sheet1")
.Columns(1).ColumnWidth = 60
.Rows(1).RowHeight = 150
'label
Set addedLbl = .OLEObjects.Add(ClassType:="Forms.Label.1")
With addedLbl
.Left = 180
.Top = 25
.Width = 90
.Object.BackColor = &HC0C0FF
.Object.Caption = "A Coloured Label"
End With
'combobox
Set addedCmbo = .OLEObjects.Add(ClassType:="Forms.Combobox.1")
With addedCmbo
.Left = 180
.Top = 75
.Width = 90
.Height = 20
End With
'checkbox
Set addedChkBox = .OLEObjects.Add(ClassType:="Forms.CheckBox.1")
With addedChkBox
.Left = 25
.Top = 25
.Width = 90
.Height = 20
.Object.Caption = "A Checkbox"
.Object.SpecialEffect = 2
.Object.Value = False
End With
'textbox
Set addedTextBox = .OLEObjects.Add(ClassType:="Forms.TextBox.1")
With addedTextBox
.Left = 25
.Top = 75
.Width = 90
.Height = 50
.Object.WordWrap = True
.Object.MultiLine = True
.Object.SpecialEffect = 1
.Object.Text = "Some wrapped text in this raised textbox."
End With
End With
End Sub
If you want to add them manually then select the Developer Tab and in the Controls group select Insert. You can then add controls to the worksheet. I have used ActiveX controls. Then right-click the control to list its Properties.

Related

combobox addItem strangely not working - remains blank

very simple Problem:
Dim myForm As Object
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
Set MyComboBox = myForm.Designer.Controls.Add("Forms.ComboBox.1")
With MyComboBox
.Left = 100
.Top = 20 + x * 10
.Height = 16
.Width = 100
For Each col In fields
.AddItem col
Next
End With
I can clearly print out the values in fields (which is a variant), but somehow the Combobox remains blank... Any ideas? I'm wasting way to much time on this...
thanks for your help

Resize excel comments to fit text with specific width

I'd like to have the comment box fit the comments just right (no extra space at the bottom).
I know there is the .AutoSize but I want the maximum width to be 300.
Here is the code I have,
For Each mycell In myRng.Cells
If Not (mycell.Comment Is Nothing) Then
With mycell.Comment.Shape
.TextFrame.AutoSize = True
If .width > 300 Then
lArea = .width * .height
.width = 300
.height = (lArea / 300)
End If
End With
End If
Next mycell
mycell and myRng are Range datatypes, lArea is Long.
Now, this works relatively well but leaves extra space at the bottom of a number of comments because the area the AutoSized text takes up is different from the area of the AutoSized comment box.
Is there a way to check for blank space inside a comment and then trim it? Or is what I have the best it is going to be?
try this ... test comment has been placed in cell E4
discovered by putting Range("e4").Comment.Shape.TextFrame in the Watch window
Sub testComment()
With Range("e4").Comment.Shape
.TextFrame.AutoSize = True
lArea = .Width * .Height
.Width = 300
.Height = (lArea / .Width) ' used .width so that it is less work to change final width
.TextFrame.AutoMargins = False
.TextFrame.MarginBottom = 0 ' margins need to be tweaked
.TextFrame.MarginTop = 0
.TextFrame.MarginLeft = 0
.TextFrame.MarginRight = 0
End With
End Sub
I've changed the code in the previous comment to only resize the box if width is above 300 because otherwise the final size of small boxes were messed up. Also changed to go through all comment box on activesheet
Sub reset_box_size()
Dim pComment As Comment
For Each pComment In Application.ActiveSheet.Comments
With pComment.Shape
.TextFrame.AutoSize = True
lArea = .Width * .Height
'only resize the autosize if width is above 300
If .Width > 300 Then .Height = (lArea / .Width) ' used .width so that it is less work to change final width
.TextFrame.AutoMargins = False
.TextFrame.MarginBottom = 0 ' margins need to be tweaked
.TextFrame.MarginTop = 0
.TextFrame.MarginLeft = 0
.TextFrame.MarginRight = 0
End With
Next
End Sub
there is an autosizefunction.
here is a small code to show how to use:
Dim Rg_Com as Range , Rg_Value as Range
Set Rg_Com = Cells(1,1)
Set Rg_Value = Cells(1,2)
'Comment in A1 will be same as Text in B1:
With Rg_Com
.ClearComments
.AddComment
With .Comment
.Text Text:=Rg_Value.Value2
.Shape.TextFrame.AutoSize = True '<<< just to make all text visible in one comment, all chars having the basic size
End With
End With

Replicate an object in powerpoint using vba?

I want to replicate an selected object in powerpoint using VBA code. I have a following code mention below
Sub CopySizeAndPosition()
' Usage: Select two shapes. The size and position of
' the first shape selected will be copied to the second.
Dim w As Double
Dim h As Double
Dim l As Double
Dim t As Double
With ActiveWindow.Selection.ShapeRange(1)
w = .Width
h = .Height
l = .Left
t = .Top
End With
With ActiveWindow.Selection.ShapeRange(2)
.Width = w
.Height = h
.Left = l
.Top = t
End With
End Sub
But I want to specify my value instead of getting object value. So, please help and thanx in advance!
Assuming you have selected a single shape, you can set your values like this:
' Sets the size and position of the first shape in a selection
Sub SetShapeSizeAndPosition()
With ActiveWindow.Selection.ShapeRange(1)
.Width = 100
.Height = 100
.Left = 100
.Top = 100
End With
End Sub

RE: Getting the name of the object onFocus & Adding Caption to a Button dynamically Created

I have written VBA code for Dynamically creating textboxes and buttons. The following is the code when I hit an "Add" button on the userform.
Dim oTxtBox As Control
Dim oBrwsBtn As Control
Dim oCaption As Control
Dim oTxtLen As Integer
oTxtLen1 = TextBox1.Width
oTxtBrth1 = TextBox1.Height
oTxtPos1 = TextBox1.Left
oButLen = CommandButton1.Width
oButBrth = CommandButton1.Height
oTxtLen2 = TextBox2.Width
oTxtBrth2 = TextBox2.Height
If i = Empty Then
i = 1
End If
Set oTxtBox = Me.Controls.Add("Forms.TextBox.1")
Set oBrwsBtn = Me.Controls.Add("Forms.CommandButton.1")
Set oCaption = Me.Controls.Add("Forms.TextBox.1")
With oTxtBox
.Left = oTxtPos1
.Top = oTxtBrth1 + 18 + (oTxtBrth + 18) * (i - 1)
.Width = oTxtLen1
.Height = oTxtBrth1
End With
With oBrwsBtn
.Left = oTxtPos1 + oTxtLen1 + 18
.Top = oTxtBrth1 + 18 + (oTxtBrth + 18) * (i - 1)
.Width = oButLen
.Height = oButBrth
End With
With oCaption
.Left = oTxtPos1 + oTxtLen1 + 18 + oButLen + 18
.Top = oTxtBrth1 + 18 + (oTxtBrth + 18) * (i - 1)
.Width = oTxtLen2
.Height = oTxtBrth2
End With
i = i + 1
Q1 Now How to Edit the caption of the browse Button which I create dynamically No method .Caption with oBrWsBtn
And Q2: How to get the value when the focus is changed
For example When I click on 'TextBox1' Object. A variable should assign itself with the name (i. e. var(str) = focus object name)
Thanks in advance
Q1: Why don't you try defining the control type more specifically and see if that permits you to use the .Caption method. Don't understand why it's not working now, but maybe the Control object type doesn't have the .Caption method because not all controls have captions.
Try:
'at the top of the sub:
Dim oBrwsBtn as CommandButton
'then later
Set oBrwsBtn = Me.Controls.Add("Forms.CommandButton.1")
Another option to consider would be to create these buttons and just have them be invisible until needed. But that assumes you don't actually have a good reason to create them dynamically, which you may have.
Q2: Use a control_AfterUpdate sub:
'Dim this wherever appropriate for scope. Could be in the afterupdate sub if that works.
dim TextBox1_Value as string
Private Sub TextBox1_AfterUpdate()
TextBox1_Value = TextBox1.value
End Sub

How to pass control values between userform events?

I'm struggling with a userform (called Label_Select) that I created.
I'm initializing the userform with some text boxes and check boxes and assigning some values in them.
Then I have a OK button on the userform that was created at design mode (I can create this button at runtime if that helps).
I need to use the text boxes and check boxes values in the code of the OK_Click, refer below.
Currently I get a "Sub or Function not defined" for the OK_Click sub.
How can I pass the text boxes and check boxes values between the userform initialize code and other click events of the userform?
Thank you for your responses
Private Sub UserForm_Initialize()
Dim LotBox(500) As MSForms.TextBox
Dim SensorCheckBox(500) As MSForms.CheckBox
For i = 1 To 4
For j = 1 To 4
k = i + (4 * j)
Set LotBox(k - 4) = Label_Select.Controls.Add("Forms.TextBox.1")
Set SensorCheckBox(k - 4) = Label_Select.Controls.Add("Forms.CheckBox.1")
With LotBox(k - 4)
.Top = 250 + i * 25
.Left = (j * 80) - 50
.Width = 40
.Height = 30
.Font.Size = 14
.Font.Name = "Calibri"
.SpecialEffect = fmSpecialEffectSunken
.Value = k
.AutoSize = True
End With
With SensorCheckBox(k - 4)
.Top = 246 + i * 25
.Left = (j * 80) - 8
.Height = 30
End With
If LotBox(k - 4).Value = " " Then
Label_Select.Controls.Remove LotBox(k - 4).Name
Label_Select.Controls.Remove SensorCheckBox(k - 4).Name
End If
Next j
Next i
End Sub
Private Sub OK_Click()
Worksheets("Sheet1").Cells(1,1)=LotBox(1).Value
Worksheets("Sheet1").Cells(2,1)=SensorCheckBox(1).Value
End Sub
Try making LotBox and SensorCheckBox public variables
You've declared LotBox and SensorCheckBox within UserForm_Initialize, so as soon as that sub ends they will both go out of scope.
Move them up to the top of the module as Global variables.