【问题标题】:Changing Shape Quantity Based On Cell Value根据单元格值更改形状数量
【发布时间】:2019-03-05 00:16:01
【问题描述】:

我有一个宏,它根据用户在“输入信息”表上输入的尺寸生成缩放形状。宏链接到一个按钮,一旦单击该按钮,形状就会出现在用户选择的工作表 (ws - ws5) 上,并且形状尺寸、数量和描述将添加到“容器 BOM”工作表中。用户也可以在“输入信息”表上的单元格中输入形状的数量,但到目前为止,我无法链接形状数量以产生多个形状。

现在我有另一个按钮(基本上是相同的宏,但没有将形状详细信息添加到“容器 BOM”表),如果选择的数量超过一个,用户可以使用它来创建其他形状。我正试图消除那些额外的工作。

Sub AddShapeToCell()

Dim s As Shape
Dim r As Long
Dim ws As Worksheet
Set ws = Sheets("Deep Blue")
Set ws1 = Sheets("GC II")
Set ws2 = Sheets("300ft Barge")
Set ws3 = Sheets("275ft Barge")
Set ws4 = Sheets("250ft Barge")
Set ws5 = Sheets("User Defined Vessel")
Dim TriggerCellb As Range
Set TriggerCellb = Range("D8")
Const scaling As Double = 2.142857


'Create a shape

If TriggerCellb.Value = "Deep Blue" Then
Set s = ws.Shapes.AddShape(Cells(4, 17), 53, 66, Cells(20, 4) * scaling * 
Cells(5, 17), Cells(22, 4) * scaling * Cells(5, 17))

ElseIf TriggerCellb.Value = "GC II" Then
Set s = ws1.Shapes.AddShape(Cells(4, 17), 53, 66, Cells(20, 4) * scaling * 
Cells(5, 17), Cells(22, 4) * scaling * Cells(5, 17))

ElseIf TriggerCellb.Value = "300ft Barge" Then
Set s = ws2.Shapes.AddShape(Cells(4, 17), 53, 66, Cells(20, 4) * scaling * 
Cells(5, 17), Cells(22, 4) * scaling * Cells(5, 17))

ElseIf TriggerCellb.Value = "275ft Barge" Then
Set s = ws3.Shapes.AddShape(Cells(4, 17), 53, 66, Cells(20, 4) * scaling * 
Cells(5, 17), Cells(22, 4) * scaling * Cells(5, 17))

ElseIf TriggerCellb.Value = "250ft Barge" Then
Set s = ws4.Shapes.AddShape(Cells(4, 17), 53, 66, Cells(20, 4) * scaling * 
Cells(5, 17), Cells(22, 4) * scaling * Cells(5, 17))

ElseIf TriggerCellb.Value = "User Defined Vessel" Then
Set s = ws5.Shapes.AddShape(Cells(4, 17), 53, 66, Cells(20, 4) * scaling * 
Cells(5, 17), Cells(22, 4) * scaling * Cells(5, 17))

End If
'make it nearly white
s.Fill.ForeColor.RGB = RGB(245, 245, 255)

'show text within it
s.TextFrame.Characters.Text = Range("d12").Value
s.TextFrame.Characters.Font.ColorIndex = 2

With s.TextFrame.Characters(0, 0)
s.TextFrame.HorizontalAlignment = xlHAlignCenter
s.TextFrame.VerticalAlignment = xlVAlignCenter
.Font.Color = RGB(0, 0, 0)

End With

'add to BOM
Dim lastCell As Range
Set lastCell = Sheets("Vessel BOM").Range("C" & 
Rows.Count).End(xlUp).Offset(1, 0)

Sheets("Enter Information").Range("g20:m20").Copy
lastCell.PasteSpecial (xlPasteValues)

Sheets("Enter Information").Range("g20:m20").Copy
lastCell.PasteSpecial (xlPasteFormats)
Application.CutCopyMode = False

End Sub

【问题讨论】:

    标签: excel vba shapes


    【解决方案1】:

    不确定您要问什么,但您可以创建多个形状,例如

    Sub x()
    
    Dim s As Shape, i As Long
    
    For i = 1 To range("A1").value
        Set s = ActiveSheet.Shapes.AddShape(msoShapeBevel, 10, 20 * i, 10, 10)
    Next i
    
    End Sub
    

    【讨论】:

    • 谢谢,SJR。我希望用户在单元格中输入一个数字。如果该数字为 2,则单击按钮时会出现两个形状。如果该数字为 20,则单击按钮时会出现 20 个形状。
    • 在这种情况下,只需将循环基于单元格的值而不是硬编码 5。for i=1 to range("a1").value.
    • 做到了!谢谢!现在,我需要找到一种方法来在表格上创建形状后对其进行偏移。
    • 很高兴有帮助。您必须使用 top/left/height/width 参数。在示例中,形状出现在垂直线上,因为我只改变了顶部值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多