我目前有一个excel项目,我用它来整理大部分数据,以便将数据导入特定程序。我目前在一个模块中使用这段代码,并在其他模块的其他地方调用它。
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
现在我只需要弄清楚如何在每个单元格之间添加空格。所以A1复制到C1,A2复制到C3,依此类推。
谢谢
答案 0 :(得分:2)
用双分隔符加入它们然后分成一个。
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
不确定是否总是需要将行放入列中,但是您可以使用相同的列将行放入具有if语句的行中,该语句是否为rngSource.Columns.Count> rngSource.Rows.Count。
答案 1 :(得分:1)
我认为您可以分两个阶段分解您的解决方案:
1st - 继续像现在一样进行转置;
第二步 - 创建一个循环以将内容移动到下一行。类似的东西:
for i = 1 to 8
range("C" & i).EntireRow.Insert
i = i + 1
next