Full Version: Syntax To Sum A Row In Vba; Display In Msg Box
UtterAccess Discussion Forums > Microsoft® Office > Microsoft Excel
corrieann
Here is my code:
CODE
Option Explicit

Dim DiffCount As Long

'Calcuate the DiffCount Variable
    DiffCount = .Sum(Range(B1:IV1))

'Dispay Message Box with Confirmation Message
    MsgBox DiffCount & " cells contain different formulas!", vbInformation, _
        "Comparing Cell Values In " & ws1.Name & " With Those In " & ws2.Name
End Sub


This is the line that won't work:
CODE
    DiffCount = .Sum(Range(B1:IV1))


How can I tell VB to add row one (not including Cell A1 because that is a row header) and then use that value as the variable DiffCount to be used in my message box?


DanielPineault
You have to do something like

CODE
DiffCount = Application.WorksheetFunction.Sum(Range("B1:IV1"))
ipisors
just to expand/explain a little:

The syntax you were using (a dot without anything directly in front of it, like .something) is reserved for use inside a With statement, and the thing that comes after the dot is a method of the With-thing.

Example

Dim ws as Worksheet
Set ws = thisworkbook.Worksheets("Sheet1")

With ws
.Range("A1").Value="something"
End With

So try this, remember to set everything fully qualified:

Dim DiffCount As Long
Dim ws as Worksheet
Set ws = ThisWorkbook.Worksheets("SheetNameHere")
'Calcuate the DiffCount Variable
DiffCount = Application.WorksheetFunction.Sum(ws.Range(B1:IV1))

'Dispay Message Box with Confirmation Message
MsgBox DiffCount & " cells contain different formulas!", vbInformation, _
"Comparing Cell Values In " & ws1.Name & " With Those In " & ws2.Name
End Sub

That's all assuming, of course, that what you want to do is Sum the numerical values in B1:IV1
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.