VBA Sub将所选内容中的公式复制为文本 我是一名金融专业的学生,有一些基本的编程知识(在几年前我的UNI上使用C++编程2),但没有VBA经验。我想为一个学生组织开发一个小的自定义excel外接程序,其中包括一些Sub,在创建excel文件时可以节省大量时间,因为excel文件变得非常大

VBA Sub将所选内容中的公式复制为文本 我是一名金融专业的学生,有一些基本的编程知识(在几年前我的UNI上使用C++编程2),但没有VBA经验。我想为一个学生组织开发一个小的自定义excel外接程序,其中包括一些Sub,在创建excel文件时可以节省大量时间,因为excel文件变得非常大,excel,vba,range,copy-paste,excel-addins,Excel,Vba,Range,Copy Paste,Excel Addins,前两个sub用于此:我希望能够将公式从一个单元格范围复制并粘贴到另一个单元格范围,而无需更改公式中的单元格引用。要使用sub,我们的一个成员只需使用指定的热键。例如,excel用户可以选择一系列单元格,然后点击(ctrl+alt+shift+c)进行复制,点击(ctrl+alt+shift+v)进行粘贴 我试图创建复制子对象,但没有成功,因为我不知道如何正确使用/迭代范围,甚至不知道何时使用范围与多维数组。我也很难在网上找到这个特定用例的答案,所以如果可能的话,我非常感谢任何人向我提供关于这个主

前两个sub用于此:我希望能够将公式从一个单元格范围复制并粘贴到另一个单元格范围,而无需更改公式中的单元格引用。要使用sub,我们的一个成员只需使用指定的热键。例如,excel用户可以选择一系列单元格,然后点击(ctrl+alt+shift+c)进行复制,点击(ctrl+alt+shift+v)进行粘贴

我试图创建复制子对象,但没有成功,因为我不知道如何正确使用/迭代范围,甚至不知道何时使用范围与多维数组。我也很难在网上找到这个特定用例的答案,所以如果可能的话,我非常感谢任何人向我提供关于这个主题的更多资源或任何示例代码

有关更多颜色,请参见下面子部分的大致psuedocode和当前进度:

Psuedocode-复制子项

Sub CopyFormulasAsText()
    ' General documentation:
        ' This sub is intened to allow one to copy and paste formulas without changing the cell references
        ' In particular, this sub simply copies all formulas in the user selection as text
        ' with the same references embedded
        
    
    ' define variables
    Dim selctnRowCount As Integer
    Dim selctnColCount As Integer
    
    ' assign values to variables. These values are the number of rows and columns in the user selection
    selctnRowCount = Selection.Rows.Count
    selctnColCount = Selection.Columns.Count

    ' create an A x B size matrix, where A and B are the number of rows and columns in the user selection area, respectively
    ' this array will hold the formulas in each cell, which we will then copy to the clipboard as text
    
    Dim formulasArray(selctnRowCount, selctnColCount) As String
    
    ' I am unclear what next steps to take in this sub, or whether my my formulasArray should be a Range

End Sub
  • 注释-将所选内容中的公式作为文本复制到剪贴板
  • (1) 获取用户选择的行数和列数(我成功地做到了这一点)
  • (2) 创建与所选对象具有相同尺寸的范围或数组
  • (3) 对于myArray中myRange/index中的每个单元格,该值等于所选内容中相应单元格的公式,并转换为文本
  • (3) 将myRange/myArray复制到剪贴板
Psuedocode-粘贴子项

Sub CopyFormulasAsText()
    ' General documentation:
        ' This sub is intened to allow one to copy and paste formulas without changing the cell references
        ' In particular, this sub simply copies all formulas in the user selection as text
        ' with the same references embedded
        
    
    ' define variables
    Dim selctnRowCount As Integer
    Dim selctnColCount As Integer
    
    ' assign values to variables. These values are the number of rows and columns in the user selection
    selctnRowCount = Selection.Rows.Count
    selctnColCount = Selection.Columns.Count

    ' create an A x B size matrix, where A and B are the number of rows and columns in the user selection area, respectively
    ' this array will hold the formulas in each cell, which we will then copy to the clipboard as text
    
    Dim formulasArray(selctnRowCount, selctnColCount) As String
    
    ' I am unclear what next steps to take in this sub, or whether my my formulasArray should be a Range

End Sub
  • (1) 将剪贴板内容(myRange/myArray)粘贴为文本
  • (2) 如有必要,将这些粘贴内容格式化为公式
当前进度-复制子项

Sub CopyFormulasAsText()
    ' General documentation:
        ' This sub is intened to allow one to copy and paste formulas without changing the cell references
        ' In particular, this sub simply copies all formulas in the user selection as text
        ' with the same references embedded
        
    
    ' define variables
    Dim selctnRowCount As Integer
    Dim selctnColCount As Integer
    
    ' assign values to variables. These values are the number of rows and columns in the user selection
    selctnRowCount = Selection.Rows.Count
    selctnColCount = Selection.Columns.Count

    ' create an A x B size matrix, where A and B are the number of rows and columns in the user selection area, respectively
    ' this array will hold the formulas in each cell, which we will then copy to the clipboard as text
    
    Dim formulasArray(selctnRowCount, selctnColCount) As String
    
    ' I am unclear what next steps to take in this sub, or whether my my formulasArray should be a Range

End Sub

谢谢您的反馈@Mech,我已经更新了我的帖子以包含我当前的进度。如果您有
FORMULATEXT
功能,那么您可以通过引用不带VBA的单元格(例如
=FORMULATEXT(A1)
等)来实现这一点。谢谢@shrivallabha.redij。我知道如何使用FORMULATEXT(),我还知道在Excel中解决此问题的其他方法,但它们都需要很短的时间,这就是问题所在。该解决方案无法修复我的用例。我想完成这个宏,因为虽然它在一个实例中节省了少量时间,但在长时间运行中它会显著增加。那么,为什么要将公式复制为文本,然后再复制回公式中呢?不是100%这是你想要的,但是你可以做
Range(ReceivingRange)。Formula=Range(CopyingFormula)。Formula
。这基本上与新范围内的公式相同。它也不会改变引用。用这种方式(如果可以的话)而不是复制/粘贴的优点是速度要快得多。