热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将2D数组转换为1D数组-Converta2Darraytoa1Darray

HowdoIworkaroundthe255characterspercelllimitwhenconvertingarange(multidimensionala

How do I work around the 255 characters per cell limit when converting a range (= multidimensional array) to single dimensional array with the use of Application.Index(array, row, column)?

使用Application.Index(数组,行,列)将范围(=多维数组)转换为单维数组时,如何解决每个单元格限制255个字符?

The following truncated example reproduces the error:

以下截断的示例重现错误:

Error 13. Type mismatch

错误13.类型不匹配

(The complete code is on superuser where I tried to help another user).

(完整的代码是超级用户,我试图帮助其他用户)。

How to reproduce

  • Open a new Excel sheet and insert the formula =REPT("x",256) to cell A1
    This creates a 256 characters long string which is just 1 character too long for the last step

    打开一个新的Excel工作表并将公式= REPT(“x”,256)插入单元格A1这将创建一个长度为256个字符的字符串,该字符串对于最后一步而言只有1个字符太长

  • Open the VBA editor (Alt+F11) and paste the below code somewhere

    打开VBA编辑器(Alt + F11)并将以下代码粘贴到某处

  • Execute the code line by line with F8

    用F8逐行执行代码

    Function StringLengthTest()       
        Dim arr2D As Variant
        Dim arr1D As Variant        
        arr2D = Rows(1)
        arr1D = Application.Index(arr2D, 1, 0)        
    End Function
    
  • You'll see the same error at the last line when Excel tries to convert a range (2D) to a 1D array while one of its cells has more than 255 characters.

    当Excel尝试将范围(2D)转换为1D数组时,您会在最后一行看到相同的错误,而其中一个单元格的字符数超过255个。

To prove this, change =REPT("x",256) to =REPT("x",255) and run the code again. This time it will work.

为了证明这一点,将= REPT(“x”,256)更改为= REPT(“x”,255)并再次运行代码。这次它会起作用。

Question: Should I declare my variables in another way? Is there a better way to convert a range (which is always a 2D object at first) to a single dimensional array?

问题:我应该以另一种方式声明我的变量吗?有没有更好的方法将范围(最初始终是2D对象)转换为单维数组?

I know I could use a loop to iterate through the arrays and save all 2D array values one by one to a 1D array. But that's not efficient. Imagine really large sheets.

我知道我可以使用循环迭代数组并将所有2D数组值逐个保存到1D数组。但那效率不高。想象一下真的很大的床单。

1 个解决方案

#1


0  

by far the best way of getting anything from a cells into memory (an array) is to use an array variant. I think the problem you are having is with index not with your method.

到目前为止,从单元格到内存(数组)中获取任何内容的最佳方法是使用数组变体。我认为你遇到的问题是索引而不是你的方法。

Hopefully this code should explain it.

希望这段代码能够解释它。

Dim v_data As Variant
Dim rw As Long, cl As Long ' for row and column
Dim arr1d() As Variant
Dim count As Long

' I'm going to use UsedRange to get the whole sheet  .UsedSheet
' If you just want things from one row or column then just spec
' activesheet.range("A1:A100") or use cells()
With ActiveSheet.UsedRange
  ReDim v_data(1 To .Rows.count, 1 To .Columns.count)
  v_data = .Value
End With

'now we have all the things from that sheet.
'so to convert to 1d array where the cell value is say = 1
For rw = LBound(v_data) To UBound(v_data)
   For cl = LBound(v_data, 2) To UBound(v_data, 2) ' note the comma 2 for the second dimension bounds.
       If v_data(rw, cl) = 1 Then
           count = count + 1
           ReDim Preserve arr1d(1 To count)
           arr1d(count) = v_data(rw, cl)
       End If
   Next cl
Next rw

For count = LBound(arr1d) To UBound(arr1d)
    Debug.Print arr1d(count)
Next count

now the trick is to farm this off to a function that takes a few args( a 2d range, what you are looking for in that range) and returns your list.

现在的诀窍就是把它变成一个带有几个args的函数(一个2d范围,你在那个范围内寻找的东西)并返回你的列表。

To get your data back into a workbook

将数据恢复到工作簿中

ActiveSheet.Cells(1, 1).Resize(UBound(arr1d), 1).Value = arr1d

make a range of the exact same size in terms of the bounds of your array and then ensuring you use .value just pop in the variant array.

根据数组的边界确定一个完全相同的范围,然后确保在变量数组中使用.value。


推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • 006_Redis的List数据类型
    1.List类型是一个链表结构的集合,主要功能有push,pop,获取元素等。List类型是一个双端链表的结构,我们可以通过相关操作进行集合的头部或者尾部添加删除元素,List的设 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
author-avatar
PearlLisa_Shanghai_901
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有