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

I'm searching a column for matches (FindThis) and writing to cells on
the row (WriteReason) on success. The macro below works fine, but my
number of matching terms is increasing and I find myself just copying
and pasting the code over and over again. Is there a better way to
modularize this just plug in values without creating "FindThisA,
FindThisB", etc?

----

Sub Exclusions()

WriteManual = "Manual"

Const FindThisA = "MatchA"
Const WriteReasonA = "Failure Code A"
Const FindThisB = "MatchB"
Const WriteReasonB = "Failure Code B"

Dim A As Range, retA
ActiveCell.EntireColumn.Select
For Each A In Selection
retA = InStr(1, A, FindThisA, vbTextCompare)
If (Not IsNull(retA)) And (retA 0) Then
A.Offset(0, -17).Value = WriteReasonA
A.Offset(0, -20).Value = WriteManual
End If
Next A

Dim B As Range, retB
ActiveCell.EntireColumn.Select
For Each B In Selection
retB = InStr(1, B, FindThisB, vbTextCompare)
If (Not IsNull(retB)) And (retB 0) Then
B.Offset(0, -17).Value = WriteReasonB
B.Offset(0, -20).Value = WriteManual
End If
Next B

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Reusing code

This should help out a little:

Sub Exclusions()
Call FindExclusions("MatchA", "Failure Code A", "Manual")
Call FindExclusions("MatchB", "Failure Code B", "Manual")
End Sub

Function FindExclusions(findStr As String, writeReason As String,
writeManual As String)
Dim A As Range, retA
ActiveCell.EntireColumn.Select
For Each A In Selection
retA = InStr(1, A, findStr, vbTextCompare)
If (Not IsNull(retA)) And (retA 0) Then
A.Offset(0, -17).Value = writeReason
A.Offset(0, -20).Value = writeManual
End If
Next A
End Function

If you have a very large number of terms to search for, you could also put
all of the terms into a sheet and have the macro loop through each term.

Hope this helps!
Andy

"Blee" wrote:

I'm searching a column for matches (FindThis) and writing to cells on
the row (WriteReason) on success. The macro below works fine, but my
number of matching terms is increasing and I find myself just copying
and pasting the code over and over again. Is there a better way to
modularize this just plug in values without creating "FindThisA,
FindThisB", etc?

----

Sub Exclusions()

WriteManual = "Manual"

Const FindThisA = "MatchA"
Const WriteReasonA = "Failure Code A"
Const FindThisB = "MatchB"
Const WriteReasonB = "Failure Code B"

Dim A As Range, retA
ActiveCell.EntireColumn.Select
For Each A In Selection
retA = InStr(1, A, FindThisA, vbTextCompare)
If (Not IsNull(retA)) And (retA 0) Then
A.Offset(0, -17).Value = WriteReasonA
A.Offset(0, -20).Value = WriteManual
End If
Next A

Dim B As Range, retB
ActiveCell.EntireColumn.Select
For Each B In Selection
retB = InStr(1, B, FindThisB, vbTextCompare)
If (Not IsNull(retB)) And (retB 0) Then
B.Offset(0, -17).Value = WriteReasonB
B.Offset(0, -20).Value = WriteManual
End If
Next B

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Reusing code

Thanks Andy; that worked very well.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Reusing code

Hi Blee,
Try this instead.
As you can see there is nothing actually hard coded, its is all done
through the constants.
You can add as many failure codes as you want.
Also, this wont loop through all the cells in the colum.

Kind regards
Bernie Russell

----------------------------------------------------------------------------------
Public Sub Exclusions2()



Const ACTION As String = "Manual"
Const SEARCHPREFIX As String = "Match"
Const REASONPREFIX As String = "Failure Code "
Const OFFSETMANUAL As Integer = -17 'Or whatever offset you want.
Const OFFSETREASON As Integer = -20

Dim FailureCodes() As Variant
FailureCodes = Array("A", "B", "C", "D", "E", "F") 'Put in what ever
you want here

Dim fc As String
Dim sCVal As String

Dim i As Integer
Dim rRangeToSearch As Range
'Set rRangeToSearch = ActiveCell.EntireColumn 'Or set it like this
worksheets(1).Range("A:A")
With ActiveCell.EntireColumn
' This sets the range to be searched to the first cell in the column
to the last used cell in the column.
' So it wont have to go through 65536 cells. ie This is quicker
Set rRangeToSearch =
Range(.Cells(1), .Cells(.Cells.Count).End(xlUp))
Debug.Print rRangeToSearch.Address
End With

On Error GoTo Err_Exclusions2
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
Dim c As Range
For Each c In rRangeToSearch.Cells
Debug.Print c.Address & " " & c.Value
With c
If Len(.Value) 0 Then
sCVal = UCase(.Value)
For i = LBound(FailureCodes) To UBound(FailureCodes)
fc = UCase(FailureCodes(i))
If InStr(1, sCVal, UCase(SEARCHPREFIX) & fc) 0 And
Len(fc) < 0 Then
.Offset(0, OFFSETREASON).Value = CStr(REASONPREFIX & fc)
.Offset(0, OFFSETMANUAL).Value = ACTION
End If
Next i
End If
End With 'c
Next c

ExitSub:
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
Set c = Nothing
Set rRangeToSearch = Nothing
Exit Sub

Err_Exclusions2:
MsgBox "An error occured in when running the Exclusions2 sub
procedure"
GoTo ExitSub
End Sub




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
reusing code MJKelly Excel Programming 3 August 28th 08 09:55 PM
Reusing Form Code DMc2004 Excel Programming 4 November 16th 07 05:33 PM
Reusing formula Tony29 Excel Discussion (Misc queries) 7 September 7th 06 03:34 AM
Reusing grouping of non-adjacent cells [email protected] Excel Discussion (Misc queries) 2 May 28th 06 01:21 PM
reusing a recordset for a pivot-table? Bart op de grote markt Excel Programming 2 March 7th 06 03:34 PM


All times are GMT +1. The time now is 02:12 PM.

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"