msoShapeOval entered in cell not allowed to be value for variable declared as MsoShapeType - excel

In the attached code everything works except assigning the value to shpOval3.
I am trying to take the text from a cell and assign it to a variable declared as MsoShapeType.
1) The code misfires with Type mismatch when it attempts to assign the cell value of msoShapeOval to shpOval3 (declared as MsoShapeType).
2) If I try strOval4 (string) instead in .Shapes.AddShape(strOval4, Left, Top, Width, Height) it also misfires and says Type mismatch.
I am trying to avoid converting to an msoShapeType constant in the cell as the cell value is loaded into a combobox on a form and the value of 9 is meaningless to the users. I can convert if necessary but am looking for a solution without converting.
Sub ShpType()
Dim shpOval1 As Long
Dim shpOval2 As MsoShapeType
Dim shpOval3 As MsoShapeType
Dim strOval4 As String
Sheets("Data").Range("Company1Shape") = "msoShapeOval"
shpOval1 = msoShapeOval
shpOval2 = msoShapeOval
shpOval3 = Sheets("Data").Range("Company1Shape")
strOval4 = Sheets("Data").Range("Company1Shape")
Debug.Print "shpOval1 = "; shpOval1
Debug.Print "shpOval2 = "; shpOval2
Debug.Print Sheets("Data").Range("Company1Shape")
Debug.Print "shpOval3 = "; shpOval3
Debug.Print "strOval4 = "; strOval4
End Sub
Debugger results with shpOval3 commented out
shpOval1 = 9
shpOval2 = 9
msoShapeOval
shpOval3 = 0
strOval4 = msoShapeOval

You can use a Select Case statement to test your string and assign the appropriate constant to a variable declared as msoAutoShapeType...
Dim strShapeType As String
strShapeType = Sheets("Data").Range("Company1Shape")
Dim shapeType As MsoAutoShapeType
Select Case strShapeType
Case "msoShapeOval"
shapeType = msoShapeOval
Case "msoShapeRectangle"
shapeType = msoShapeRectangle
'etc
'
'
'
End Select
Hope this helps!

Related

VBA type Mismatch error,how can I be sure I'm defining the right value type to variables?

I'm trying to compare an String value and add a String result from that String Value, it throws an error type 13, I believe the error relies in the "Range" property,it works If I don't define an range and leave a single Cell value, but I need to check the same logic for a certain range.
Sub Vars()
'myVar = 20'
'MsgBox myVar'
Dim currencyVar As Double
Dim locationRange As String
Dim RangeResult As String
locationRange = Application.Sheets("Sheet4").Range("F3:F19")
RangeResult = Sheets("Sheet4").Range("G3:G19")
Debug.Print (locationRange)
mainXlookup = WorksheetFunction.XLookup(Sheets("Sheet4").Range("b3:b19"), Sheets("Sheet3").Range("b2:b18"), Sheets("Sheet3").Range("c2:c18"), "Not in project")
Sheets("Sheet4").Range("f3:f19") = mainXlookup
If locationRange = "Bethesda" Then
RangeResult = "In-Range"
End If
End Sub

Find textbox in a group of shapes (VBA 6.3)

I am using VBA 6.3. In Excel I have a chart - left y axis on it and numbers. When I copy the Chart to powerpoint and degroup it, I leave the y axis only with the TextBoxes and the axis alone. Now I would like to obtain the minimum and maximum numbers beside the axis. First I tried to detect the TextBoxes it should be msoTextBox value 17. But when I checked the type number is 378*... I could not find out what is it (using google search). I need to obtain the two numbers and length of the axis line (so I can calculate the ratio y_length/(max-min) ).
This is the code sofar.
Sub GetMinMax()
Dim YAxisMinMax() As Integer
With ActiveWindow.Selection
If (.Type = ppSelectionShapes) And (.ShapeRange.Type = msoTextBox) Then
Else
MsgBox "Failed"
Exit Sub
End If
End With
End Sub
Rhe result - The Message is Failed. It did not found the TextGroup with number.
*Edit: I have found, that the type is different for every "text box". However the name of the shape is "rectangle", not a text box". It looks like text box, because it has text inside it.
So far, this what I have done:
Option Explicit
Private Type T_HORIZONTAL_LINE
ShortLineLeft As Integer
ShortLineTop As Integer
Length As Integer
isFound As Boolean
End Type
Private Type T_VERTICAL_LINE
ShortLineLeft As Integer
ShortLineTop As Integer
Length As Integer
isFound As Boolean
End Type
Sub LookForAxis()
Dim Horizontal As T_HORIZONTAL_LINE
Dim Axis As T_VERTICAL_LINE
Dim YAxisMinMax(2) As Integer
Dim OldMinMax(2) As Integer
Dim Value As Integer
Dim Ratio As Single
Dim Text As String
Dim Sh As Shape
With ActiveWindow.Selection
If (.Type = ppSelectionShapes) And (.ShapeRange.Type = msoGroup) Then
For Each Sh In .ShapeRange.GroupItems
If Sh.Type = msoLine Then
If (Axis.isFound) And (Horizontal.isFound) Then
ElseIf (Sh.Width < Sh.Height) And (Not Axis.isFound) Then
Axis.Length = Sh.Height
Axis.ShortLineLeft = Sh.Left
Axis.ShortLineTop = Sh.Top
Axis.isFound = True
ElseIf (Sh.Width > Sh.Height) And (Not Horizontal.isFound) Then
Horizontal.Length = Sh.Width
Horizontal.ShortLineLeft = Sh.Left
Horizontal.ShortLineTop = Sh.Top
Horizontal.isFound = True
End If
ElseIf (Sh.Type = msoAutoShape) And (Sh.HasTextFrame = msoTrue) Then
Text = Sh.TextFrame.TextRange.Text
Value = CInt(Text) ' Possibly: CLng()
If Value < OldMinMax(1) Then
OldMinMax(1) = Value
ElseIf Value > OldMinMax(2) Then
OldMinMax(2) = Value
End If
End If
Next Sh
Ratio = Axis.Length / (OldMinMax(2) - OldMinMax(1)) ' Axis length div. axis range
Else
MsgBox "You have not selected an OBJECT in PowerPoint to dimension."
Exit Sub
End If
End With
End Sub
Notice: I have first placed all the axis lines and rectangles with TextFrame to one group to make it easy.

VBA COUNTA Userform

I have a Userform with several textboxes and a command button. When the information is entered and submitted the information is transfered to the first empty row.
I need a code that would counta() text within 4 columns within that row. So translate =IF(IsBlank($A2),"",COUNTA(E2:H2) to VBA code to calculate after the user submitted the information.
Option Explicit
Sub test()
Debug.Print "Var 1 : "; CountRangeIf("not(A3="""")", Range("E3:H3"))
Dim testCriteria As Boolean
testCriteria = Not (Range("A3").Value = "")
Debug.Print "Var 2 : "; CountRangeIf_Var2(testCriteria, Range("E3:H3"))
End Sub
Public Function CountRangeIf(IfCriteriaString As String, CountRange As Range) As Variant
Dim resultCriteria As Boolean
CountRangeIf = "" ' Result = "" if Criteria is false
resultCriteria = Evaluate(IfCriteriaString)
With Application.WorksheetFunction
If resultCriteria Then
CountRangeIf = .CountA(CountRange)
End If
End With
End Function
Public Function CountRangeIf_Var2(IfCriteria As Boolean, CountRange As Range) As Variant
CountRangeIf_Var2 = "" ' Result = "" if Criteria is false
With Application.WorksheetFunction
If IfCriteria Then
CountRangeIf_Var2 = .CountA(CountRange)
End If
End With
End Function
Presuming we're using Sheet1
and presuming your Row # is already stored in
ThisRowNum variable
Following should be close to what you asked for
If Trim(CStr(Sheets("Sheet1").Range("A" & ThisRowNum).Value)) = "" then
xCtr = 0 ' Your formula used a null string - you can fix this
else
xCtr = WorksheetFunction.CountA(Sheets("Sheet1").Range("E" & ThisRowNum &":H" & ThisRowNum))
endif
The xCtr variable is the result

getting compiler error (invalid identifier)

As said above, in my following code I get an compiler error telling me there is an invalid identifier. I don't really see the problem, basically it is a very easy code. The problem should be in the lines using the backcolor-Function.
Sub addmaterial()
Dim AMU As UserForm
Set AMU = AddMaterialUserform1
Dim SCU As ComboBox
Set SCU = AMU.SelectComboBoxUserform
Dim APCU As ComboBox
Set APCU = AddMaterialUserform1.AddedPropertiesComboBoxUserform
Dim TextBoxObject As Combobox
Dim i As Integer
SCU.AddItem "Material"
SCU.AddItem "Material Group"
APCU.BorderColor.ColorIndex = 15
For i = 1 To 12
TextBoxObject = "Textbox" & i
AMU.TextBoxObject.BackColor.ColorIndex = 15
Next
AMU.Show
End Sub
You try to assign a String to an Object
TextBoxObject = "Textbox" & i
You can use the AMU.Controls- Collection
Set TextBoxObject = AMU.Controls("Textbox" & i)
If you don't have the reference just the Name.
Or if there is no Collection on other Objects have a look at
CallByName(Object As Object, ProcName As String, _
CallType As VbCallType, Args() As Variant)`
`.
Dim TextBoxObject As String
AMU.TextBoxObject.BackColor.ColorIndex = 15
At a guess it doesn't like you declaring a string variable as the same name as a text box

Application defined or object defined error (named ranges)

Im stumped at this point with my code here, im getting this generic error at the line where its supposed to copy and paste the selected range after creating the worksheet but its giving me this frustrating error - i dont know what im doing wrong here, please help guys, all help appreciated :)
Private Sub ExpBttn()
Dim WrkShtExists As Boolean
Dim Sht As Worksheet
Dim x As Integer
Dim TgtRngR As Range
Dim TgtRngB As Range
Dim TgtNme As String
Select Case Multi
Case MultiPage1.Value = 0
Set TgtRngR = Sheets("Tracker").Range("U2:Z19")
Set TgtRngB = Sheets("Tracker").Range("U21:Z26")
TgtNme = "P4PSoft"
Case MultiPage1.Value = 1
Set TgtRngR = Sheets("Tracker").Range("AB2:AG19")
Set TgtRngB = Sheets("Tracker").Range("AB21:AG26")
TgtNme = "P4PHard"
Case MultiPage1.Value = 2
Set TgtRngR = Sheets("Tracker").Range("AP2:AU19")
Set TgtRngB = Sheets("Tracker").Range("AP21:AU26")
TgtNme = "RCI"
Case MultiPage1.Value = 3
Set TgtRngR = Sheets("Tracker").Range("AI2:AN19")
Set TgtRngB = Sheets("Tracker").Range("AI21:AN26")
TgtNme = "RCDI"
End Select
Sheets.Add.Name = "Exported_" & TgtNme
Sheets("Tracker").Range("TgtRngR").Copy Destination:=Sheets("Exported_" & TgtNme).Range("A1:F18")
Sheets("Tracker").Range("TgtRngB").Copy Destination:=Sheets("Exported_" & TgtNme).Range("A20:F25")
Sheets("Exported_" & TgtNme).Range("A1:F18") = Sheets("Tracker").Range("TgtRngR").Value
Sheets("Exported_" & TgtNme).Range("A20:F25") = Sheets("Tracker").Range("TgtRngB").Value
Sheets("Exported_" & TgtNme).Select
ActiveSheet.Columns("A:F").AutoFit
You've set your range variable in the Case statement, so use it in the copy/paste
TgtRngR.Copy Destination:=Sheets("Exported_" & TgtNme).Range("A1:F18")
and
Sheets("Exported_" & TgtNme).Range("A1:F18") = TgtRngR
Also, your Case statment logic is incorrect. It should be something like
TgtNme = ""
Select Case MultiPage1.Value
Case 0
...
Case 1
...
'etc
End Select
If TgtNme <>"" then
' do the cop pastes
End If
Please review your Case statement:
in the Case opener you use a (local) variable Multi
in the contition statements you use a different object MultiPage1.Value
both of them are not declared
For me it looks as none of the Case branches are executed and hence TgtRngR, TgtRngB, TgtNme are undefined.
Setting a breakpoint at Private Sub ExpBttn() and stepping through the Sub with F8 would help.

Resources