|
楼主 |
发表于 2024-12-20 16:01
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
抛砖引玉,还希望各位老师帮忙优化一下,谢谢
- Sub 宏2()
- '
- ' 宏2 宏
- '
- '
- ActiveSheet.ChartObjects("图表 1").Activate
- ActiveChart.FullSeriesCollection(4).Select
- ActiveChart.FullSeriesCollection(4).Points(7).Select
- With Selection.Format.Fill
- .Visible = msoTrue
- .ForeColor.ObjectThemeColor = msoThemeColorAccent6
- .ForeColor.TintAndShade = 0
- .ForeColor.Brightness = 0.400000006
- .Transparency = 0
- .Solid
- End With
- ActiveChart.FullSeriesCollection(4).DataLabels.Select
- ActiveChart.FullSeriesCollection(4).Points(1).DataLabel.Select
- Selection.Left = 430.412
- Selection.Top = 382.419
- ActiveChart.FullSeriesCollection(4).Select
- ActiveChart.FullSeriesCollection(4).Points(1).Select
- With Selection.Format.Fill
- .Visible = msoTrue
- .ForeColor.RGB = RGB(0, 32, 96)
- .Transparency = 0
- .Solid
- End With
- End Sub
复制代码- Sub 改变颜色(x, ys)
- '
- ' 宏2 宏
- '
- '
- ActiveSheet.ChartObjects("图表 1").Activate
- If ys = 1 Then
-
- ActiveChart.FullSeriesCollection(4).Points(x).Select
- With Selection.Format.Fill
- .Visible = msoTrue
- .ForeColor.RGB = RGB(192, 0, 0)
- .Transparency = 0
- .Solid
- End With
- Else
- ActiveChart.FullSeriesCollection(4).Points(x).Select
- With Selection.Format.Fill
- .Visible = msoTrue
- .ForeColor.RGB = RGB(0, 255, 0)
- .Transparency = 0
- .Solid
- End With
-
- End If
-
- End Sub
- Sub 改变颜色2(x, ys)
- '
- ' 宏2 宏
- '
- '
- ActiveSheet.ChartObjects("图表 2").Activate
- If ys = 1 Then
-
- ActiveChart.FullSeriesCollection(4).Points(x).Select
- With Selection.Format.Fill
- .Visible = msoTrue
- .ForeColor.RGB = RGB(192, 0, 0)
- .Transparency = 0
- .Solid
- End With
- Else
- ActiveChart.FullSeriesCollection(4).Points(x).Select
- With Selection.Format.Fill
- .Visible = msoTrue
- .ForeColor.RGB = RGB(0, 255, 0)
- .Transparency = 0
- .Solid
- End With
-
- End If
-
- End Sub
复制代码
- Private Sub Worksheet_Change(ByVal Target As Range)
- Dim KeyCells As Range
- Dim ws As Worksheet
- Dim cell As Range
- ' 设置工作表对象
- Set ws = Me ' 或者使用 ThisWorkbook.Sheets("Sheet1") 指定特定的工作表
- ' 只处理G列的变化
- On Error GoTo ExitHandler
- Application.EnableEvents = False ' 防止无限递归触发事件
- If Not Intersect(Target, ws.Columns("G")) Is Nothing Then
- For Each cell In Target
- ' 检查修改的单元格是否在G列
- If cell.Column = 7 Then ' G列是第7列
- ' 检查同一行的D、E、F列是否非空
- If Len(Trim(cell.Offset(0, -3).Value)) > 0 And _
- Len(Trim(cell.Offset(0, -2).Value)) > 0 And _
- Len(Trim(cell.Offset(0, -1).Value)) > 0 Then
- ' 弹出提示窗口
- 'MsgBox "注意:您编辑了G列的单元格,且同行的D、E、F列非空。", vbInformation, "提醒"
- h = Target.row - 1
- ys = Target.Value
- Call 改变颜色2(h, ys)
- Target.Select
- End If
- End If
- Next cell
- End If
- ExitHandler:
- Application.EnableEvents = True ' 重新启用事件
- Exit Sub
- ErrorHandler:
- MsgBox "发生错误: " & Err.Description, vbCritical
- Resume ExitHandler
- End Sub
复制代码 自己能力不行,图表代码不是很熟悉,现在还想更自动化一下,提前感谢各位老师帮助
测试代码.rar
(73.83 KB, 下载次数: 3)
|
|