来自分隔文件的非常简单的VBA复制/粘贴错误地将所有列合并为一个

时间:2015-08-27 14:16:52

标签: excel vba excel-vba delimiter

我有多个文件(fanspeedA,fanspeedB ....),它们没有任何文件扩展名并且是管道分隔的(或“|”)。缺少文件扩展名会导致VBA在复制粘贴期间失去对分隔列的理解吗?

下面是我正在使用的VBA代码,它执行以下步骤:

1)定义变量,存储从

调用此宏的工作表

2)打开一个对话框以选择文件

3)根据文件选择启动循环,用管道分隔符“|”打开每个文件

  • 所有文件都已正确打开,并且已正确分隔所有已识别的列

4)当临时文件打开时,复制使用范围和关闭文件

5)打开原始工作簿,根据临时文件名创建新工作表,将单元格粘贴到此工作表中

  • 此时手动复制/粘贴将保留列,但在VBA中执行此步骤会将所有列合并到第一列中
Sub loopyarray()

Dim filenames As Variant

' get current workbook name to cut/paste opened sheets into
Dim strBookName As Workbook, tmpBookName As String
Set strBookName = ThisWorkbook

' set the array to a variable and the True is for multi-select
filenames = Application.GetOpenFilename(, , , , True)

counter = 1

' ubound determines how many items in the array
While counter <= UBound(filenames)

  ' Opens the selected files
  Workbooks.OpenText filenames(counter), 437, 1, xlDelimited, xlTextQualifierDoubleQuote, 0, 0, 0, 0, 0, 1, "|"

  ' Copy From Temporary Book
  tmpBookName = ActiveSheet.Name 'save temporary sheet name
  ActiveSheet.UsedRange.Select
  Selection.Copy
  ActiveWorkbook.Close

  ' Paste to Original Book
  Windows(strBookName.Name).Activate 'activate original book
  Worksheets.Add(Before:=Worksheets(1)).Name = tmpBookName 'new sheet based on temp sheet name
  Range("A1").Select
  ActiveSheet.Paste

  ' increment counter
  counter = counter + 1

  Wend
End Sub

编辑1:更改了部分sub,现在正在抛出错误。

' Copy From Temporary Book
      tmpBookName = ActiveSheet.Name 'save temporary sheet name
      Dim rngCopy As Range
      Set rngCopy = ActiveSheet.UsedRange
      rngCopy.Copy
      ActiveWorkbook.Close

      ' Paste to Original Book
      Windows(strBookName.Name).Activate 'activate original book
      Worksheets.Add(Before:=Worksheets(1)).Name = tmpBookName 'new sheet based on temp sheet name
      Cells(1, 1).Paste ' THIS IS WHERE ERROR IS OCCURING 

3 个答案:

答案 0 :(得分:0)

尽可能避免使用select。 例如,替换

  

ActiveSheet.UsedRange.Select
    Selection.Copy

dim rngCopy as range
set rngCopy = activesheet.usedrange
    rngCopy.copy

'/ Go to other workbook

ActiveSheet.Cells(1,1).PasteSpecial xlPasteValues

甚至更好,将所有数据放入一个数组(特别是如果你想对它做任何事情),然后将数组打印到第二张表

'/ Determine 1st/last row/column of the data
dim arrData as variant
    arrData = Array()
    ReDim arrData(bounds of the data range)
    arrData = RngCopy (from above)

new sheet:

set rngPaste = '/(Size of Array)
    rngpaste = arrData

答案 1 :(得分:0)

使用数组存储(可能操作)并传输值:

Sub PasteUsingArray()

Dim rngCopy As Range
    rngCopy = ActiveSheet.UsedRange

    Dim LB1 As Long '/ Lower Bound of the 1st dimension (rows)
    Dim UB1 As Long
    Dim LB2 As Long
    Dim UB2 As Long

        LB1 = 1
        LB2 = 1 '/ Standard for arrays unless you specify otherwise

        UB1 = rngCopy.Rows.Count
        UB2 = rngCopy.Columns.Count '/ We now have the size of our data

        Dim arrData As Variant
            arrData = Array()

            ReDim arrData(LB1 To UB1, LB2 To UB2) '/ Now our array is the sime size as our data
            arrData = rngCopy '/ Voila, our array now contains all the data. What was in Cell(1,1) is now in arrData(1,1) [Asuming the data starts in cells(1,1)]

            '/ To print this data to a second sheet, we do this process in reverse.

        Dim rngPaste As Range

        Set rngPaste = Range(Cells(LB1, LB2), Cells(UB1, UB2))

        rngPaste = arrData

End Sub

只要数组变量仍然被引用,您可以在粘贴之前或之后对数据执行操作。

答案 2 :(得分:0)

结合在这里说的一些事情和我在互联网上发现的事情:

此最终代码将打开提示,选择一系列文件,打开每个文件,根据分隔符&#34; |&#34;分隔,将每个文件中使用范围的值保存到原始工作簿中作为新工作表,关闭打开的文件。它绝对不是最有效的,但它不再使用select和activesheet。

Sub loopyarray()

Dim filenames As Variant

' get current workbook name to cut/paste opened sheets into
Dim strBookName As Workbook, tmpBookName As String
Set strBookName = ThisWorkbook
Dim str() As String

Dim myRow As Long
Dim myCol As Long

' set the array to a variable and the True is for multi-select
filenames = Application.GetOpenFilename(, , , , True)

   counter = 1

   ' ubound determines how many items in the array
   While counter <= UBound(filenames)

      ' Opens the selected files
      Workbooks.OpenText filenames(counter), 437, 1, xlDelimited, xlTextQualifierDoubleQuote, 0, 0, 0, 0, 0, 1, "|"

      ' Copy From Temporary Book
      tmpBookName = ActiveSheet.Name ' save temporary sheet name
      Dim rngCopy As Range
      Set rngCopy = ActiveSheet.UsedRange
      Dim inputArray As Variant
      inputArray = rngCopy.Value ' convert used range to array
      ActiveWorkbook.Close

      ' Paste to Original Book
      Windows(strBookName.Name).Activate 'activate original book
      Worksheets.Add(Before:=Worksheets(1)).Name = tmpBookName 'new sheet based on temp sheet name
      For myCol = 1 To UBound(inputArray, 2)
        For myRow = 1 To UBound(inputArray, 1)
            Cells(myRow, myCol).Value = inputArray(myRow, myCol)
        Next
      Next

      ' increment counter
      counter = counter + 1

   Wend
End Sub