What is the difference between .text, .value, and .value2?

后端 未结 6 1901
孤独总比滥情好
孤独总比滥情好 2020-11-22 00:07

What is the difference between .text, .value, and .value2? Such as when should target.text, target.value, and target.value2 be used?

6条回答
  •  故里飘歌
    2020-11-22 00:15

    Out of curiosity, I wanted to see how Value performed against Value2. After about 12 trials of similar processes, I could not see any significant differences in speed so I would always recommend using Value. I used the below code to run some tests with various ranges.

    If anyone sees anything contrary regarding performance, please post.

    Sub Trial_RUN()
        For t = 0 To 5
            TestValueMethod (True)
            TestValueMethod (False)
        Next t
    
    End Sub
    
    
    
    
    Sub TestValueMethod(useValue2 As Boolean)
    Dim beginTime As Date, aCell As Range, rngAddress As String, ResultsColumn As Long
    ResultsColumn = 5
    
    'have some values in your RngAddress. in my case i put =Rand() in the cells, and then set to values
    rngAddress = "A2:A399999" 'I changed this around on my sets.
    
    
    
    With ThisWorkbook.Sheets(1)
    .Range(rngAddress).Offset(0, 1).ClearContents
    
    
    beginTime = Now
    
    For Each aCell In .Range(rngAddress).Cells
        If useValue2 Then
            aCell.Offset(0, 1).Value2 = aCell.Value2 + aCell.Offset(-1, 1).Value2
        Else
            aCell.Offset(0, 1).Value = aCell.Value + aCell.Offset(-1, 1).Value
        End If
    
    Next aCell
    
    Dim Answer As String
     If useValue2 Then Answer = " using Value2"
    
    .Cells(Rows.Count, ResultsColumn).End(xlUp).Offset(1, 0) = DateDiff("S", beginTime, Now) & _
                " seconds. For " & .Range(rngAddress).Cells.Count & " cells, at " & Now & Answer
    
    
    End With
    
    
    End Sub
    

提交回复
热议问题