简体   繁体   English

使用带有字符串的 IF 语句使用 .value

[英]Using an IF statement with a string using .value

Does the .value command return strings and if not how do you have an IF statement check if the cell(s) holds the correct string. .value 命令是否返回字符串,如果不是,您如何使用 IF 语句检查单元格是否包含正确的字符串。

I am working on a budget sheet where a certain column sets which department the budget and its breakdown should be forwarded to.我正在制作一个预算表,其中某个列设置了预算及其细目应该转发到哪个部门。

Sub calcMonthly()

Dim ws As Worksheet
Dim wssum As Worksheet

'set worksheets to copy values
Set ws = Sheets("Sheet 1")
Set wssum = Sheets("Sheet 2")


Dim i As Integer
Dim j As Integer
Dim k As Integer

Dim bumonth As Currency
Dim busum As Currency


'sort through Departments for % breakdown
For k = 0 To 18

    'sort through months
    For i = 0 To 11
    busum = 0
        'sort through each item
        For j = 0 To 350
            bumonth = 0
            bumonth = CCur(ws.Cells(3 + j, 37 + k).Value * ws.Cells(3 + j, 24 + i).Value)
            busum = busum + bumonth
        Next j
    'row C holds the string which details if the item if physical hardware or digital then uploads it to the cell
    If ws.Cells(3 + j, 3) = "SW" Then
        wssum.Cells(3 + k, 2 + i).Value = busum
    Else
        wssum.Cells(3 + k, 14 + i).Value = busum
    End If
    Next i
Next k


End Sub

.Value does return a string, but it doesn't look like you included it in the IF statement. .Value 确实返回一个字符串,但看起来您没有将它包含在 IF 语句中。

If ws.Cells(3 + j, 3) .value = "SW" Then如果 ws.Cells(3 + j, 3) .value = "SW" 然后

Range.Value returns a Variant whose subtype depends on the content of the cell. Range.Value返回一个Variant其子类型取决于单元格的内容。

Given a #N/A , #VALUE , #REF!给定#N/A#VALUE#REF! , or any other cell error value, it returns a Variant/Error that can't be coerced into anything other than a Variant - trying to compare it to a string or numeric value or expression will throw error 13 "type mismatch". ,或任何其他单元格错误值,它返回一个Variant/Error ,该Variant/Error不能被强制转换为Variant以外的任何东西 - 尝试将其与字符串或数值或表达式进行比较将抛出错误 13“类型不匹配”。

You can avoid this runtime error by evaluating whether the variant subtype is Error using the IsError function, ideally by capturing the cell's value into a local Variant variable first, so you don't have to access the cell twice.您可以通过使用IsError函数评估变体子类型是否为Error来避免此运行时错误,理想情况下,首先将单元格的值捕获到本地Variant变量中,这样您就不必两次访问该单元格。

Given an empty cell that has no formula, no value, no content whatsoever, it returns a Variant/Empty ;给定一个没有公式、没有值、没有任何内容的空单元格,它返回一个Variant/Empty the IsEmpty function can be used to validate this variant subtype. IsEmpty函数可用于验证此变体子类型。

Given a Date value, it returns a Variant/Date .给定一个Date值,它返回一个Variant/Date Given any numeric value, it returns a Variant/Double .给定任何数值,它返回一个Variant/Double Given a TRUE or FALSE value, it returns a Variant/Boolean .给定一个TRUEFALSE值,它返回一个Variant/Boolean

And given a String value, it does return a Variant/String .并且给定一个String值,它确实返回一个Variant/String


Note that the default member of the Range class is a hidden [_Default] property with two optional parameters:请注意, Range类的默认成员是一个隐藏的[_Default]属性,带有两个可选参数:

属性_Default([RowIndex], [ColumnIndex]),Excel.Range的默认成员

When no parameters are provided, an explicit call to .Value is equivalent.当没有提供参数时,对.Value的显式调用是等效的。 Explicit member calls should generally be preferred over implicit default member calls, and whether implicit or explicit, the calls should be consistent:显式成员调用通常应优先于隐式默认成员调用,并且无论是隐式还是显式,调用都应保持一致:

 If ws.Cells(3 + j, 3) = "SW" Then ' implicit: .Value wssum.Cells(3 + k, 2 + i).Value = busum Else wssum.Cells(3 + k, 14 + i).Value = busum End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM