Contains Text

CBorders

New Member
Joined
Sep 23, 2011
Messages
16
I'm trying to write a macro that executes when the cell in column A contains the word Total. Looking online, I keep seeing examples that make me think Find should work, but every time I try to run it I am told that the Sub or Function is undefined.

My current code is:

Sub AddTotals()
'
' AddTotals Macro
' Add the Totals for each location
'

'

Dim Location As Currency


Range("A1").Select
Do While (IsEmpty(ActiveCell) = False Or IsEmpty(ActiveCell.Offset(1,0)) = False)
If IsNumber(Find("Total", A1)) Then
ActiveCell.Offset(0, 7).Value = Location And Location = 0
Else
Location = Location + ActiveCell.Offset(0, 7).Value
End If
ActiveCell.Offset(1, 0).Select

Loop

End Sub

Any assistance for why Find isn't working for me would be appreciated.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I am guessing, but are you trying to use the worksheet function FIND? (vs the vba method)
 
Upvote 0
If IsNumber(Find("Total", A1)) Then

Any assistance for why Find isn't working for me would be appreciated.
Find, at least the way you are trying to use it, is a worksheet function, not a VB function. The VB equivalent of what you are trying to do would use the InStr (in string) function....

If InStr(Range("A1").Value, "Total") > 0 Then

However, the way your code is structured, it won't work either way. You reference to A1 is absolute and doesn't change when you later on in your code select a cell offset by 1 from the last cell referenced in the loop. As it turns out, you won't need to fix anything as the range object has a Find method that can be used to iterate through all of the cells in Column A with the word "Total" in them. Unfortunately, I'm not crystal clear on what your code is supposed to be doing. Can you provide a written description of what your code is attempting to do?
 
Upvote 0
I am guessing, but are you trying to use the worksheet function FIND? (vs the vba method)

I honestly have no idea. I just started learning macro related stuff about a week ago. I'm trying to automate as much as I can of some reports I run and have gotten quite a bit, but I'm stuck on this step and none of the examples I've found online have worked or been helpful.

If you know what I need to put instead to trigger an if statement whenever the cell in column A contains the word Total, please let me know.
 
Upvote 0
Find, at least the way you are trying to use it, is a worksheet function, not a VB function. The VB equivalent of what you are trying to do would use the InStr (in string) function....

If InStr(Range("A1").Value, "Total") > 0 Then

However, the way your code is structured, it won't work either way. You reference to A1 is absolute and doesn't change when you later on in your code select a cell offset by 1 from the last cell referenced in the loop. As it turns out, you won't need to fix anything as the range object has a Find method that can be used to iterate through all of the cells in Column A with the word "Total" in them. Unfortunately, I'm not crystal clear on what your code is supposed to be doing. Can you provide a written description of what your code is attempting to do?

Tried it with that and it doesn't have an error message now, but it never seems to trigger the condition. I'll try to give an example of what I want it to do.

So first I had to convert a list of times at locations to numbers and round up.
Original form was basically:

Location: 1h 08m
Location: 2h 05m
Total for Location: 3h 13m

Changed to:
Location Stop One: 1.25
Location Stop Two: 2.25
Total for Location: 3.25

The problem is I actually need it to add the two rounded up numbers and put it where the total is so that it reads:
Location Stop One: 1.25
Location Stop Two: 2.25
Total for Location: 3.5

So I'm trying to make a variable that takes the first stop at a location, adds it to the next stop, and the next stop, etc. until it reaches the line that says Total for (Location). At that point I want it to replace the current number at Total for Location, reset the variable, and move on to the next location until all locations have been gone through.
 
Upvote 0
Okay. As Rick pointed out, I think a short/clear description of what we are wanting to do would be the best.

As we are starting in A1, it appears we want to do loop until we hit a blank cell. If that is correct, is there anything (data-wise and hopefully not) below these cells in Col A?

And what are we doing here: '...= Location And Location = 0'
 
Upvote 0
Yes, I also have a question. Are you simply trying to find (one instance of) "Total" in column A? - Or will there be multiple instances found and you want to perform your code on each instance?
I suspect you're wanting to find multiple instances, so perhaps something like this?:
Code:
Sub AddTotalsDemo()
Dim Location As Currency, LstRw As Long, Rng As Range
LstRw = Cells(Rows.Count, "A").End(xlUp).Row
For Each Rng In Range("A1:A" & LstRw)
  If Rng.Value = "Total" Then
    Rng(, 8).Value = Location
    Location = 0
  Else
    Location = Location + Rng(, 8).Value
  End If
Next Rng
End Sub

Is this even close to what you're really trying to do?
 
Upvote 0
So (after reading your latest post) am I right in thinking you want to go through column H and sum up all the values in the rows between the rows in column A that contain "Total", put that total in column H - same row labeled "Total" in column A, reset the counter to 0 and start summing from the next row again?
 
Upvote 0
So (after reading your latest post) am I right in thinking you want to go through column H and sum up all the values in the rows between the rows in column A that contain "Total", put that total in column H - same row labeled "Total" in column A, reset the counter to 0 and start summing from the next row again?


Yes, that sounds right.
 
Upvote 0
Okay. As Rick pointed out, I think a short/clear description of what we are wanting to do would be the best.

As we are starting in A1, it appears we want to do loop until we hit a blank cell. If that is correct, is there anything (data-wise and hopefully not) below these cells in Col A?

No, there's nothing below the data in column A. After replacing my code with Rick's, the Loop runs and stops at the end where it's supposed to. But it's not stopping on the way to replace column H's number when the cell in column A contains total and it's not resetting the counter. So it's basically just summing all the numbers in column H and putting them at the end.

And what are we doing here: '...= Location And Location = 0'

My amateurish attempt at trying to get the cell in column H to equal the value stored in the counter and then reset the counter to 0. It doesn't appear to ever get to this line of the code though.
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,916
Members
452,949
Latest member
beartooth91

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