从多行单元格列生成列表

发布于 2024-11-29 19:29:24 字数 196 浏览 0 评论 0原文

我有一个表,其中一列是项目的属性。一个项目可以有许多属性,所有这些属性都存储在每个项目的一个单元格中,以单独的文本行形式存储。我想制作包含迄今为止我使用过的所有属性类型的脚本,并将该列表保存在第二张表中。它可能无法通过标准公式实现,因此我假设需要使用VBA。我以前从未使用过VBA,不知道如何完成这项任务。我也找不到这种语言的任何参考。我正在使用 MS Office 2007。

I've got a table and one of its column is kind of attributes of item. One item can have many attributes, and all of them are stored in one cell per each item, in seperate lines of text. I want to make script that enlist all the attribute types I've used so far and save that list in the second sheet. It propably cannot be achieved by standard formulas, so I assume the need to use VBA. I never used VBA before and don't know how to do this task. Also I can't find any reference of this language. I'm using MS Office 2007.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2024-12-06 19:29:24
Sub export()
Dim cell As Range
Dim i As Long, j As Long
Dim var() As String
j = 1
For Each cell In Sheets(1).Range("a1:a10")
    Var = Split(cell, vbLf)
        For i = 0 To UBound(Var)
            Sheets(2).Range("a" & j).Value = Var(i)
            j = j + 1
        Next i    
Next cell
End Sub

我的示例循环sheet1 的a1:a10 范围内的所有单元格。根据您的需求进行调整。
每个单元格都按换行符拆分,并将每一行复制到第二个工作表中。

Sub export()
Dim cell As Range
Dim i As Long, j As Long
Dim var() As String
j = 1
For Each cell In Sheets(1).Range("a1:a10")
    Var = Split(cell, vbLf)
        For i = 0 To UBound(Var)
            Sheets(2).Range("a" & j).Value = Var(i)
            j = j + 1
        Next i    
Next cell
End Sub

My example loops all the cells in the range a1:a10 of sheet1. Adapt it to your needs.
Each cell is splitted on linefeed and each row copied in the second sheet.

站稳脚跟 2024-12-06 19:29:24

这是 @nick Rulez 的精彩答案的修改版本,它使用字典对象,允许您排除所有重复项。

Sub export()

Dim cell As range
Dim i As Long, j As Long
Dim var As Variant
Dim dictionary As Object
Set dictionary = CreateObject("scripting.dictionary")

On Error Resume Next
j = 1
For Each cell In Sheets(1).range("a1:a10")
    var = Split(cell, vbLf)
    For i = 0 To UBound(var)
        dictionary.Add var(i), 1
        j = j + 1
    Next i
Next

Sheet2.range("A1").Resize(dictionary.count).Value = _
Application.Transpose(dictionary.keys)

End Sub

工作原理:它与 nick 的功能相同,但不是将每个拆分条目写入sheet2,而是将条目添加到字典文件中。由于字典不允许有 2 个具有相同值的键,因此它会跳过重复项(不过,在接下来的错误恢复中您需要重复重复项)。 Dictionary 具有超酷的调换键数组的能力,所以最后,我只需一步复制整个列表即可。

Here is a modified version of @nick rulez's great answer that uses a dictionary object, allowing you to exclude all duplicates.

Sub export()

Dim cell As range
Dim i As Long, j As Long
Dim var As Variant
Dim dictionary As Object
Set dictionary = CreateObject("scripting.dictionary")

On Error Resume Next
j = 1
For Each cell In Sheets(1).range("a1:a10")
    var = Split(cell, vbLf)
    For i = 0 To UBound(var)
        dictionary.Add var(i), 1
        j = j + 1
    Next i
Next

Sheet2.range("A1").Resize(dictionary.count).Value = _
Application.Transpose(dictionary.keys)

End Sub

How it works: It does the same thing as nick's but instead of writing each split entry to sheet2, it adds the entries to a dictionary file. Since a dictionary won't allow 2 keys of the same value, it skips dupes (you need that on error resume next though). Dictionary comes with the ultra-cool ability to transpose the array of keys, so at the end, I just copy the whole list over in one move.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文