|
我写了一段代码,
点击第一列单元格就会弹出对话窗口,
可是对话窗口UserForm1每次都是在Excel窗口的中间,
即使设置UserForm1的属性StartUpPosition为手动,
然后在Worksheet_SelectionChange事件中,
UserForm1.Top = Target.Top
UserForm1.Left = Target.Offset(0, 1).Left
还是不行,特别是行数多了,
窗口弹出后就没影了!
请高人指教!
清风_II版主提供的代码:- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- Dim rng As Range, X As Single, Y As Single
- Set rng = ActiveCell
- With ActiveWindow
- X = .PointsToScreenPixelsX((rng.Left + rng.Width) * 4 / 3)
- Y = .PointsToScreenPixelsY((rng.Top + rng.Height) * 4 / 3)
- End With
-
- With UserForm1
- If .Visible = False Then .Show 0
- .Caption = "当前活动单元格:" & rng.Address
- .Move X * 3 / 4, Y * 3 / 4
- End With
- Set rng = Nothing
- End Sub
复制代码 评论:
oobird版主的代码缺乏灵活性,对于行高不同的表格,弹出窗口将会参差不齐;
清风_ll 版主的代码较灵活适用性强。
但是,共同存在的问题是,如果有冻结窗格,弹出窗口还是不能很好的对齐。
小fisher 说:
代码要想通用的话,需要考虑以下几个因素:
1。滚动
2。拆分窗口
3。冻结窗格
4。显示比例
非常麻烦
确实麻烦!
特别是PointsToScreenPixelsX和PointsToScreenPixelsY两个方法,
至今没弄明白在冻结窗格时,是如何用的。
以下为解决代码,拆分没考虑,解决了滚动、冻结,
显示比例就选默认的96 DPI,不折腾了,这样就够实用的了。
-
- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- Dim x As Single, y As Single
- Dim Pances1Row As Long, Pances1Col As Long
- Pances1Row = ActiveWindow.Panes(1).VisibleRange.Rows.Count
- Pances1Col = ActiveWindow.Panes(1).VisibleRange.Columns.Count
-
- With ActiveWindow
- If .FreezePanes = False Then '当没有冻结窗格
- x = .PointsToScreenPixelsX((.Selection.Left + .Selection.Width) * 4 / 3) '4/3为像素转换为磅的系数,当DPI设置为96时,即4个像素为3磅。
- y = .PointsToScreenPixelsY((.Selection.Top + .Selection.Height) * 4 / 3)
- Else
- If .Panes.Count = 2 Then '如果有两个Panes冻结窗格
- If .Selection.Row > Pances1Row Then '上下分的时候,如果选中的单元格在下边的Panes(2)中
- x = .PointsToScreenPixelsX((.Selection.Left + .Selection.Width) * 4 / 3)
- y = .PointsToScreenPixelsY((.Selection.Top + .Selection.Height) * 4 / 3) + (.Panes(1).VisibleRange.Rows.Height + 14.25) * 4 / 3 '14.25 Point为默认的一个行高值。
- ElseIf .Selection.Column > Pances1Col Then '左右分的时候,如果选中的单元格在右边的Panes(2)中
- x = .PointsToScreenPixelsX((.Selection.Left + .Selection.Width) * 4 / 3) + (.Panes(1).VisibleRange.Columns.Width + 25.4) * 4 / 3 '25.4 Point为测试得到的一个值。
- y = .PointsToScreenPixelsY((.Selection.Top + .Selection.Height) * 4 / 3)
- Else '其他就是在Panes(1)中
- x = .PointsToScreenPixelsX((.Selection.Left + .Selection.Width) * 4 / 3)
- y = .PointsToScreenPixelsY((.Selection.Top + .Selection.Height) * 4 / 3)
- End If
- Else '其他就是有四个Panes冻结窗格的情况
- If .Selection.Row > Pances1Row And .Selection.Column > Pances1Col Then '如果选中的单元格在右下Panes
- x = .PointsToScreenPixelsX((.Selection.Left + .Selection.Width) * 4 / 3) + (.Panes(1).VisibleRange.Columns.Width + 25.4) * 4 / 3
- y = .PointsToScreenPixelsY((.Selection.Top + .Selection.Height) * 4 / 3) + (.Panes(1).VisibleRange.Rows.Height + 14.25) * 4 / 3
- ElseIf .Selection.Row > Pances1Row And .Selection.Column <= Pances1Col Then '如果选中的单元格在左下Panes
- x = .PointsToScreenPixelsX((.Selection.Left + .Selection.Width) * 4 / 3)
- y = .PointsToScreenPixelsY((.Selection.Top + .Selection.Height) * 4 / 3) + (.Panes(1).VisibleRange.Rows.Height + 14.25) * 4 / 3
- ElseIf .Selection.Row <= Pances1Row And .Selection.Column > Pances1Col Then '如果选中的单元格在右上Panes
- x = .PointsToScreenPixelsX((.Selection.Left + .Selection.Width) * 4 / 3) + (.Panes(1).VisibleRange.Columns.Width + 25.4) * 4 / 3
- y = .PointsToScreenPixelsY((.Selection.Top + .Selection.Height) * 4 / 3)
- Else '其他就是选中的单元格在左上Panes(1)中
- x = .PointsToScreenPixelsX((.Selection.Left + .Selection.Width) * 4 / 3)
- y = .PointsToScreenPixelsY((.Selection.Top + .Selection.Height) * 4 / 3)
- End If
- End If
- End If
- End With
-
- With UserForm1
- If .Visible = False Then .Show 0
- .Move x * 3 / 4, y * 3 / 4 'Move 方法,参数为单精度值,以磅为单位。
- End With
- End Sub
复制代码
[ 本帖最后由 向東 于 2009-6-7 10:34 编辑 ] |
|