是否有可能在Excel 2007中dynamic控制文本框的位置

我在一个仪表板项目上工作,我将有三个值:最小值,最大值和当前值。 最小值和最大值将是一个酒吧的终点,我想放置一个文本框包含当前值沿着酒吧的适当位置。 见下文:

是否有可能在Excel中做到这一点,如果是这样,我将如何去实现这一点。 我有一些Visual Basic的经验,但我没有遇到过这个。

在这里输入图像说明

最终,我正试图在以下链接做一个仪表板的Excel版本:

链接到仪表板

我喜欢你的想法,因此我检查了完整的代码是怎么样的。 结果如下:

 Sub SolutionShape(currentVal) Dim shpBar As Shape, shpCurrent As Shape 'let's assume we have only two shapes on the Activesheet Set shpBar = ActiveSheet.Shapes(1) Set shpCurrent = ActiveSheet.Shapes(2) Dim barMin As Double, barMax As Double barMin = 0.51 'both values could be taken from sheet barMax = 6.75 'let's do it visualy complicated this time :) With shpCurrent .Left = (-.Width / 2 + shpBar.Left) + _ (((currentVal - barMin) / (barMax - barMin)) * shpBar.Width) **'EDITED- adding information about current value:** .TextFrame.Characters.Text = currentVal End With End Sub 

从即时窗口事件调用程序进行testing,例如:

 SolutionShape 0.51 'go to beginning SolutionShape 6.75 'go to end 

这个解决scheme可以在你放置形状的任何地方工作,不pipe你设置的是什么新的尺寸。

当没有selectShape对象时打开macroslogging。 现在select它并改变它的位置。 停止录制并使用生成的代码。

我试过的时候看起来很有用。 我有一些IncrementTop和IncrementLeft代码。 您也可以直接使用Top和Left属性。

将Shape对象的名称改为有意义的(在公式框左侧的地址框中)可能是一个想法,所以你的代码变得更具可读性。

所以对于我名为PositionIndicator的Shape:

 ActiveSheet.Shapes("PositionIndicator").Left = 250 

要么

 ActiveSheet.Shapes("PositionIndicator").Left = _ ActiveSheet.Shapes("PositionIndicator").Left + 5 

要将其链接到单元格值,只需使用Range("CELLADDRESS").Value2

要在每次更改单元格值时应用它,请使用:

 Private Sub Worksheet_Change(ByVal Target As Range) 'Here your script to check if the change concerns one of your input cells and then run the code to change the location of the Shape-object End Sub 

祝你好运

假设进度条是工作表(索引1)上的形状,文本框是形状索引2; 以下内容将根据完成百分比,沿着进度条移动文本框。

注意:必须调整它以抵消箭头左侧文本框形状的部分。

 Option Explicit Public Sub movebox() Dim textbox As Shape, progbar As Shape Dim ws As Worksheet Dim stp As Integer, endp As Integer Dim tbdyn As Integer Dim mn As Double, mx As Double, actper As Double, cur As Double Dim admn As Double, admx As Double Set ws = Sheets("sheet1") Set progbar = ws.Shapes(1) Set textbox = ws.Shapes(2) '// Far left of progress bar position stp = progbar.Left '// Far right of progress bar position endp = (progbar.Width + stp) '// Adjust for starting at 0.51 '// You could adjust mn,mx and cur to take the values '// from the appropriate cells on the spreadsheet mn = 0.51 mx = 6.07 admn = 0 admx = 6.07 - mn cur = 4 '// Calculate percentage complete actper = cur / admx '// Apply percentage to progress bar tbdyn = actper * endp '// Move the textox appropriately textbox.Left = tbdyn End Sub