Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Select Range

I want to select a cell that contain a specific word in it: I have
written as follow:
Sub Test()

Cells(2, 4) = 0
For i = 1 To 1000

If Cells(i, 1).Contain "SAFEWAY" Then
Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If

Next i

End Sub

But it does't work
could you please make it correct for me.
Thanks
Ali
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Select Range

This should do it:

Sub Test()

Cells(2, 4) = 0
For i = 1 To 1000
If InStr(1, Cells(i, 1), "SAFEWAY", vbTextCompare) Then
Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If
Next i
End Sub

Regards,
Per

On 16 Jun., 02:27, Ali wrote:
I want to select a cell that contain a specific word in it: I have
written as follow:
Sub Test()

Cells(2, 4) = 0
For i = 1 To 1000

If Cells(i, 1).Contain "SAFEWAY" Then
Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If

Next i

End Sub

*But it does't work
could you please make it correct for me.
Thanks
Ali


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Select Range

Per Jessen explained :
This should do it:

Sub Test()

Cells(2, 4) = 0
For i = 1 To 1000
If InStr(1, Cells(i, 1), "SAFEWAY", vbTextCompare) Then
Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If
Next i
End Sub

Regards,
Per


Another way:

Sub Test()
Dim i As Integer
Cells(2, 4) = 0
For i = 1 To 1000
If (InStr(Cells(i, 1), "SAFEWAY") 0) Then _
Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
Next i
End Sub

regards,

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 168
Default Select Range

On Jun 15, 11:54*pm, GS wrote:
Per Jessen explained :

This should do it:


Sub Test()


Cells(2, 4) = 0
For i = 1 To 1000
If InStr(1, Cells(i, 1), "SAFEWAY", vbTextCompare) Then
* * Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If
Next i
End Sub


Regards,
Per


Another way:

* Sub Test()
* * Dim i As Integer
* * Cells(2, 4) = 0
* * For i = 1 To 1000
* * * If (InStr(Cells(i, 1), "SAFEWAY") 0) Then _
* * * * *Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
* * Next i
* End Sub

regards,

--
Garry

Free usenet access athttp://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Since you said select, this assumes you have only ONE to find and
select
Sub GotoSafewayinColA_SAS()
Columns("a").Find(What:="safeway", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False).Select
End Sub
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 168
Default Select Range

On Jun 16, 7:24*am, Don Guillett Excel MVP
wrote:
On Jun 15, 11:54*pm, GS wrote:





Per Jessen explained :


This should do it:


Sub Test()


Cells(2, 4) = 0
For i = 1 To 1000
If InStr(1, Cells(i, 1), "SAFEWAY", vbTextCompare) Then
* * Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If
Next i
End Sub


Regards,
Per


Another way:


* Sub Test()
* * Dim i As Integer
* * Cells(2, 4) = 0
* * For i = 1 To 1000
* * * If (InStr(Cells(i, 1), "SAFEWAY") 0) Then _
* * * * *Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
* * Next i
* End Sub


regards,


--
Garry


Free usenet access athttp://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Since you said select, this assumes you have only ONE to find and
select
Sub GotoSafewayinColA_SAS()
*Columns("a").Find(What:="safeway", LookIn:=xlValues, _
*LookAt:=xlWhole, SearchOrder:=xlByRows, _
*SearchDirection:=xlNext, MatchCase:=False).Select
End Sub- Hide quoted text -

- Show quoted text -


Quicker than a loop. This finds all in col D and gives your cell(2,4)
total
Sub findAllSafeways()
Set c = Columns("d").Find(what:="safeway", _
LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
FirstAddr = c.Address
Do
Cells(2, 4) = Cells(2, 4) - c.Offset(, -2)
Set c = Columns("A").FindNext(after:=c)
Loop While Not c Is Nothing And c.Address < FirstAddr
End If
End Sub


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 168
Default Select Range

On Jun 16, 7:37*am, Don Guillett Excel MVP
wrote:
On Jun 16, 7:24*am, Don Guillett Excel MVP
wrote:





On Jun 15, 11:54*pm, GS wrote:


Per Jessen explained :


This should do it:


Sub Test()


Cells(2, 4) = 0
For i = 1 To 1000
If InStr(1, Cells(i, 1), "SAFEWAY", vbTextCompare) Then
* * Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If
Next i
End Sub


Regards,
Per


Another way:


* Sub Test()
* * Dim i As Integer
* * Cells(2, 4) = 0
* * For i = 1 To 1000
* * * If (InStr(Cells(i, 1), "SAFEWAY") 0) Then _
* * * * *Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
* * Next i
* End Sub


regards,


--
Garry


Free usenet access athttp://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Since you said select, this assumes you have only ONE to find and
select
Sub GotoSafewayinColA_SAS()
*Columns("a").Find(What:="safeway", LookIn:=xlValues, _
*LookAt:=xlWhole, SearchOrder:=xlByRows, _
*SearchDirection:=xlNext, MatchCase:=False).Select
End Sub- Hide quoted text -


- Show quoted text -


Quicker than a loop. This finds all in col D and gives your cell(2,4)
total
Sub findAllSafeways()
Set c = Columns("d").Find(what:="safeway", _
LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
FirstAddr = c.Address
Do
Cells(2, 4) = Cells(2, 4) - c.Offset(, -2)
Set c = Columns("A").FindNext(after:=c)
Loop While Not c Is Nothing And c.Address < FirstAddr
End If
End Sub- Hide quoted text -

- Show quoted text -

You don't say if "safeway" is the ONLY word in the cell. If NOT,
change =xlwhole to xlpart
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Select Range

Don Guillett Excel MVP submitted this idea :
On Jun 15, 11:54*pm, GS wrote:
Per Jessen explained :

This should do it:


Sub Test()


Cells(2, 4) = 0
For i = 1 To 1000
If InStr(1, Cells(i, 1), "SAFEWAY", vbTextCompare) Then
* * Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If
Next i
End Sub


Regards,
Per


Another way:

* Sub Test()
* * Dim i As Integer
* * Cells(2, 4) = 0
* * For i = 1 To 1000
* * * If (InStr(Cells(i, 1), "SAFEWAY") 0) Then _
* * * * *Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
* * Next i
* End Sub

regards,

--
Garry

Free usenet access athttp://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Since you said select, this assumes you have only ONE to find and
select
Sub GotoSafewayinColA_SAS()
Columns("a").Find(What:="safeway", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False).Select
End Sub


Well, I think the OP uses the word "select" rather loosely as his
sample code clearly indicates that the intention is to 'find' the text
within a range of cells. Given what the code sample implies is being
done as a result of finding that text, I don't see any point in
"Selecting" anything.

One good point you've demonstrated that has not been address is "case".
To that I offer this revised version of my previous example:

* Sub Test()
* * Dim i As Integer
* * Cells(2, 4) = 0
* * For i = 1 To 1000
* * * If (InStr(UCase$(Cells(i, 1)), "SAFEWAY") 0) Then _
* * * * *Cells(2, 4) = (Cells(2, 4) - Cells(i, 2))
* * Next i
* End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default Select Range

hello,

another way without loop:
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''
Sub Test()
Dim VisibleCells As Range
Application.ScreenUpdating = False
With ActiveSheet
If .AutoFilterMode Then .Cells.AutoFilter
.Range("A1:A1000").AutoFilter Field:=1, Criteria1:="=*SAFEWAY*"
If InStr(1, .Cells(1, 1), "SAFEWAY", vbTextCompare) Then .Cells(2, 4)
= .Cells(2, 4) - Cells(1, 2)
Set VisibleCells = .Range("B2:B1000").SpecialCells(xlCellTypeVisible)
.Cells(2, 4) = .Cells(2, 4) -
Application.WorksheetFunction.Subtotal(9, VisibleCells)
If .AutoFilterMode Then .Cells.AutoFilter
End With
Application.ScreenUpdating = False
End Sub





"Ali" a écrit dans le message de groupe de
discussion :
...
I want to select a cell that contain a specific word in it: I have
written as follow:
Sub Test()

Cells(2, 4) = 0
For i = 1 To 1000

If Cells(i, 1).Contain "SAFEWAY" Then
Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If

Next i

End Sub

But it does't work
could you please make it correct for me.
Thanks
Ali


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default Select Range

Another code (shorter):
'''''''''''''''''''''''''''''''''''''''''''''''''' ''
Sub Macro2()
Dim xFilter As Range, xSum As Range
With ActiveSheet
Set xFilter = .Range("A1:A1000")
Set xSum = xFilter.Offset(0, 1)
.Cells(2, 4) = .Cells(2, 4) + _
Application.SumIf(xFilter, "*SAFEWAY*", xSum)
End With
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' ''




"Ali" a écrit dans le message de groupe de
discussion :
...
I want to select a cell that contain a specific word in it: I have
written as follow:
Sub Test()

Cells(2, 4) = 0
For i = 1 To 1000

If Cells(i, 1).Contain "SAFEWAY" Then
Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If

Next i

End Sub

But it does't work
could you please make it correct for me.
Thanks
Ali


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default Select Range

Sorry, replace:
.Cells(2, 4) = .Cells(2, 4) + _
Application.SumIf(xFilter, "*SAFEWAY*", xSum)

with
.Cells(2, 4) = .Cells(2, 4) - _
Application.SumIf(xFilter, "*SAFEWAY*", xSum)




"Charabeuh" a écrit dans le message de groupe de
discussion : ...
Another code (shorter):
'''''''''''''''''''''''''''''''''''''''''''''''''' ''
Sub Macro2()
Dim xFilter As Range, xSum As Range
With ActiveSheet
Set xFilter = .Range("A1:A1000")
Set xSum = xFilter.Offset(0, 1)
.Cells(2, 4) = .Cells(2, 4) + _
Application.SumIf(xFilter, "*SAFEWAY*", xSum)
End With
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' ''




"Ali" a écrit dans le message de groupe de
discussion :
...
I want to select a cell that contain a specific word in it: I have
written as follow:
Sub Test()

Cells(2, 4) = 0
For i = 1 To 1000

If Cells(i, 1).Contain "SAFEWAY" Then
Cells(2, 4) = Cells(2, 4) - Cells(i, 2)
End If

Next i

End Sub

But it does't work
could you please make it correct for me.
Thanks
Ali


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can change range to select active rows instead of :=Range("S10 ldiaz Excel Discussion (Misc queries) 7 August 29th 08 03:52 PM
macro to select range from active cell range name string aelbob Excel Programming 2 July 14th 08 09:19 PM
When entering data into a range of cells, select the entire range. Q Excel Discussion (Misc queries) 0 September 26th 07 04:36 AM
Compare a selected Range with a Named range and select cells that do not exist PCLIVE Excel Programming 1 October 18th 05 07:09 PM
Select Sheet then Select Range Gee[_2_] Excel Programming 3 May 27th 04 10:10 PM


All times are GMT +1. The time now is 03:37 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"