Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在Excel VBA中将列转换为带空格的行吗?_Vba_Excel - Fatal编程技术网

我可以在Excel VBA中将列转换为带空格的行吗?

我可以在Excel VBA中将列转换为带空格的行吗?,vba,excel,Vba,Excel,我目前有一个excel项目,我用它来整理大量的数据,以便将这些数据导入到一个特定的程序中。我目前在一个模块中使用这段代码,并在其他模块中调用它 Sub CopyTransposed(rngSource As Range, rngTargetCell As Range) rngTargetCell.Resize(rngSource.Columns.Count, rngSource.Rows.Count).Value = _ Application.WorksheetFuncti

我目前有一个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,依此类推


谢谢

我认为您可以将解决方案分为两个阶段:

1-保持现在的转置

第二个-创建循环以将内容移动到下一行。比如:

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

用双分隔符连接它们,然后在单个分隔符上拆分

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。

我来试一试!谢谢你的打字错误。谢谢!!工作