跳到主要內容

在Excel中滾動時如何始終保持圖表在視圖中?

如果在工作表中插入了圖表,則在向下滾動工作表以查看數據時,無法與下面顯示的屏幕截圖同時查看該圖表,該屏幕截圖必須是討厭的。 在本文中,我介紹了一個VBA代碼,即使向下或向上滾動工作表也可以始終查看圖表。

doc保持圖表在視圖1
doc向下箭頭
doc保持圖表在視圖2

始終查看圖表


箭頭藍色右氣泡 始終查看圖表

要在滾動工作表時保留圖表在視圖中,可以在VBA代碼下面應用以解決該問題。

1.右鍵單擊要保持圖表可見的工作表選項卡,然後單擊 查看代碼 形成上下文菜單。 看截圖:
doc保持圖表在視圖3

2.在彈出 Microsoft Visual Basic for Applications 窗口,將下面的代碼粘貼到空白腳本。

VBA:始終查看圖表

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'UpdatebyExtendoffice20161111
    Dim CPos As Double
    Application.ScreenUpdating = False
    CPos = ActiveWindow.ScrollRow * ActiveCell.RowHeight
    ActiveSheet.ChartObjects("Chart 2").Activate
    ActiveSheet.Shapes("Chart 2").Top = CPos
    ActiveWindow.Visible = False
    Application.ScreenUpdating = True
End Sub

doc保持圖表在視圖5

3.保存並關閉對話框,然後在單擊任何單元格時圖表將上下移動。
doc保持圖表在視圖6

筆記:

(1)在VBA代碼中,“圖表2”是您要保留的圖表名稱,您可以根據需要進行更改。

(2)此VBA不能始終查看一組圖表。

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (15)
Rated 4.75 out of 5 · 2 ratings
This comment was minimized by the moderator on the site
A mí me pasa que la siguiente vez que abro el archivo, el script ya no funciona. Tengo que copiarlo, borrarlo, cerrar el archivo tras guardarlo, volver a abrir el archivo y volver a pegar el script en VBA. ¿Alguna solución?
Rated 4.5 out of 5
This comment was minimized by the moderator on the site
Hi, save the workbook as excel macro enable workbook, the vba will be work when open next time.https://cdn.extendoffice.com/images/stories/comments/sun-comment/doc-macro-enable.png
This comment was minimized by the moderator on the site
I encountered a problem with unselecting the cell and needed two charts, So I made some changes and it seems to work now.
To use it put the right Chart Name.
*Note: This one is for two charts


Private Sub worksheet_selectionchange(ByVal target As Range)
Dim chartposition As Double

chartposition = ActiveWindow.ScrollRow * ActiveCell.RowHeight
ActiveSheet.Shapes("Chart 2").Top = chartposition

ActiveSheet.ChartObjects("Chart 3").Top = chartposition + 250

End Sub
This comment was minimized by the moderator on the site
Can any one assist. when i follow theses steps


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'UpdatebyExtendoffice20161111
Dim CPos As Double
Application.ScreenUpdating = False
CPos = ActiveWindow.ScrollRow * ActiveCell.RowHeight
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveSheet.Shapes("Chart 2").Top = CPos
ActiveWindow.Visible = False
Application.ScreenUpdating = True
End Sub


i get the following err

Run-Time error '-2147024809 (80070057)':
The item with the specified name was not found

when i debug
ActiveSheet.ChartObjects("Chart 2").Activate
This comment was minimized by the moderator on the site
Hi, Christo, you need to change the chart name "Chart 2" to your chart really name in the script. You can click at the chart and see its name in the name box. See screenshot:
https://www.extendoffice.com/images/stories/comments/sun-comment/do_chart_name.png
This comment was minimized by the moderator on the site
yes thanks. after a made the question i realised my chart name was wrong
thanks

ps.
do you perhaps know how i can make the chart kept in view as i scroll without clicking where it must follow
This comment was minimized by the moderator on the site
can I use Kutools to automatically use that VBA for my charts??
This comment was minimized by the moderator on the site
Sorry, Wangnuli, Kutools has not support this so far.
This comment was minimized by the moderator on the site
Hola muchas gracias por el código, utilizando este código ¿Hay alguna manera de limitar qué tan alto en la hoja se reubicará el gráfico? por ejemplo, no quiero que se coloque encima de la fila 9. Ayuda por favor.
Rated 5 out of 5
This comment was minimized by the moderator on the site
Is there a way to limit how high up on the sheet the chart will relocate to? I don't want it to affix above row 8
This comment was minimized by the moderator on the site
Is there a similar formula that can be built for Google Sheets?
This comment was minimized by the moderator on the site
This macro did exactly what I wanted. However it created another problem that I wondered if you might have a solution to.

While this macro is active I cannot select cells for other purposes such as formatting or merging them. Click and drag, shift nor ctrl work to select a group of cells. I can only select the one cell I clicked on. I frequently want to change formatting (background, fill down a formula, etc.) The only way I have been able to do this is to delete the macro, save, make my formatting changes, paste the macro back in and save.

Is there a simpler way to do this? Perhaps:

1. (preferred) A simple keystroke that would temporarily disable the macro and then re-enable it.

2. Some code added to the macro to allow selection of a group of cells.

Dennis
This comment was minimized by the moderator on the site
This was good but I wish there was a way that it would just move with the scroll wheel, like heading lines. Also if I want to select a cell it takes two clicks. The first click moves the chart but also selects the chart so I have to click again to select the cell.
This comment was minimized by the moderator on the site
I added "activecell.select" on the last line of this code and it fixed the double clicking issue. It will automatically select the last active cell, which will be the one you clicked on to move the chart. Hope this helps.
This comment was minimized by the moderator on the site
THIS IS WHAT I NEEDED! GOLDEN!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations