Fast processing for cell-by-cell computation.

Post Reply
wwj
Posts: 2497
Joined: 27 Jan 2007 08:16

Fast processing for cell-by-cell computation.

Post by wwj »

Fast processing for cell-by-cell computation.

Code: Select all


Sub convert_elapsed_time(flag As String)
    '
    '   Subtract the ss time (127750.0 days for SS, sp=1-5
    '   Fast processing....
    '
    Dim wsOut As Worksheet
    
    ThisWorkbook.Activate
    
    inSht = "FormedData"
    Set wsIn = ThisWorkbook.Sheets(inSht)

    ' Number of columns in the formed-data sheet
    maxCol = wsIn.Cells(2, Columns.Count).End(xlToLeft).Column
    tm4ss = 127750#  ' days for SP=1-5.
    
    For j = 1 To maxCol Step 2
    
        lastRow = wsIn.Cells(Rows.Count, j).End(xlUp).Row
        
        ' Preallocate observation data array
        maxRow = lastRow - 2
        ReDim Data(1 To maxRow, 1 To 1)
        
        For i = 1 To maxRow
            tm = wsIn.Cells(i + 2, j)
            Data(i, 1) = tm - tm4ss
        Next i
        
        ' Write model data in bulk
        wsIn.Range(wsIn.Cells(3, j), wsIn.Cells(lastRow, j)).Value = Data
        
    Next j
    
End Sub


Convert times to Dates

Code: Select all


Sub convert_elapsed_time_to_date(flag As String)
    '
    '   Day 1 = April 9, 1996.
    '   Fast processing....
    '
    Dim wsIn As Worksheet
    Dim wsOut As Worksheet
    
    ThisWorkbook.Activate
    
    inSht = "FormedData"
    outSht = "HwDate"

    ' Define the source and destination sheets
    Set wsIn = ThisWorkbook.Sheets(inSht)
    Set wsOut = ThisWorkbook.Sheets(outSht)
    
    ' Clear the destination sheet before copying
    wsOut.Cells.Clear
    
    ' Copy the contents from Sheet A to Sheet B
    wsIn.Cells.Copy Destination:=wsOut.Cells
    
    ' Convert the elapsed time to date.
    ' Number of columns in the formed-data sheet
    maxCol = wsOut.Cells(2, Columns.Count).End(xlToLeft).Column
    
    For j = 1 To maxCol Step 2
    
        lastRow = wsOut.Cells(Rows.Count, j).End(xlUp).Row
        
        ' Preallocate observation data array
        maxRow = lastRow - 2
        ReDim Data(1 To maxRow, 1 To 1)
        
        For i = 1 To maxRow
            tm = wsIn.Cells(i + 2, j)
            
            If tm >= 0 Then
                Data(i, 1) = tm + DateValue("4/9/1996")
            Else
                wsOut.Cells(i + 2, j) = ""
                wsOut.Cells(i + 2, j + 1) = ""
            End If
        Next i
        
        ' Write model data in bulk
        wsOut.Range(wsOut.Cells(3, j), wsOut.Cells(lastRow, j)).Value = Data
        
    Next j
    
    ' Message box
    MsgBox "Done for converting elapsed times to date! "
    
End Sub


Post Reply