请教EXCEL,VBA宏相关问题,请高手指教!
请问下EXCEL里,视图->工具栏->绘图里画的圆或线条,如何通过VBA宏实现(通过某单元格的数值的大小判断,来隐藏该圆或线条,比如A1>0,该圆自动显示;A1<=0,该...
请问下EXCEL里,视图->工具栏->绘图里画的圆或线条,如何通过VBA宏实现(通过某单元格的数值的大小判断,来隐藏该圆或线条,比如A1>0,该圆自动显示;A1<=0,该圆自动隐藏;A2>0,另一个线条自动显示;A2<=0,该线条自动隐藏;A3>0,另一个矩形自动显示,A3<=0,该矩形自动隐藏)。
我只会隐藏一个,代码如下:
Public Sub 隐藏()
ActiveSheet.Shapes(ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Name).Select
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 65
Selection.ShapeRange.Fill.Transparency = 1#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoFalse
End Sub
Public Sub 显示()
ActiveSheet.Shapes(ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Name).Select
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 65
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
End Sub
Public Sub 是否显示()
If Range("a1").Value > 0 Then
显示
Else
隐藏
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("a1").Value > 0 Then
显示
Else
隐藏
End If
End Sub
请高手指教,有多个图形分别由不同的单元格控制而不干涉。
请给出详细代码,因为我是新手,谢谢! 展开
我只会隐藏一个,代码如下:
Public Sub 隐藏()
ActiveSheet.Shapes(ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Name).Select
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 65
Selection.ShapeRange.Fill.Transparency = 1#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoFalse
End Sub
Public Sub 显示()
ActiveSheet.Shapes(ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Name).Select
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 65
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
End Sub
Public Sub 是否显示()
If Range("a1").Value > 0 Then
显示
Else
隐藏
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("a1").Value > 0 Then
显示
Else
隐藏
End If
End Sub
请高手指教,有多个图形分别由不同的单元格控制而不干涉。
请给出详细代码,因为我是新手,谢谢! 展开
2个回答
展开全部
点击要放入图形的单元格标签,点右键选“查看代码”,把下面代码复制进去。
第一次执行时,要先执行“添加形状”宏,只执行一次即可,就自动生成圆、矩形与直线,以后就不用再执行了。
只要改变A1\A2\A3格中数值,就可使三个形状显示或隐藏了。
Sub 添加形状()
With ActiveSheet.Shapes.AddShape(msoShapeOval, 180, 0, 72, 72) '添加一个圆形
.Name = "yuan" '命名
.Fill.ForeColor.RGB = RGB(0, 255, 0) '设置前景色
End With
With ActiveSheet.Shapes.AddShape(msoShapeRectangle, 180, 80, 72, 72) '添加一个矩形
.Name = "ju" '命名
.Fill.ForeColor.RGB = RGB(255, 0, 0) '设置前景色
End With
With ActiveSheet.Shapes.AddLine(180, 160, 280, 160) '添加一个直线
.Name = "xian" '命名
.Fill.ForeColor.RGB = RGB(255, 0, 0) '设置前景色
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If [a1] > 0 Then
ActiveSheet.Shapes("yuan").Visible = msoTrue
Else
ActiveSheet.Shapes("yuan").Visible = msoFalse
End If
If [a2] > 0 Then
ActiveSheet.Shapes("ju").Visible = msoTrue
Else
ActiveSheet.Shapes("ju").Visible = msoFalse
End If
If [a3] > 0 Then
ActiveSheet.Shapes("xian").Visible = msoTrue
Else
ActiveSheet.Shapes("xian").Visible = msoFalse
End If
End Sub
第一次执行时,要先执行“添加形状”宏,只执行一次即可,就自动生成圆、矩形与直线,以后就不用再执行了。
只要改变A1\A2\A3格中数值,就可使三个形状显示或隐藏了。
Sub 添加形状()
With ActiveSheet.Shapes.AddShape(msoShapeOval, 180, 0, 72, 72) '添加一个圆形
.Name = "yuan" '命名
.Fill.ForeColor.RGB = RGB(0, 255, 0) '设置前景色
End With
With ActiveSheet.Shapes.AddShape(msoShapeRectangle, 180, 80, 72, 72) '添加一个矩形
.Name = "ju" '命名
.Fill.ForeColor.RGB = RGB(255, 0, 0) '设置前景色
End With
With ActiveSheet.Shapes.AddLine(180, 160, 280, 160) '添加一个直线
.Name = "xian" '命名
.Fill.ForeColor.RGB = RGB(255, 0, 0) '设置前景色
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If [a1] > 0 Then
ActiveSheet.Shapes("yuan").Visible = msoTrue
Else
ActiveSheet.Shapes("yuan").Visible = msoFalse
End If
If [a2] > 0 Then
ActiveSheet.Shapes("ju").Visible = msoTrue
Else
ActiveSheet.Shapes("ju").Visible = msoFalse
End If
If [a3] > 0 Then
ActiveSheet.Shapes("xian").Visible = msoTrue
Else
ActiveSheet.Shapes("xian").Visible = msoFalse
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询