简体   繁体   English

我可以将列转置为Excel VBA中包含空格的行吗?

[英]Can I transpose a column to a row WITH spaces in Excel VBA?

I currently have an excel project that I use to sort out large sections of data in order to import that data into a specific program. 我目前有一个excel项目,我用它来整理大部分数据,以便将数据导入特定程序。 I currently use this section of code in one module and call it elsewhere in my other modules. 我目前在一个模块中使用这段代码,并在其他模块中的其他地方调用它。

Sub CopyTransposed(rngSource As Range, rngTargetCell As Range)

    rngTargetCell.Resize(rngSource.Columns.Count, rngSource.Rows.Count).Value = _

    Application.WorksheetFunction.Transpose(rngSource)

End Sub
-------------------------------------------------------------------------------------------
Sub Trans()

    CopyTransposed [A1:A4], [C1]

End Sub

Now I just need to figure out how to add spaces inbetween each cell. 现在我只需要弄清楚如何在每个单元格之间添加空格。 So A1 copies to C1, A2 copies to C3, and so forth. 因此A1复制到C1,A2复制到C3,依此类推。

Thank you 谢谢

Join them with a double delimiter then split on a single. 用双分隔符加入它们然后分成一个。

Sub CopyTransposed(rngSource As Range, rngTargetCell As Range)

    Dim str As String, vals As Variant
    str = Join(Application.Transpose(rngSource), "||")
    vals = Split(str, "|")

    rngTargetCell.Resize(1, UBound(vals) + 1) = vals

End Sub

Sub Trans()

    CopyTransposed [A1:A4], [C1]

End Sub

Not sure if you always need this for rows into columns but you can use the same for columns into rows with an If statement of whether rngSource.Columns.Count > rngSource.Rows.Count. 不确定是否总是需要将行放入列中,但是可以使用相同的列将行添加到具有if语句的行中,该语句是否为rngSource.Columns.Count> rngSource.Rows.Count。

I think you could split your solution in two phases: 我认为您可以分两个阶段分解您的解决方案:

1st - keep doing the transpose as now; 1 - 继续像现在一样进行转置;

2nd - create a loop to move the content to the next row. 第二步 - 创建一个循环以将内容移动到下一行。 Something like: 就像是:

      for i = 1 to 8
          range("C" & i).EntireRow.Insert
          i = i + 1
      next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel / VBA-将列转置为行,在记录之间保留设置的空间 - Excel/VBA - Transpose column to row leaving set spaces between records Excel VBA:将列转置为行 - Excel VBA: Transpose a column to a row 如何在Excel中将列转置为行 - How do I transpose a column into a row in Excel Excel VBA-如何将数据从一列转置到不同列,直到指定字符 - Excel VBA - how can I transpose data from one column to different columns until a specified characters 使用Excel / VBA,如何将列组转置为多行,但仍将组元素保留在同一行中? - With Excel/VBA, how can I transpose groups of columns into multiple rows, but still keeping the group elements within the same row? Excel / Google表格将列转置为行标题 - Excel/Google Sheets Transpose Column into Row Headers 使用 python 将 excel 中的数据从列转置到行 - Transpose data from column to row in excel with python Excel VBA 表操作:基于第一列转置列 - Excel VBA table manipulation: transpose column based on first column 如何使用VBA从特定的Excel单元格迭代到该列中具有值的最新行? - How can I iterate from a specific Excel cell to the latest row having a value in this column using VBA? 如何根据第一行和第一列中的值对一系列矩阵求和 - Excel VBA - How can I sum a series of matrices based on the values in the first row and column - Excel VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM