I have multipage userform. I created a button, when I click to this button it adds a Combobox and some labels and textbox. Later I need to create a change funtion for this Combobox. But, I can't use the name of the Combobox.
Private Sub add_ndc_button_Click()
ndc_page_count = Me.ndc_pages.Pages.Count
Me.ndc_pages.Pages.Add ("NDC " & ndc_page_count + 1)
Set ndc_no_textbox_pages(ndc_page_count + 1) = ndc_pages.Pages(ndc_page_count).Controls.Add("Forms.ComboBox.1")
With ndc_no_textbox_pages(ndc_page_count + 1)
.Top = first_c_y + space_between_rows
.Left = first_c_x + space_between_columns
.Height = text_height
.Width = text_width
.RowSource = "=ProductMasterData!F2:F19"
End With
End Sub
Then I need to create the following function using the name created with button click. But, ndc_no_textbox_pages(1)_Change() gives an error.
Public Sub ndc_no_textbox_pages(1)_Change()
Set SearchRangeNDC = Worksheets("ProductMasterData").Range("F1:F100")
Set FindRowNDC = SearchRangeNDC.Find(ndc_no_textbox.Value, LookIn:=xlValues, lookat:=xlWhole)
If FindRowNDC Is Nothing Then
MsgBox "Please Enter Correct NDC Number or" & vbNewLine & "Select From Drop Down Menu", vbOKOnly, "Required Field"
Else
NDCRow = FindRowNDC.Row
ndc_no_textbox_pages_label(1).Caption = Worksheets("ProductMasterData").Range("F" & NDCRow) & vbNewLine & Worksheets("ProductMasterData").Range("N" & NDCRow)
End If
End Sub
Related
I'm using vba in an MS Access form to insert data from Access into MS Word content controls. The code works for text fields, date fields and checkbox fields. But I'm having trouble inserting data into the drop-down field. My drop-down list properties in MS Word uses a "display name" and value to store the data (e.g. Display Name=Adirondack and Value=1, Display Name=Buffalo and Value=2). Here is the code that works along with the error message that results from attempting to insert into a drop-down list. Your help will be much appreciated!
Private Sub Command179_Click()
' subroutine for exporting referral data from MS Access table to MS Word form content controls
Dim CC As ContentControl
Dim objLE As ContentControlListEntry
Dim fc As Field
Dim ccInfo As String
Dim Female As String
Dim appWord As Word.Application
Dim doc As Word.Document
Dim strDocName As String
Dim blnQuitWord As Boolean
On Error GoTo ErrorHandling
strPath = "C:\Users\AlbanyHiker\Documents\Custom Office Templates\INTAKE FORM\INTAKE BLANK FORM.docm"
strDocName = strPath
Set appWord = GetObject(, "Word.Application")
appWord.Visible = True
Set doc = appWord.Documents.Open(strDocName)
If IsNull(Me!ref_referral_dt) Then
MsgBox "REFERRAL DATE IS MISSING, COMPLETE FORM BEFORE EXPORT"
Me!ref_referral_dt = #1/1/9999#
End If
For Each CC In doc.ContentControls
ccInfo = "<> ID= " & CC.ID & " Title = " & CC.Title & " tag = " & CC.Tag & " Text = " & CC.Range.Text & vbCrLf
Debug.Print ccInfo
Select Case CC.Tag:
Case "frm_referral_dt"
CC.Range.Text = Me!ref_referral_dt
Case "frm_referral_number"
CC.Range.Text = Me!ref_referral_number
Case "frm_part_name_first"
CC.Range.Text = Me!ref_part_name_first
Case "frm_part_name_last"
CC.Range.Text = Me!ref_part_name_last
Case "frm_part_address1"
CC.Range.Text = Me!ref_part_address1
Case "frm_part_address2"
CC.Range.Text = Me!ref_part_address2
Case "frm_mailing_current"
If Me!ref_mailing_current = "-1" Then CC.Checked = True
Case "frm_part_city"
CC.Range.Text = Me!ref_part_city
Case "frm_part_zip"
CC.Range.Text = Me!ref_part_zip
Case "frm_part_telephone"
CC.Range.Text = Me!ref_part_telephone
' Next case statement throws the following error message
' 6124: you are not allowed to edit this selection because it is protected
Case "frm_part_region"
CC.Range.Text = Me!ref_part_region
End Select
Next
MsgBox "INTAKE Report Data Was Successfully Exported, Remember to Save the Word-Fillable File Using a Different Name"
Cleanup:
'do something here to cleanup stuff
Exit Sub
ErrorHandling:
Select Case Err.Number
Case -2147022986, 429
Set appWord = CreateObject("Word.Application")
blnQuitWord = True
Resume Next
Case -2147352571
MsgBox "There is a Type Mismatch Error indicating that a date may have been mistyped" _
& " No data imported. PLEASE CHECK DATA ENTRY ON ALL DATES ", vbOKOnly, _
" Please check the date entries on the form"
Case 5121, 5174
MsgBox "You must select a valid Word Document. " _
& " No data was imported.", vbOKOnly, _
" Document Not Found"
Case 5491
MsgBox "The document you selected does not" _
& " contain the required form fields." _
& " No data exported.", vbOKOnly, _
" Fields Not Found"
Case Else
MsgBox Err & ": " & Err.Description
End Select
GoTo Cleanup
ExitSubError:
Set rs = Nothing
'..and set it to nothing
MsgBox "Export failed, correct problems and export again"
Exit Sub
End Sub
Edit: 17-07-2018 found a solution to retrieve image dimensions in Excel.
I've created a code to retrieve image files in Excel and it's working fine, but once I resize the image it doesn't automatically update its value I need to shift between images before and then go back to the resized imaged to get the value.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim mypic As Picture
If Target.Address = "$A$4" Then
Me.Pictures.Visible = False
With Range("e2")
For Each mypic In Me.Pictures
If mypic.Name = .Text Then
mypic.Visible = True
mypic.Top = .Top
mypic.Left = .Left
Exit For
End If
Next mypic
End With
With ActiveSheet
s = Round(.Shapes(.Range("e2").Value).Height / 72 * 2.54, 2) & "cm"
y = Round(.Shapes(.Range("e2").Value).Width / 72 * 2.54, 2) & "cm"
MsgBox "Picture dimensions are " & vbLf & vbLf & _
"Height: " & s & vbLf & vbLf & _
"Width: " & y
.Range("Q5") = s
.Range("Q6") = y
End With
End If
End Sub
The code as above is there a way to automatically update the values without closing the Excel file or shifting between images.
Thank you in advanced!
I have a textbox set up in a GUI where the user can enter information. This string is then spit out in a textbox within a PPT slide. Depending on the number of lines used in the textbox within the PPT slide, I need to enter the next set of information so many new lines below the text from the textbox. Here is what I have so far:
This is the code that takes the text the user enters in the textbox within the GUI and places it in the textbox within the PPT slide:
Private Sub Location()
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'Make sure there is text in the call to action textbox. If not, display an error message.
If C2AText = "" Then
MsgBox "Woah there! You need to enter text in the location/call to action box."
'Otherwise, if text is inserted, place that text in the WarningData box found on the PPT slide.
Else
.TextRange = C2AText
.TextRange.Paragraphs.Font.Size = 21
.TextRange.Paragraphs.Font.Name = "Calibri"
.TextRange.Paragraphs.Font.Shadow.Visible = True
.TextRange.Paragraphs.Font.Bold = msoTrue
End If
End With
End Sub
This text determines whether or not anything is selected in the HailInfo drop down. If it is, I need to place this text so many lines below the C2AText that was inserted in the previous Sub:
Private Sub HailInfo()
Call Dictionary.HailInfo
ComboBoxList = Array(CStr(HailDropDown))
For Each Ky In ComboBoxList
'On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in HailDropDown, do nothing and exit this sub.
If HailDropDown = "" Then
Exit Sub
'If a hail option is selected, execute the following code.
ElseIf HailDropDown <> "" And C2AText.LineCount = 2 Then
.TextRange = .TextRange & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
ElseIf HailDropDown <> "" And C2AText.LineCount = 3 Then
.TextRange = .TextRange & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
End If
End With
Next
Set dict2 = Nothing
End Sub
Using the C2AText.LineCount within the HailInfo sub does not appear to do anything. It will not insert the hail text anywhere, so I am not sure what I am doing wrong. Any help would be greatly appreciated...thanks!!
You should try the following ...
Private Sub HailInfo()
Call Dictionary.HailInfo
ComboBoxList = Array(CStr(HailDropDown))
For Each Ky In ComboBoxList
'On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in HailDropDown, do nothing and exit this sub.
If HailDropDown = "" Then
Exit Sub
'If a hail option is selected, execute the following code.
Else
.TextRange.Text = .TextRange.Text & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
End If
End With
Next
Set dict2 = Nothing
End Sub
You were only referencing .TextRange, rather than .TextRange.Text.
Also, because you need to add the text at the end, you only need an Else condition, rather than two ElseIfs that both do the same thing! ;0)
More example code ... https://msdn.microsoft.com/en-us/library/office/ff822136.aspx
I have multiple subs within VBA that all have their output within the same text box (WarningData) in a PPT slide. For example, Sub 1 takes a user selection (a selection they made from a drop down menu within a GUI) and inserts that at the top of the text box. Sub 2 inserts another line of text below that line. Sub 3 inserts additional text below that. I need Sub 1 and 2 to have the same font style, but Sub 3 needs to have a different font.
Here is what Sub 1 and Sub 2 look like:
Private Sub 1() 'Sub 2 is very similar.
Call Dictionary.WindInfo
'Sets the font for the warning information text.
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange.Font
.Size = 24
.Name = "Calibri"
.Bold = msoTrue
.Shadow.Visible = True
.Glow.Radius = 10
.Glow.Color = RGB(128, 0, 0)
End With
ComboBoxList = Array(CStr(ComboBox3), CStr(ComboBox4))
For Each Ky In ComboBoxList
On Error Resume Next
'If nothing is selected in ComboBox4, do nothing and exit this sub.
If ComboBox4 = "" Then
Exit Sub
ElseIf ComboBox3 = "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & dict3.Item(Ky)(0)
'Otherwise, if it has a selection, insert selected text.
ElseIf ComboBox3 <> "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & vbCrLf & vbCrLf & dict3.Item(Ky)(0)
End If
Next
Set dict3 = Nothing
End Sub
The following sub is the one that I need to have a different font style:
Private Sub 3()
Call Dictionary.Call2Action
ComboBoxList = Array(CStr(ComboBox7))
For Each Ky In ComboBoxList
On Error Resume Next
'If nothing is selected in ComboBox7 and TextBox9, do nothing and exit this sub.
If ComboBox7 = "" And TextBox9 = "" Then
Exit Sub
'Otherwise, if either has a selection, insert selected text.
ElseIf ComboBox7 <> "" And TextBox9 = "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & vbCrLf & vbCrLf & dict7.Item(Ky)(0)
ElseIf ComboBox7 = "" And TextBox9 <> "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & vbCrLf & vbCrLf & TextBox9
End If
Next
Set dict7 = Nothing
End Sub
Any idea if this is possible?
Thanks!!
I simplified the code using a With statement and added 2 x font lines to show how to set the Font name. Other properties are also available in the Font2 object e.g. .Size, .Bold, .Fill etc.
Private Sub Three()
Call Dictionary.Call2Action
ComboBoxList = Array(CStr(ComboBox7))
For Each Ky In ComboBoxList
On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in ComboBox7 and TextBox9, do nothing and exit this sub.
If ComboBox7 = "" And TextBox9 = "" Then
Exit Sub
'Otherwise, if either has a selection, insert selected text.
ElseIf ComboBox7 <> "" And TextBox9 = "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & dict7.Item(Ky)(0)
.TextRange.Font.Name = "Calibri"
ElseIf ComboBox7 = "" And TextBox9 <> "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & TextBox9
.TextRange.Font.Name = "Calibri"
End If
End With
Next
Set dict7 = Nothing
End Sub
Using the TextRange.Paragraphs method I was able to accomplish this task:
Private Sub 3()
Call Dictionary.Call2Action
ComboBoxList = Array(CStr(ComboBox7))
For Each Ky In ComboBoxList
On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in ComboBox7 and TextBox9, do nothing and exit this sub.
If ComboBox7 = "" And TextBox9 = "" Then
Exit Sub
'Otherwise, if either has a selection, insert selected text.
ElseIf ComboBox7 <> "" And TextBox9 = "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & dict7.Item(Ky)(0)
.TextRange.Paragraphs(3).Font.Size = 18
.TextRange.Paragraphs(3).Font.Name = "Calibri"
.TextRange.Paragraphs(3).Font.Fill.ForeColor.RGB = vbWhite
.TextRange.Paragraphs(3).Font.Bold = msoTrue
.TextRange.Paragraphs(3).Font.Glow.Transparency = 1
ElseIf ComboBox7 = "" And TextBox9 <> "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & TextBox9
.TextRange.Paragraphs(3).Font.Size = 18
.TextRange.Paragraphs(3).Font.Name = "Calibri"
.TextRange.Paragraphs(3).Font.Fill.ForeColor.RGB = vbWhite
.TextRange.Paragraphs(3).Font.Bold = msoTrue
End If
End With
Next
Set dict7 = Nothing
End Sub
Is there any way I can populate list box or other feature in user form by range of cells?
I would like to put each of my selected columns into 1 list box like in:
.
For example A2:U100 without creating new list box for every column?
Right now I do it like:
ListBox1.List = Application.Worksheets("Můj_Ranking").Range("B2:B" & lastRw).Value
ListBox2.List = Application.Worksheets("Můj_Ranking").Range("C2:C" & lastRw).Value
ListBox3.List = Application.Worksheets("Můj_Ranking").Range("D2:D" & lastRw).Value
ListBox4.List = Application.Worksheets("Můj_Ranking").Range("E2:E" & lastRw).Value
ListBox5.List = Application.Worksheets("Můj_Ranking").Range("F2:F" & lastRw).Value
ListBox6.List = Application.Worksheets("Můj_Ranking").Range("G2:G" & lastRw).Value
ListBox7.List = Application.Worksheets("Můj_Ranking").Range("H2:H" & lastRw).Value
ListBox8.List = Application.Worksheets("Můj_Ranking").Range("I2:I" & lastRw).Value
ListBox9.List = Application.Worksheets("Můj_Ranking").Range("J2:J" & lastRw).Value
ListBox10.List = Application.Worksheets("Můj_Ranking").Range("K2:K" & lastRw).Value
ListBox11.List = Application.Worksheets("Můj_Ranking").Range("L2:L" & lastRw).Value
So you indeed want a ListBox with multiple columns, something like this should help :
With ListBox1
.ColumnCount = 11
.ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50"
.ColumnHeads = False
.RowSource = "=Můj_Ranking!B2:L" & LastRw
.MultiSelect = fmMultiSelectMulti
End With
Or how to loop through controls :
For i = 1 To 11
With Application.Worksheets("Můj_Ranking")
Controls("ListBox" & i).List = .Range(ColLet(i) & "2:" & ColLet(i) & lastRw).Value
End With
Next i
And you also have .RowSource property for most of the controls! ;)
And the function to get the letters for columns :
Public Function ColLet(x As Integer) As String
With ActiveSheet.Columns(x)
ColLet = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1)
End With
End Function
haven't tested for Listbox but here is how I fill a Combobox with the result of a recordset
Function Fill_Combobox(ByRef cbo As ComboBox, ByVal rs As ADODB.Recordset, ByVal colWidth As String)
Dim aryColumnWidth() As String
Dim i As Integer
aryColumnWidth = Split(colWidth, ";")
cbo.Clear
cbo.ColumnCount = UBound(aryColumnWidth) + 1
cbo.ColumnHeads = False
cbo.ColumnWidths = colWidth
Do Until rs.EOF
With cbo
.AddItem
For i = 0 To UBound(aryColumnWidth)
.List(.ListCount - 1, i) = rs.Fields(i)
Next
End With
rs.MoveNext
Loop
End Function
it should work similar for a listbox. Call the AddItem Method to add an new entry to the listbox and then fill it by accessing the List element
Assuming I'm reading your question right, this should add a single line with the number of columns you have in a single ListBox.
for i = 2 to lstRw
With ListBox1
.AddItem Application.Worksheets("Můj_Ranking").Range("B" & i).value
.List(.ListCount - 1 ,1) = Application.Worksheets("Můj_Ranking").Range("C" & i).Value
.List(.ListCount - 1 ,2) = Application.Worksheets("Můj_Ranking").Range("D" & i).Value
'And so on for each column
.List(.ListCount - 1 ,10) = Application.Worksheets("Můj_Ranking").Range("L" & i).Value
End With
next i
Remember to edit the column count properties of the ListBox control on your userform of it won't work :)