调试通过的例子的VBA代码如下图:
例子程序执行后,工作表的结果如下图:
程序文本代码如下,请把例子中的*.txt修改为*.amp:
Option Explicit
Sub 导入文件的第一行()
Dim p$, f$, i&, s$
p = "d:\doc\" '文件所在位置,必须以\结尾
f = Dir(p &"*.txt") '可能需要修改扩展名
While f <>""
Open p &f For Input As #1
Input #1, s
Close #1
i = i + 1
Cells(i, 1) = f
Cells(i, 2) = s
f = Dir
Wend
End Sub
例子数据源:
程序代码:
运行结果:
是不是很完美,程序文本:
Option Explicit
Sub 转换()
Dim a1, a2(1 To 1000, 1 To 100), i, j, x, y, m, n
Set x = CreateObject("Scripting.Dictionary")
Set y = CreateObject("Scripting.Dictionary")
a1 = Range("a1").CurrentRegion
For i = 1 To UBound(a1)
If a1(i, 1) = "价格" Then
If Not y.Exists(a1(i, 2)) Then
a2(1, y.Count + 2) = a1(i, 2)
y.Add a1(i, 2), y.Count + 2
End If
n = y(a1(i, 2))
Else
If Not x.Exists(a1(i, 1)) Then
a2(x.Count + 2, 1) = a1(i, 1)
x.Add a1(i, 1), x.Count + 2
End If
m = x(a1(i, 1))
a2(m, n) = a1(i, 2)
End If
Next i
Range("d1").Resize(x.Count + 1, y.Count + 1) = a2
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)