AutoSum in a Macro with conditions, Question from a newbie

wazzulu1

Board Regular
Joined
Oct 4, 2006
Messages
164
Hi;

I'm trying to create a macro to automatically sum columns G throughR, based on a condition of a value in column C.

If C = 'Total for Customer' (text statement)

Then AutoSum Columns G through R


I am linking cells in column C from another tab in the same workbook, so I have to have the macro pick up the values I'm calling, not the formula in Column C.

How do I modify this macro below to possible use for this purpose?

Sub testautosum()
'
' testautosum Macro
'
Range("C2").Select
Cells.Find(What:="Total for Customer", After:=ActiveCell, LookIn:= _
xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext _
, MatchCase:=False, SearchFormat:=False).Activate
Range("G15").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-3]C:R[-1]C)"
Range("G15").Select
Selection.AutoFill Destination:=Range("G15:R15"), Type:=xlFillDefault
Range("G15:R15").Select
End Sub
 
Re: AutoSum in a Macro with conditions, Question from a newb

OK
How about?
Code:
Sub testautosum()
    Dim r As Range, ff As string
    Set r = Columns("c").Find("Total for Customer")
    If Not r Is Nothing Then
       ff = r.Address
       Do
          r.Offset(,4).Resize(,12).formulaR1C1 = "=Sum(r[-3]c:r[-1]c)"
          Set r = Columns("c").FindNext(r)
       Loop Until ff = r.Address
    End If
End Sub
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Re: AutoSum in a Macro with conditions, Question from a newb

Or this?
Code:
Sub testautosum()
    Dim r As Range, ff As string
    Set r = Columns("c").Find("Total")
    If Not r Is Nothing Then
       ff = r.Address
       Do
          r.Offset(,4).Resize(,12).formulaR1C1 = "=Sum(r[-3]c:r[-1]c)"
          Set r = Columns("c").FindNext(r)
       Loop Until ff = r.Address
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,076
Messages
6,128,670
Members
449,463
Latest member
Jojomen56

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top