VBA - Selection.Formula不会返回所有内容

时间:2015-05-14 21:57:01

标签: vba excel-vba excel-formula formulas excel

假设我在Excel中的一个单元格中有一个很长的公式:

  

= IF(SUM(D3:D6)> 1," A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z 01",   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   02&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   03&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   04&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   05&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   06&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   07&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   08&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   09&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   10&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   11&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   12&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   13&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   14""无" ))))))))))))))

现在我调用以下VBA sub

Private Sub Get_Formula()
    MsgBox Selection.Formula
End Sub

然后我没有收到完整的公式,但只有

  

= IF(SUM(D3:D6)> 1," A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z 01",   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   02&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   03&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   04&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   05&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   06&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   07&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   08&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   09&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   10&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   11&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   12&#34 ;,   IF(SUM(D3:D6)大于1," A-B-C-d-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z   13",IF(SUM(D3:D6)> 1," A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V

如何获得完整的公式?

2 个答案:

答案 0 :(得分:2)

MsgBox函数的“prompt”参数的最大长度大约为1024个字符,具体取决于所用字符的宽度。 (see https://msdn.microsoft.com/en-us/library/office/gg251821(v=office.15).aspx

您可以使用以下代码在Visual Basic即时窗口中打印整个公式:

Sub Get_Formula()
    Rem Opens Immediate Window and Clears it
    SendKeys "^g^a{DEL}": Stop

    With ActiveCell
        Debug.Print vbLf; vbLf; String(131, "*")
        Debug.Print "Formula in Wbk\Wsh\Cell: "; .Parent.Parent.Name; " \ "; .Parent.Name; " \ "; .Address(0, 0)
        Debug.Print ActiveCell.Formula
        Debug.Print vbLf; String(131, "*"); vbLf
    End With
    SendKeys "^g^{HOME}" ': Stop
End Sub

此外,我看到公式中存在一些不一致之处,但我想这只是一个伪造的公式,用于解释MsgBox函数的问题。 这个长公式难以阅读,我可以建议使用 Alt + Enter 组合键在同一单元格中开始一个新行,从而在几行中打破你的公式(见下图)

enter image description here

答案 1 :(得分:0)

我把你相当大的公式放在 A1 然后在 A2 A15 我插入了:

=MID(A$1,76*(ROW()-2)+1,75)

我运行了这个宏:

Sub dural2()
  With Range("A1")
    .Formula = "'" & .Formula
  End With

  ActiveSheet.ListBoxes.Add(332.25, 62.25, 242.25, 120).Select
    s = Selection.Name
    ActiveSheet.Shapes(s).Select
    With Selection
        .ListFillRange = "A2:A15"
        .LinkedCell = "$Z$1"
        .MultiSelect = xlNone
        .Display3DShading = True
    End With
End Sub

并得到了这个有点丑陋的结果:

enter image description here

我不是将此作为一种实用的解决方案。只是一个带有滚动条的 ListBox 可以显示足够字符的演示