Excel VBA,Paste special添加尾随零

时间:2015-09-13 21:38:45

标签: vba excel-vba excel

我将来自ANSYS mechanical的原始数据导出为.xml,格式如下(2行,x列数):

Steps   Time [s]    [A] C1  (Total) [N] 
1       1             1,    4,4163e+005 

我有很多文件,我尝试使用VBA将这些文件合并到Excel中的一个表中。该脚本工作正常,但有一个例外,它不能正确解释科学格式。我的结果如下:

  

步骤1
  时间[s] 1
  [A] C1(总计)[N] 4,42E + 09

代码如下:

Private Sub CommandButton1_Click()
Dim directory As String, fileName As String, sheet As Worksheet, total As Integer

Dim wb1 As Excel.Workbook
Dim wb2 As Excel.Workbook
Set wb1 = ThisWorkbook
wb1.Sheets("Sheet1").Cells.ClearContents

'define table headers on row 1
wb1.Sheets("Sheet1").Range("A1:A1").Value = "Load Case"
wb1.Sheets("Sheet1").Range("B1:B1").Value = "Load Case"
wb1.Sheets("Sheet1").Range("C1:C1").Value = "Load Case"

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'directory of source files
directory = "C:\Users\xxxxxxx\Ansysxls\"
fileName = Dir(directory & "*.xl??")

'Define the last used row in the target sheet
LastRow = wb1.Sheets("Sheet1").Cells(wb1.Sheets("Sheet1").Rows.Count, "B").End(xlUp).Row + 1

Do While fileName = "Asymmetric.xls"

 'define which workbook to open
  Set wb2 = Workbooks.Open(directory & fileName)


   'loop through sheets in source file
    For Each sheet In Workbooks(fileName).Worksheets

                  'Select range in source file
                  wb2.Sheets(sheet.Name).Range("A1").CurrentRegion.Select

                  'Replace commas with dot
                  Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
                  SearchOrder:=xlByRows, MatchCase:=False

                  Selection.Copy

                 'Paste Special to target file <-----Smth wrong in my paste special???
                  wb1.Sheets("Sheet1").Range("B" & LastRow).PasteSpecial _
                  Paste:=xlPasteValuesAndNumberFormats, SkipBlanks:=True, Transpose:=True
                  wb2.Sheets(sheet.Name).Activate

    Next sheet

    'define first row and last row of last import and add from what file the came
    FirstRow = LastRow
    LastRow = wb1.Sheets("Sheet1").Cells(wb1.Sheets("Sheet1").Rows.Count, "B").End(xlUp).Row + 1

    'remove file ending ".xls" from column
    wb1.Sheets("Sheet1").Range("A" & FirstRow & ":" & "A" & LastRow).Value = Left(fileName, Len(fileName) - 4)

    Workbooks(fileName).Close
    fileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

'Create Table
 wb1.Sheets("Sheet1").ListObjects.Add(xlSrcRange, Sheets("Sheet1").Range("A1").CurrentRegion, , xlYes, Header = xlYes).Name = "myTable1"

End Sub

有人能帮我理解为什么它会从e + 5变为e + 9吗?

0 个答案:

没有答案