Excel单元格值作为字符串不会存储为字符串

时间:2013-05-29 17:42:30

标签: excel vba excel-vba

我无法在此处获取此代码以获取单元格内容并将其存储为字符串。我得到一个双: 54.6666666667代替N03:DM:(示例单元格内容)。

如果我使用Cstr(Sheet1.Cells(i, 5).Value),我仍会得到相同的结果。

任何帮助都将不胜感激。

Option Explicit
Private Sub GetAddress()

Dim varAdd As String
Dim i As Integer

    For i = 2 To 327
        If varTag = Sheet1.Cells(i, 2).Value Then
           varAdd = Sheet1.Cells(i, 5).Value
           varAdd = Left(varAdd, 7)
           Sheet3.Cells(incR, 2).Value = varAdd
           Exit For
        End If   
    Next i

End Sub

图纸截图enter image description here

1 个答案:

答案 0 :(得分:31)

使用Range("A1").Text代替.Value

发表评论编辑:
为什么呢?
因为Range对象的.Text属性返回电子表格中明显可见的内容,所以如果单元格显示为i100l:25he*_92,那么< - Text将完全返回单元格中的内容,包括任何格式。
.Value.Value2属性会返回存储在引擎盖下的单元格中的内容,不包括格式设置。特别是.Value2的日期类型,它将返回十进制表示。

如果你想深入了解其含义和表现,我发现this article似乎是一个很好的指南

其他编辑
在这里你去@Santosh
输入(手动 DEFAULT (col A)到其他列的值 不要格式化A列 将列B格式化为文本
将列C格式化为日期[dd / mm / yyyy]
将列D格式化为百分比
Dont Format column A, Format B as TEXT, C as Date, D as Percentage
现在,
将此代码粘贴到模块中

Sub main()

    Dim ws As Worksheet, i&, j&
    Set ws = Sheets(1)
    For i = 3 To 7
        For j = 1 To 4
            Debug.Print _
                    "row " & i & vbTab & vbTab & _
                    Cells(i, j).Text & vbTab & _
                    Cells(i, j).Value & vbTab & _
                    Cells(i, j).Value2
        Next j
    Next i
End Sub

Analyse 输出!它真的很容易,没有更多我可以帮助:)

            .TEXT              .VALUE             .VALUE2
row 3       hello             hello               hello
row 3       hello             hello               hello
row 3       hello             hello               hello
row 3       hello             hello               hello
row 4       1                 1                   1
row 4       1                 1                   1
row 4       01/01/1900        31/12/1899          1
row 4       1.00%             0.01                0.01
row 5       helo1$$           helo1$$             helo1$$
row 5       helo1$$           helo1$$             helo1$$
row 5       helo1$$           helo1$$             helo1$$
row 5       helo1$$           helo1$$             helo1$$
row 6       63                63                  63
row 6       =7*9              =7*9                =7*9
row 6       03/03/1900        03/03/1900          63
row 6       6300.00%          63                  63
row 7       29/05/2013        29/05/2013          41423
row 7       29/05/2013        29/05/2013          29/05/2013
row 7       29/05/2013        29/05/2013          41423
row 7       29/05/2013%       29/05/2013%         29/05/2013%
相关问题