Incomplete code! help please


Board Regular
Nov 24, 2008
Hello,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
I have two pieces of code that I need to talk to each other. Script 1 is complete and works as intended. Script 2 is a work in progress and includes notes that have been commented explaining what needs to occur. I don’t know how to pass defined variables from script to script and make the two work together. Any help would be greatly appreciated.<o:p></o:p>
Note: for additional information please see<o:p></o:p>
<o:p></o:p>Script 1
Sub Replacer()
     'Does a Find and Replace on whole words throughout the selected range. Uses a table of _
    Find And Replace strings taken from Sheet2 columns A And B _
    Uses regular expressions For search To make sure found strings are complete words _
    Uses arrays For speed For range To be searched And For source of Find/Replace strings. _
    Note: will wipe out all formulas In the selected range!
    Dim RgExp As Object
    Dim rg As Range
    Dim X As Variant, Y As Variant
    Dim i As Long, j As Long, k As Long, nColumns As Long, nFindReplace As Long, nRows As Long
    Dim FindReplacePrompt As String
    FindReplacePrompt = "I couldn't find the Find/Replace strings at Sheet2!A1:Bxx. Please select them now." & _
    "  No blanks allowed in first column!"
    If Selection.Cells.Count = 1 Then
        If Selection = "" Then
            MsgBox "Please select some cells to run the macro on, then try again"
            Exit Sub
            ReDim X(1 To 1, 1 To 1)
            X(1, 1) = Selection
        End If
        X = Selection.Value
    End If
     'Populate the array variable Y with Find/Replace strings. Default source is Sheet2, A1:Bxx
    On Error Resume Next
    Set rg = Worksheets("Standard Abbreviations").Range("A2")
    If rg Is Nothing Then
        Set rg = Application.InputBox(prompt:=FindReplacePrompt, Title:="Source of Find/Replace strings", Type:=8)
        If rg Is Nothing Then Exit Sub
        If rg.Cells(1, 1) = "" Then
            Set rg = Application.InputBox(prompt:=FindReplacePrompt, Title:="Source of Find/Replace strings", Type:=8)
            If rg Is Nothing Then Exit Sub
            Set rg = Range(rg, rg.End(xlDown).Offset(0, 1))
        End If
    End If
    On Error GoTo 0
    Y = rg.Value
    nFindReplace = UBound(Y)
    Set RgExp = CreateObject("VBScript.RegExp")
    With RgExp
        .Global = True
        .IgnoreCase = True  'True if search is case insensitive. False otherwise
    End With
    nRows = UBound(X)
    nColumns = UBound(X, 2)
    For i = 1 To nFindReplace
        RgExp.Pattern = "\b" & Y(i, 1) & "\b"
        For j = 1 To nRows
            For k = 1 To nColumns
                X(j, k) = RgExp.Replace(X(j, k), Y(i, 2))
            Next k
        Next j
    Next i
    Set RgExp = Nothing
    Selection.Value = X 'Replace cell values with the edited strings
End Sub
Script 2
Public Sub Array_Replace()
    Dim txt_in As String, txt_out As String
    Dim ary_ele As Variant
    Dim i As Long, j As Long, iLen As Long
   'Here is where I begin the second loop using the selection from Sub Replacer
    txt_in = "I need to have this equal to the selection in the range based off of selection used in Sub Replacer()"
        ary_ele = Split(txt_in, " ")
            For i = UBound(ary_ele) To LBound(ary_ele) Step -1
                'find and replace procedure goes here - find the ary_ele value in column "A" (Exact Match) on sheet3
                'and replace with corresponding in column "B" (or maybe i can call Sub Replacer() to do the find and
                'replace within the array.
                    txt_out = ""
                    For j = LBound(ary) To UBound(ary)
                        If (txt_out <> "") Then txt_out = txt_out & " "
                        txt_out = txt_out & ary(j)
                    Next j
                iLen = Len(txt_out)
                If iLen <= 48 Then Exit For
            Next i
    'Here i need to replace the Selection.Value from Sub Replacer() with txt_out.
    'Now i need to loop to next cell in the original selection used in Sub Replacer()
End Sub
Thank you,

Joseph Marro

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...