Consulting

Results 1 to 9 of 9

Thread: Solved: Convert to function

  1. #1

    Solved: Convert to function

    Hi,

    I have been suggested following program to replace values in Col G.
    Is this possible to convert this procedure into a function, so that I can use it for any Column (not only for Col G). This idea will help me convert remaining procedures.


    [vba]

    Public Sub ReplaceColG()
    Dim lLR As Long
    Dim r As Range
    lLR = Range("G" & Rows.Count).End(xlUp).Row
    For i = 2 To lLR
    If Range("G" & i).Value <> "" Then
    Set r = Columns("C:C").Find(What:=Range("G" & i).Value, _
    LookIn:=xlValues, LookAt:=xlWhole)
    If Not r Is Nothing Then
    Range("G" & i).Value = r.Offset(, -1).Value
    End If
    End If
    Next i
    End Sub

    [/vba]

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    A function returns a value. What value did you expect to return?

    [VBA]
    Sub Test_ReplaceCol()
    ReplaceCol "G"
    End Sub

    Public Sub ReplaceCol(col As String)
    Dim lLR As Long
    Dim r As Range
    lLR = Range(col & Rows.Count).End(xlUp).Row
    For i = 2 To lLR
    If Range(col & i).Value <> "" Then
    Set r = Columns("C:C").Find(What:=Range(col & i).Value, _
    LookIn:=xlValues, LookAt:=xlWhole)
    If Not r Is Nothing Then
    Range(col & i).Value = r.Offset(, -1).Value
    End If
    End If
    Next i
    End Sub[/VBA]

  3. #3
    Hi, you are right. Apologies, I could not write it well. I thank that you have given it a form of Sub... Excellent... I have a query for which I need your advice ...

    [vba]

    Public Sub ReplaceCol(col As String)
    [/vba]

    If I make above as Private, then can following access that Private Sub?

    [VBA]
    Sub Test_ReplaceCol()
    [/VBA]


    How can I make this sub to be "private"?

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by justdriving
    ...[vba]

    Public Sub ReplaceCol(col As String)
    [/vba]

    If I make above as Private, then can following access that Private Sub?

    [vba]
    Sub Test_ReplaceCol()
    [/vba]
    Call it from the same module. Or to (at least IMO) make it more unnecessarily complicated, use .Run.

    Quote Originally Posted by justdriving
    How can I make this sub to be "private"?
    PLease refer to vba Help. Look up Sub or Function Statement.

    Cat-killin' curiousity has the best of me it appears. Why would you want/need the sub Kenneth provided to be Private? It has a required parameter, its not as if its going to be accidently accessed.

  5. #5
    Thanks GTO, actually Alt+F8 can display Public Sub, which I wish to avoid. Apologies, I should have changed this in my question. Your help is appreciated.

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I am the one to apologize as I may have misunderstood. Are you wanting to make Sub Test_ReplaceCol() private? Or Public Sub ReplaceCol(col As String) Private?

  7. #7
    Quote Originally Posted by GTO
    Call it from the same module. Or to (at least IMO) make it more unnecessarily complicated, use .Run.

    I see that you answered above. Both Private Sub can be called, if they reside in same Module. I am not aware about RUN. Can you please help how it could be used?

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Look at Application.Run in VBA Help.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Quote Originally Posted by Kenneth Hobs
    A function returns a value. What value did you expect to return?

    [vba]
    Sub Test_ReplaceCol()
    ReplaceCol "G"
    End Sub

    Public Sub ReplaceCol(col As String)
    Dim lLR As Long
    Dim r As Range
    lLR = Range(col & Rows.Count).End(xlUp).Row
    For i = 2 To lLR
    If Range(col & i).Value <> "" Then
    Set r = Columns("C:C").Find(What:=Range(col & i).Value, _
    LookIn:=xlValues, LookAt:=xlWhole)
    If Not r Is Nothing Then
    Range(col & i).Value = r.Offset(, -1).Value
    End If
    End If
    Next i
    End Sub[/vba]
    I thank for your help.

    Is there any way, that I could modify above program like this: -

    [vba]
    Sub Test_ReplaceCol()

    ReplaceCol "G" , "Sheet1", "ABC.xls"
    End Sub
    [/vba]

    By doing this, I can ensure that all operations happen on Sheet1 of ABC.xls file. I also will have option to change sheet name and file name, when required.

    Your advice is requested.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •