我有一个Excel文件,其中包含大量以科学计数法表示的数据,并按以下列排序:
我有一些VB代码,可从Excel提取数据的每一列并将每一列写入文本文件:
Sub ExportToNotepad()
Dim wsData As Variant
Dim myFileName As String
Dim FN As Integer
Dim p As Integer, q As Integer
Dim path As String
Dim myString As String
Dim lastrow As Long, lastcolumn As Long
lastrow = Sheets("flux2018").Range("B" & Rows.Count).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(29, Columns.Count).End(xlToLeft).Column
path = "C:\Users\bloughj\Documents\NFluxes\"
For p = 1 To lastcolumn
wsData = ActiveSheet.Cells(28, p).Value
If wsData = "" Then Exit Sub
myFileName = wsData
myFileName = myFileName & ".txt"
myFileName = path & myFileName
For q = 29 To lastrow
myString = myString & vbCrLf & Cells(q, p)
FN = FreeFile
Open myFileName For Output As #FN
Print #FN, myString
Close #FN
Next q
myString = ""
Next p
End Sub
似乎是在提取数据,但并没有像原始数据那样将它放在科学计数法中,带有4个小数位。同样,每个生成的文本文件都以空行开头:
示例:
24
20000000
10000000
5000000
2000000
1000000
100000
10000
1000
100
10
1
0.625
0.5
0.4
0.3
0.2
0.1
0.08
0.06
0.04
0.03
0.02
0.01
0.0002
0.00001
1.0515639915552E-05
3.63926199086599E-04
1.9676665491577E-03
1.42654567203907E-03
5.81606017515438E-03
1.00247561028745E-02
1.32534668446465E-02
1.54728811746672E-02
1.69472358325774E-02
1.87875054958548E-02
4.40254611566843E-03
2.24021737838812E-03
2.37656211009138E-03
3.40419784285222E-03
7.97308912593133E-03
9.15215298875479E-02
7.52308259032017E-02
0.12439644621192
0.192207571010406
0.120888605459018
0.126903744311048
0.112336857282057
5.20321789727604E-02
1.37510447075413E-05
56
20000000
6430000
4300000
3000000
1850000
1500000
1200000
861000
750000
600000
470000
330000
270000
200000
50000
20000
17000
3740
2250
192
188
118
116
105
101
67.5
65
37.1
36
21.8
21.2
20.5
7
6.88
6.5
6.25
5
1.13
1.08
1.01
0.625
0.45
0.375
0.35
0.325
0.25
0.2
0.15
0.1
0.08
0.06
0.05
0.04
0.0253
0.01
0.004
0.00001
如何获取它以科学计数格式将输出格式设置为小数点后四位,就像原始数据一样?以及如何摆脱每个.txt文件开头的空白行?
谢谢。
您不需要VBA来执行此操作。如果需要宏,则可以使用宏记录器记录自己执行这些步骤。然后将生成的VBA编辑为执行该操作的可重用函数。