Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
i found this code and am trying to adjust for my needs but it keeps stopping
at the indicated line. Sub test() Call AddName("Pear") End Sub Public Sub AddName(ByVal Fruit As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngFound As Range Dim rngFoundAll As Range Dim strFirstAddress As String Set wks = ActiveSheet Set rngToSearch = wks.Columns("A") stops here Set rngFound = rngToSearch.Find(What:=Fruit, _ LookIn:=xlConstants, _ LookAt:=xlPart) If Not rngFound Is Nothing Then Set rngFoundAll = rngFound strFirstAddress = rngFound.Address Do Set rngFoundAll = Union(rngFound, rngFoundAll) Set rngFound = rngToSearch.FindNext(rngFound) Loop Until rngFound.Address = strFirstAddress ThisWorkbook.Names.Add Fruit, rngFoundAll.Address End If End Sub |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Jhyatt,
I think that the only Lookin parameters are xlValues, xlFormulas and xlComments. Just as an added extra it is recommended that you set all the arguments/parameters for find because they are saved from the previous find even if it was done in the interactive mode. Regards, OssieMac "jhyatt" wrote: i found this code and am trying to adjust for my needs but it keeps stopping at the indicated line. Sub test() Call AddName("Pear") End Sub Public Sub AddName(ByVal Fruit As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngFound As Range Dim rngFoundAll As Range Dim strFirstAddress As String Set wks = ActiveSheet Set rngToSearch = wks.Columns("A") stops here Set rngFound = rngToSearch.Find(What:=Fruit, _ LookIn:=xlConstants, _ LookAt:=xlPart) If Not rngFound Is Nothing Then Set rngFoundAll = rngFound strFirstAddress = rngFound.Address Do Set rngFoundAll = Union(rngFound, rngFoundAll) Set rngFound = rngToSearch.FindNext(rngFound) Loop Until rngFound.Address = strFirstAddress ThisWorkbook.Names.Add Fruit, rngFoundAll.Address End If End Sub |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi again Jhyatt,
I decided to actually test your code and if you replace your find with the following it works:- Set rngFound = rngToSearch.Find(What:=Fruit, _ LookIn:=xlFormulas, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) Regards, OssieMac "OssieMac" wrote: Hi Jhyatt, I think that the only Lookin parameters are xlValues, xlFormulas and xlComments. Just as an added extra it is recommended that you set all the arguments/parameters for find because they are saved from the previous find even if it was done in the interactive mode. Regards, OssieMac "jhyatt" wrote: i found this code and am trying to adjust for my needs but it keeps stopping at the indicated line. Sub test() Call AddName("Pear") End Sub Public Sub AddName(ByVal Fruit As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngFound As Range Dim rngFoundAll As Range Dim strFirstAddress As String Set wks = ActiveSheet Set rngToSearch = wks.Columns("A") stops here Set rngFound = rngToSearch.Find(What:=Fruit, _ LookIn:=xlConstants, _ LookAt:=xlPart) If Not rngFound Is Nothing Then Set rngFoundAll = rngFound strFirstAddress = rngFound.Address Do Set rngFoundAll = Union(rngFound, rngFoundAll) Set rngFound = rngToSearch.FindNext(rngFound) Loop Until rngFound.Address = strFirstAddress ThisWorkbook.Names.Add Fruit, rngFoundAll.Address End If End Sub |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sorry it took so long to get back to you i changed the code as you suggested
and it works great. is there a way to have it look in multiple work sheets. "OssieMac" wrote: Hi again Jhyatt, I decided to actually test your code and if you replace your find with the following it works:- Set rngFound = rngToSearch.Find(What:=Fruit, _ LookIn:=xlFormulas, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) Regards, OssieMac "OssieMac" wrote: Hi Jhyatt, I think that the only Lookin parameters are xlValues, xlFormulas and xlComments. Just as an added extra it is recommended that you set all the arguments/parameters for find because they are saved from the previous find even if it was done in the interactive mode. Regards, OssieMac "jhyatt" wrote: i found this code and am trying to adjust for my needs but it keeps stopping at the indicated line. Sub test() Call AddName("Pear") End Sub Public Sub AddName(ByVal Fruit As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngFound As Range Dim rngFoundAll As Range Dim strFirstAddress As String Set wks = ActiveSheet Set rngToSearch = wks.Columns("A") stops here Set rngFound = rngToSearch.Find(What:=Fruit, _ LookIn:=xlConstants, _ LookAt:=xlPart) If Not rngFound Is Nothing Then Set rngFoundAll = rngFound strFirstAddress = rngFound.Address Do Set rngFoundAll = Union(rngFound, rngFoundAll) Set rngFound = rngToSearch.FindNext(rngFound) Loop Until rngFound.Address = strFirstAddress ThisWorkbook.Names.Add Fruit, rngFoundAll.Address End If End Sub |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi again Jhyatt,
I don't think that you can set a union on multiple sheets or at least I don't know how. What I have done is modify the code to step through all the worksheets and set the interior color of the found cells to Yellow just to give you an example of stepping through the worksheets. Further to what JLG Whiz said, I am intrigued as to why you want to set a union of the found cells or is this just a training example? Sub test() Call AddName("Pear") End Sub Public Sub AddName(ByVal Fruit As String) Dim ws As Worksheet Dim wks As Worksheet Dim rngToSearch As Range Dim rngFound As Range Dim strFirstAddress As String 'Repeat for each worksheet in workbook For Each wks In Worksheets Set rngToSearch = wks.Columns("A") Set rngFound = rngToSearch.Find(What:=Fruit, _ LookIn:=xlFormulas, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) If Not rngFound Is Nothing Then strFirstAddress = rngFound.Address Do 'Set the interior color of found cell to Yellow rngFound.Interior.ColorIndex = 6 Set rngFound = rngToSearch.FindNext(rngFound) Loop Until rngFound.Address = strFirstAddress End If Next wks End Sub Regards, OssieMac |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
sorry Ossiemac had go home and sleep.
my goal is to find all records in the workbook with the word trade (trades of service)in them to put them on a separate sheet so we can track the trades in our business. I have been trying different codes to figure out a way to do this and this one seemed to have potential. "OssieMac" wrote: Hi again Jhyatt, I don't think that you can set a union on multiple sheets or at least I don't know how. What I have done is modify the code to step through all the worksheets and set the interior color of the found cells to Yellow just to give you an example of stepping through the worksheets. Further to what JLG Whiz said, I am intrigued as to why you want to set a union of the found cells or is this just a training example? Sub test() Call AddName("Pear") End Sub Public Sub AddName(ByVal Fruit As String) Dim ws As Worksheet Dim wks As Worksheet Dim rngToSearch As Range Dim rngFound As Range Dim strFirstAddress As String 'Repeat for each worksheet in workbook For Each wks In Worksheets Set rngToSearch = wks.Columns("A") Set rngFound = rngToSearch.Find(What:=Fruit, _ LookIn:=xlFormulas, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) If Not rngFound Is Nothing Then strFirstAddress = rngFound.Address Do 'Set the interior color of found cell to Yellow rngFound.Interior.ColorIndex = 6 Set rngFound = rngToSearch.FindNext(rngFound) Loop Until rngFound.Address = strFirstAddress End If Next wks End Sub Regards, OssieMac |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You should abandon this code and start over. It does nothing to nothing.
However, It is stopping at the Set....Find statement because it cannot find the value you define in the place you tell it to look...xlConstants. If you change that to xlValues, then it should complete macro. "jhyatt" wrote: i found this code and am trying to adjust for my needs but it keeps stopping at the indicated line. Sub test() Call AddName("Pear") End Sub Public Sub AddName(ByVal Fruit As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngFound As Range Dim rngFoundAll As Range Dim strFirstAddress As String Set wks = ActiveSheet Set rngToSearch = wks.Columns("A") stops here Set rngFound = rngToSearch.Find(What:=Fruit, _ LookIn:=xlConstants, _ LookAt:=xlPart) If Not rngFound Is Nothing Then Set rngFoundAll = rngFound strFirstAddress = rngFound.Address Do Set rngFoundAll = Union(rngFound, rngFoundAll) Set rngFound = rngToSearch.FindNext(rngFound) Loop Until rngFound.Address = strFirstAddress ThisWorkbook.Names.Add Fruit, rngFoundAll.Address End If End Sub |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Macro recorded... tabs & file names changed, macro hangs | Excel Worksheet Functions | |||
AutoRun Macro with a delay to give user the choice to cancel the macro | Excel Programming | |||
Macro not showing in Tools/Macro/Macros yet show up when I goto VBA editor | Excel Programming | |||
Need syntax for RUNning a Word macro with an argument, called from an Excel macro | Excel Programming | |||
Start Macro / Stop Macro / Restart Macro | Excel Programming |