读取带有值列表的工作表,将其拉入另一个计算器工作表,然后将计算的值复制回第一个工作表

时间:2016-09-27 01:16:37

标签: excel-vba vba excel

我想将C级(带压力补偿)转换为PPMv。

我有两张纸。第一张表是带有原始数据的表。我有另一个计算器表(我从互联网上得到它),它将C级转换为PPMv。我想将2种类型的数据(Degree C& Pressure)复制到计算器中,然后将计算出的值复制回第一张表。

代码:

Sub CaculatePPM()
    Dim DewPoint As Double
    Dim Temp As Double
    Dim PPMv As Double
    Dim Iteration As Integer

    Iteration = 0
    Sheets("DewPoint").Select

    Do Until Selection.Offset(Iteration, 0).Value = ""
        Range("D7").Select
        Selection.Offset(Iteration, 0).Select
        DewPoint = Selection.Value
        Range("F7").Select
        Selection.Offset(Iteration, 0).Select
        Temp = Selection.Value

        Sheets("Calculator").Select
        Range("D14").Select
        Selection.Value = DewPoint
        Range("D16").Select
        Selection.Value = Temp
        Range("B19").Select
        PPMv = Selection.Value

        Sheets("DewPoint").Select
        Range("G7").Select
        Selection.Offset(Iteration, 0).Select
        Selection.Value = PPMv

        Iteration = Iteration - 1

    Loop

End Sub

1 个答案:

答案 0 :(得分:0)

我相信这是您的代码的更好版本:

Sub CaculatePPM()
    Dim Iteration As Integer

    'Set iteration to the first row to be processed    
    Iteration = 7

    'Loop while there is data to be processed
    Do Until Sheets("DewPoint").Cells(Iteration, "D").Value = ""
        'Copy data to Calculator sheet
        Sheets("Calculator").Range("D14").Value = Sheets("DewPoint").Cells(Iteration, "D").Value
        Sheets("Calculator").Range("D16").Value = Sheets("DewPoint").Cells(Iteration, "F").Value

        'This next statement is probably not necessary, but I have included
        'it just in case Excel is set to Manual Calculation
        Sheets("Calculator").Calculate

        'Copy results back to DewPoint sheet
        Sheets("DewPoint").Cells(Iteration, "G").Value = Sheets("Calculator").Range("B19").Value

        'This originally said "Iteration = Iteration - 1"
        'Change it back if you are trying to process rows starting from 7 and working UPWARDS
        Iteration = Iteration + 1
    Loop

End Sub