如何在Excel中根据单元格值切换显示已制作好的图表?
Can I Switch Charts in B1 Based on A1's Value?
Absolutely! This super common dynamic chart request is totally achievable in Excel—here are two practical, easy-to-implement methods to make it work:
Method 1: Dynamic Data Source (No VBA Needed)
Great if you just need to swap out chart data (same chart style, different values):
- First, organize your data into a structured table (e.g., put apple and Orange metrics side by side with clear category labels).
- Set up a helper range (say, D1:E2) to pull dynamic data based on A1:
D1 = XLOOKUP(A1, $A$4:$A$5, $B$4:$B$5, "") ' Pulls apple/Orange's first data point E1 = XLOOKUP(A1, $A$4:$A$5, $C$4:$C$5, "") ' Pulls the second data point - Create your chart using this helper range as the data source. Now whenever you type "apple" or "Orange" into A1, the helper range updates automatically, and your chart will switch to match.
Method 2: VBA Macro (For Different Chart Styles)
Perfect if you need entirely different chart designs (e.g., a bar chart for apple, a line chart for Orange):
- First, create all your required charts, place them in the same area near B1, and rename each one (right-click the chart → use the name box in the top-left corner to name them
ChartAppleandChartOrange). - Right-click your worksheet tab → select "View Code", then paste this code:
Private Sub Worksheet_Change(ByVal Target As Range) ' Only react when cell A1 is modified If Target.Address = "$A$1" Then ' Hide all charts first Me.ChartObjects("ChartApple").Visible = False Me.ChartObjects("ChartOrange").Visible = False ' Show the matching chart based on A1's value Select Case UCase(Target.Value) Case "APPLE" Me.ChartObjects("ChartApple").Visible = True Case "ORANGE" Me.ChartObjects("ChartOrange").Visible = True End Select End If End Sub - Save your file as an Excel Macro-Enabled Workbook (.xlsm). Now whenever you update A1, the correct chart will pop up in B1's area automatically.
Pick the method that fits your needs—both work smoothly for the scenario you described!
内容的提问来源于stack exchange,提问作者Aum Isha
相关产品推荐
相关产品推荐

