将EXCEL表格内各数据按照条件执行不同的运算,并将结果按每行输出为一个独立的txt文件,其相关代码如下
Private Sub export()
Application.ScreenUpdating = False '关闭屏幕刷新Dim nRow&, ncolumn& '定义区域变量
Dim r%, g%, b%, D!, a%
Path = " E:\" '设置导出路径
a = 1
nRow = Cells(Rows.Count, 1).End(3).Row '确定最后行行号
For i = 1 To nRow '在行中进行循环
ncolumn = Cells(i, Columns.Count).End(1).Column '确定每行最后列列号
Open Path & "\file" & i & ".txt" For Output As #1 '创建文本进行输入内容
For j = 1 To ncolumn
If 20 <= Cells(i, j) And Cells(i, j) <= 40 Then
D = (Cells(i, j) - 20) / 20
r = 0 + D * (0 - 0)
g = 0 + D * (255 - 0)
b = 255 + D * (255 - 255)
Print #1, "(", r, ",", g, ",", b, ",", a, ")," 'Print #1, Join(Application.Transpose(Application.Transpose(Range(Cells(i, 1), Cells(i, ncolumn)))), ",") '输入此行内容,以","分隔内容
ElseIf 40 < Cells(i, j) And Cells(i, j) <= 60 Then
D = (Cells(i, j) - 40) / 20
r = 0 + D * (0 - 0)
g = 255 + D * (255 - 255)
b = 255 + D * (0 - 255)
Print #1, "(", r, ",", g, ",", b, ",", a, "),"
ElseIf 60 < Cells(i, j) And Cells(i, j) <= 80 Then
D = (Cells(i, j) - 60) / 20
r = 0 + D * (255 - 0)
g = 255 + D * (255 - 255)
b = 0 + D * (0 - 0)
Print #1, "(", r, ",", g, ",", b, ",", a, "),"
ElseIf 80 < Cells(i, j) And Cells(i, j) <= 160 Then
D = (Cells(i, j) - 80) / 80
r = 255 + D * (255 - 255)
g = 255 + D * (0 - 255)
b = 0 + D * (0 - 0)
Print #1, "(", r, ",", g, ",", b, ",", a, "),"
ElseIf 160 < Cells(i, j) And Cells(i, j) <= 671 Then
D = (Cells(i, j) - 160) / 511
r = 255 + D * (0 - 255)
g = 0 + D * (0 - 0)
b = 0 + D * (0 - 0)
Print #1, "(", r, ",", g, ",", b, ",", a, "),"
End If
If j / 144 = 0 Then
Print #1, "},vect={"
End If
Next
Close #1 '关闭文本
Next '下次循环
Application.ScreenUpdating = True '开启屏幕刷新,与前对应
MsgBox "导出完成" '完成友好提示
End Sub