VBA子将选择中的公式复制为文本

问题描述

我是一名金融专业的学生,​​具有一些基本的编程知识(几年前在我的Uni编程学院学习过使用C ++的Programming 2),但没有VBA经验。我想为一个学生组织开发一个小型的自定义excel外接程序,其中包括一些子控件,这些子控件在创建我们的Excel文件时会节省大量时间。

这些子项目中的前两个是为此目的:我希望能够 将公式从一个单元格区域复制并粘贴到另一个单元格,而无需更改单元格引用 在公式中。要使用该子程序,我们的成员之一将仅使用分配的热键。例如,excel用户可以选择一个单元格区域,然后按(ctrl + alt + shift + c)复制并单击(ctrl + alt + shift + v)粘贴。

我尝试创建copy子,但未成功,因为我不知道如何正确使用/遍历Ranges,甚至不知道何时使用Ranges与多维数组。我也很难在网上找到针对该特定用例的答案,因此,如果可以的话,请允许我向我提供有关该主题的更多资源或任何示例代码的人,将不胜感激。

更多颜色,请参见下面的子图中的粗伪代码和当前进度:

伪代码-复制子

  • 注释-将“选择”中的公式复制为文本到剪贴板
  • (1)获取用户选择的行数和列数(我成功完成了此操作)
  • (2)创建一个与Selection尺寸相同的Range或Array
  • (3)对于myRange中的每个单元格/ myArray中的索引,该值等于Selection中相应单元格的公式,转换为文本
  • (3)将myRange / myArray复制到剪贴板

伪代码-粘贴子

  • (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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...