VBA Auto Update Image Dimensions in Excel - vba

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!

Related

Finding cells with certain interior color

I'm using this code to locate cells that are a certain color (changed by conditional formatting), then based on that values from the row are swapped to another sheet. However, the macro runs and just doesn't find anything. No error message, it's just not locating any cells (that I've switched to RGB 255, 0, 0 for this test). What am I doing wrong here?
Sub ChangeAccountDetailsForMay()
Dim ws As Worksheet
Set ws = Sheets("comparison")
Dim destws As Worksheet
Set destws = Sheets("Account Detail")
Dim i As Integer
Dim j As Integer
For i = 24 To 3205
If ActiveWorkbook.Sheets("comparison").Range("BF" & i).Interior.Color = RGB(255, 0, 0) Then 'Might be RGB 218 148 148
MsgBox "Found one at row " & i & "!"
For j = 25 To 3077
If ActiveWorkbook.Sheets(destws).Range("J" & j).Value = ActiveWorkbook.Sheets(ws).Range("J" & i).Value And ActiveWorkbook.Sheets(destws).Range("L" & j).Value = ActiveWorkbook.Sheets(ws).Range("L" & i).Value Then
ActiveWorkbook.Sheets(destws).Range("BD" & j).Value = ActiveWorkbook.Sheets(ws).Range("BB" & i).Value
ActiveWorkbook.Sheets(destws).Range("BE" & j).Value = ActiveWorkbook.Sheets(ws).Range("BC" & i).Value
ActiveWorkbook.Sheets(destws).Range("BF" & j).Value = ActiveWorkbook.Sheets(ws).Range("BD" & i).Value
End If
Next j
End If
Next i
MsgBox "Done!", vbInformation, "Success!"
End Sub
To get the color of the cell that is provided from Conditional Formatting one must use DisplayFormat
...Range ("BF" & i).DisplayFormat.Interior.Color...

Determining the Number of Lines in a Textbox in VBA

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

Combobox_Change Function in MultiPages in VBA

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

Edit specific line in a textbox

I wrote this macro to generate a textbox with more than one line:
Sub multipleLineTextBox()
Dim Box As Shape
Set Box = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=50, Top:=50, Width:=200, Height:=200)
Box.Line.Style = msoLineThinThin
Box.Line.Weight = 6
Box.TextFrame.TextRange.Text = "first line" & vbCrLf & "second line"
Box.TextFrame.TextRange.Font.Size = 20
End Sub
The last line edits all the text in the textbox to be size 20.
How can I edit each line separately?
TextRange has a Paragraphs collection. You can loop that or work with each item individually. For example
Dim bxRange As Word.Range
Set bxRange = Bix.TextFrame.TextRange
bxRange.Paragraphs(1).Range.Font.Size = 12
bxRange.Paragraphs(2).Range.Font.Size = 10
Use this:
Sub multipleLineTextBox()
Dim Box As Shape
Set Box = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=50, Top:=50, Width:=200, Height:=200)
With Box
.Line.Style = msoLineThinThin
.Line.Weight = 6
.TextFrame.TextRange.Text = "first line" & vbCrLf & "second line"
.TextFrame.TextRange.Paragraphs(2).Range.Font.Size = 20
End with
End Sub

Writing values to Textboxes on Chart Sheets

Looking for some guidance. I have created a statistics chart but also it lists the Top 5 WIP for certain categories.
the trouble I'm having is writing to text boxes on a chart. I have managed to do it with standard text boxes found on the insert tab but not with the ActiveX boxes which is what I need due to the need of scroll bars
Here is the function I have written to write strings to all text boxes.
Function Top5HeldCell(aSheet)
Dim row As Integer
Dim commentBox As TextBox
row = 12
For i = 4 To GetNumberOfEntries(aSheet, "A")
If Sheets(aSheet).Range("S" & i).Value <> "" Then
If Sheets(aSheet).Range("T" & i).Value = "" Then
rmNumber = Sheets(aSheet).Range("A" & i).Value
partNumber = Sheets(aSheet).Range("B" & i).Value
serialNumber = Sheets(aSheet).Range("C" & i).Value
ncNumber = Sheets(aSheet).Range("D" & i).Value
currentWIP = Sheets(aSheet).Range("W" & i).Value
Comments = Sheets(aSheet).Range("I" & i).Value
If row <= 16 Then
ActiveChart.Shapes.Range(Array("rmNumber" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = rmNumber
ActiveChart.Shapes.Range(Array("partNumber" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = partNumber
ActiveChart.Shapes.Range(Array("serialNumber" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = serialNumber
ActiveChart.Shapes.Range(Array("ncNumber" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = ncNumber
ActiveChart.Shapes.Range(Array("WIPTime" & row)).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = currentWIP
' This works using standard textboxes
'ActiveChart.Shapes.Range(Array("comment" & row)).Select
'Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = Comments
' here I'm trying to use ActiveX boxes
Charts("Statistics Chart").OLEObjects("comment" & row).Object.Text = ""
row = row + 1
End If
End If
End If
Next i
End Function
I seem to be getting the error 'Unable to get OLEObjects property of the Chart Class.
From using my nogging I expect this is something to do with the Charts not supporting this feature. Is there a work around that is known or am I really stuck???
Cheers