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.
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.
If ws.Cells(3 + j, 3) .value = "SW" Then
Range.Value
returns a Variant
whose subtype depends on the content of the cell.
Given a #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".
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.
Given an empty cell that has no formula, no value, no content whatsoever, it returns a Variant/Empty
; the IsEmpty
function can be used to validate this variant subtype.
Given a Date
value, it returns a Variant/Date
. Given any numeric value, it returns a Variant/Double
. Given a TRUE
or FALSE
value, it returns a Variant/Boolean
.
And given a String
value, it does return a Variant/String
.
Note that the default member of the Range
class is a hidden [_Default]
property with two optional parameters:
When no parameters are provided, an explicit call to .Value
is equivalent. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.