自动更改excel中的值

时间:2017-04-27 09:07:56

标签: vba

Data after closig and opening of excel我的excel有一个棘手的情况。我编写了一个VBA代码,它从许多CSV文件中收集HEX数据并将其转换为十进制并将其存储为十进制。这在测试时完美无缺,我没有问题,我也保存了它。但是当我重新打开excel时,一半的单元格返回到十六进制格式,只有一半是十进制格式。我不知道为什么会这样。当它第一次这样做时它可以工作,但是在保存关闭它并再次打开它给我这个问题 这是vba代码 OriginalData

Sub Sample()

Dim myfiles
Dim i As Integer
Dim J As Long
Dim l As Long
Dim LastRow As Long


myfiles = Application.GetOpenFilename(filefilter:="CSV Files (*.csv), *.csv", MultiSelect:=True)

If Not IsEmpty(myfiles) Then
For i = LBound(myfiles) To UBound(myfiles)
     With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & myfiles(i), Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1, 0))
        .Name = "Sample"
        .FieldNames = False
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 3
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
Next i

    LR = Range("A" & Rows.Count).End(xlUp).Row
    For J = 2 To LR

    Cells(J, 4).Value = CLng("&H" & Mid(Cells(J, 4).Value, 4, Len(Cells(J, 4).Value)))
    Cells(J, 5).Value = CLng("&H" & Mid(Cells(J, 5).Value, 8, Len(Cells(J, 5).Value)))
    Cells(J, 6).Value = CLng("&H" & Mid(Cells(J, 6).Value, 8, Len(Cells(J, 6).Value)))
    Cells(J, 7).Value = CLng("&H" & Mid(Cells(J, 7).Value, 8, Len(Cells(J, 7).Value)))
    Cells(J, 8).Value = CLng("&H" & Mid(Cells(J, 8).Value, 4, Len(Cells(J, 8).Value)))
    Cells(J, 9).Value = CLng("&H" & Mid(Cells(J, 9).Value, 8, Len(Cells(J, 9).Value)))

   Next
   LastRow = Range("C" & Rows.Count).End(xlUp).Row

   For l = 2 To LastRow

   'Cells(l, 14).Value = Left(Cells(l, 3).Value, 3)
   'Cells(l, 13).Value = Right(Range(l, 3).Value, 4)
   '(l, 12).Value = Val(Left(Right(Cells(l, 3).Value, 7), 2))
   Cells(l, 10).Value = Left(Cells(l, 3).Value, 3) + Val(Left(Right(Cells(l, 3).Value, 7), 2)) / 60 + Right(Cells(l, 3).Value, 4) / 3600

   Next

   Else
   MsgBox "No File Selected"
   End If

   End Sub

我同时解析多个CSV文件,因此在重新打开时,只有第一个文件的解析保留为十进制格式,其他文件将更改为原始十六进制格式

0 个答案:

没有答案