Excel VBA:获取数据比实际数据少一个

时间:2018-04-11 07:30:04

标签: excel vba excel-vba

我根据我的要求自定义下面的代码,代码工作正常,只是问题不写下最后一行。如果我导入10个文本文件,它将复制9张图片中的值并粘贴到Theta表格中,最后一张图表数据被省略。我无法弄清楚它丢失的地方。需要相同的帮助

`Sub CombineFiles()
Dim xFilesToOpen As Variant
Dim I As Integer
Dim xWb As Workbook
Dim xTempWb As Workbook
Dim xDelimiter As String
Dim xScreen As Boolean
Dim StartTime As Double
Dim MinutesElapsed As String

StartTime = Timer

Application.DisplayAlerts = False

' Extracting PDF to Text using command line
Application.Run "convert.xlsm!PDFExtract"

On Error GoTo ErrHandler
xScreen = Application.ScreenUpdating
Application.ScreenUpdating = False

'Opening a Theta sheet where I want to paste the data
Workbooks.Open Filename:="C:\Backup\PO\Theta.xlsb"

Sheets("Rough").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A1").Select

'Importing Text Files into Excel
xDelimiter = "|"
xFilesToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt", , "PO Extraction", , True)
If TypeName(xFilesToOpen) = "Boolean" Then
    MsgBox "No files were selected", , "PO Extraction"
    GoTo ExitHandler
End If
I = 1
Set xTempWb = Workbooks.Open(xFilesToOpen(I))
xTempWb.Sheets(1).Copy
Set xWb = Application.ActiveWorkbook
xTempWb.Close False
xWb.Worksheets(I).Columns("A:A").TextToColumns _
  Destination:=Range("A1"), DataType:=xlDelimited, _
  TextQualifier:=xlDoubleQuote, _
  ConsecutiveDelimiter:=False, _
  Tab:=False, Semicolon:=False, _
  Comma:=False, Space:=False, _
  Other:=True, OtherChar:="|"
Do While I < UBound(xFilesToOpen)
    I = I + 1
    Set xTempWb = Workbooks.Open(xFilesToOpen(I))
    With xWb
        xTempWb.Sheets(1).Move after:=.Sheets(.Sheets.Count)
        .Worksheets(I).Columns("A:A").TextToColumns _
          Destination:=Range("A1"), DataType:=xlDelimited, _
          TextQualifier:=xlDoubleQuote, _
          ConsecutiveDelimiter:=False, _
          Tab:=False, Semicolon:=False, _
          Comma:=False, Space:=False, _
          Other:=True, OtherChar:=xDelimiter
    End With

    'Replace unwanted lines into blanks
    Application.Run "Convert.xlsm!ReplaceText"

    'Removing Blank Rows after replace
    Range("A1:A10000").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    'Copying a concatenate formula from a cell to a imported worksheet
    Windows("convert.xlsm").Activate
    Sheets("Sheet2").Select
    Range("B1").Select
    Selection.Copy


    Windows("Book1").Activate
    Range("B1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy

    'Copying data from Book1, active imported text file sheet to Theta sheet
    Windows("Theta.xlsb").Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    'Pasted data have lot of unwanted space and chacters, replacing them delimiter
    Application.Run "Convert.xlsm!SelectionReplace"
    ActiveCell.Offset(1, 0).Select
    ActiveWorkbook.Save

Loop

MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
'MsgBox ("Finished")
MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation
ExitHandler:
Application.ScreenUpdating = xScreen
Set xWb = Nothing
Set xTempWb = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description, , "PO Extraction"
Resume ExitHandler
End Sub`

1 个答案:

答案 0 :(得分:0)

变化

-4init

Do While I < UBound(xFilesToOpen)

目前,您没有执行该步骤的最后一个循环:)