Excel VBA:按要求处理数据

将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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值