Excel VBA数组列表

我是VBA的蹒跚学步

我有一个很大的范围,这可能是超过1000个文本值(这可能会下降A1),我试图连接所有值与报价和逗号到一个单元格(C1),我知道转置公式,但我不知道我的VBAarrays会认识到这是一个列表。

我热衷于我的数组公式来识别C1作为列表,以执行我的操作。

我非常希望保持这个清洁,而不是使用连接并拖放各种公式。

我碰到过这个,但是这不会把所有的值粘贴到一个单元格中。

Sub transpose() Dim rng As Range Dim ws As Worksheet Dim last As Range Set ws = ActiveSheet Set last = ws.Cells(Rows.Count, "A").End(xlUp) Set rng = ws.Range("A1", last) For Each cell In rng Dim hold As String hold = """" hold = hold + cell.Value hold = hold + """" + ", " cell.Value = hold Next cell rng.Copy ActiveWorkbook.Sheets(2).Range("A1").PasteSpecial transpose:=True End Sub 

由ryan E完成的代码

如果任何人都可以提出任何作弊arrays集合列表将是伟大的。 除了在Excel中使用macros工具

例。

A1 = company1 A2 = company2等

C1会在一个单元格中显示“company1”,“company2”,…“company10000”

你可以使用Join()和Transpose()。

例如:

 Sub transpose() Dim rng As Range Dim ws As Worksheet Dim last As Range Set ws = ActiveSheet Set last = ws.Cells(Rows.Count, "A").End(xlUp) Set rng = ws.Range(ws.Range("A1"), last) ws.Range("B1").Value = """" & Join(Application.Transpose(rng.Value), """,""") & """" End Sub 

编辑 :现在我看到你真正想要做的(创build一个表名称传递给Sheets.Copy() )的数组这里有一种方法…

添加名为(例如)“组”的工作表,以保存要复制的各种工作表清单:

在这里输入图像说明

组名称在第1行,每个名称下面都有一张表。

然后使用这个代码:

 'to demo the "CopySheets()" sub... Sub Tester() CopySheets "Group2" 'copy all sheets in Group2 End Sub 'Create of copy for all sheets under "GroupName" header... Sub CopySheets(GroupName As String) Dim rng As Range, arr Dim ws As Worksheet Dim f As Range Set ws = ThisWorkbook.Sheets("Groups") '<< has lists of sheet names 'find the header for the group to be copied Set f = ws.Rows(1).Find(GroupName, lookat:=xlWhole) If Not f Is Nothing Then 'found the header, so create an array of the sheet names Set rng = ws.Range(f.Offset(1, 0), ws.Cells(ws.Rows.Count, f.Column).End(xlUp)) arr = Application.transpose(rng.Value) 'use the array in the sheets Copy method ThisWorkbook.Sheets(arr).Copy Else 'alert if you tried to copy a non-existent group MsgBox "Sheet group '" & GroupName & "' was not found!" End If End Sub