excel 从行到列

3vpjnl9f  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(47)

我的代码当前将我复制的所有文件粘贴到一个单独的列中。
我需要它去为每个新文件,而不是一行不同的列。
即file1到col 1 file2到col 2等
它会涉及到将.row更改为columns或一个新的命令吗?
这里的代码

Option Explicit

Sub CopyDataFromDump()
    Dim SourceFolder As String
    Dim nSourceFolder As String
    Dim Filename As String
    Dim Wb As Workbook
    Dim CopyFromSheet As Worksheet
    Dim PasteToSheet As Worksheet
    Dim LastRow As Long

    SourceFolder = "" ' folder path where your workbooks are located

    Dim ws As Worksheet
    Dim oFSO As Variant
    Dim oFolder As Variant
    Dim oFile As File
    Dim MyFSO As FileSystemObject

    Set MyFSO = New FileSystemObject

    Set oFolder = MyFSO.GetFolder(SourceFolder)

    For Each oFile In oFolder.Files
      Set Wb = Workbooks.Open(oFile, , Format:=5) 'open each workbook in the folder
      Set CopyFromSheet = Wb.Sheets(1) ' sheet from where you want to copy data
      Set PasteToSheet = ThisWorkbook.Sheets(1) ' sheet from where you want to paste data

      CopyFromSheet.UsedRange.Copy   ' Copy data from the specified sheet in the opened workbook

      LastRow = PasteToSheet.Cells(PasteToSheet.Row.Count, "A").End(xlUp).Row + 1     ' Find the last row in the paste sheet

      PasteToSheet.Cells(LastRow, 1).PasteSpecial xlPasteValues  ' Paste data into the destination sheet in the active workbook

      Wb.Close False  ' Close the opened workbook
    Next oFile
End Sub

字符串
我试过Active.sheets.cell。

vmpqdwk3

vmpqdwk31#

假设UsedRange在第1行中没有空单元格:

Dim NextCol As Long
'...
'...
'Find the paste column in the destination sheet
NextCol = PasteToSheet.Cells(1,PasteToSheet.Columns.Count).End(xlToLeft).Column + 1     

PasteToSheet.Cells(1, NextCol).PasteSpecial xlPasteValues

字符串

相关问题