[英]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.