Solved

excel vba subroutine to return a range

Posted on 2013-10-30
6
600 Views
Last Modified: 2013-11-01
In my main Sub, I am copying a 1 columned range.

.range.copy

I would like to then call the following sub, which will return me back a different range, r, which I will use in my main sub.
What is the correct way to call DupeKiller and return back r?

Sub DupeKiller()
Dim wsTemp As Worksheet, lRows As Long, rTemp As Range
Set wsTemp = ThisWorkbook.Worksheets("Temp")
    With wsTemp
        .Range("A1").PasteSpecial xlPasteValues
        lRows = .Range("A" & Rows.Count).End(xlUp).Row
        Set rTemp = .Range("A1:A" & lRows)
        With rTemp
            .RemoveDuplicates Columns:=1, Header:=xlNo
        End With
        lRows = .Range("A" & Rows.Count).End(xlUp).Row
        Set rTemp = .Range("A1:A" & lRows)
        Set r = rTemp
        rTemp.ClearContents
    End With
End Sub

Open in new window

0
Comment
Question by:newparadigmz
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39612811
I would change it to a function that returns a range value.
Below I am showing an example of calling the function.

Another way would be to use a public variable that both routines could access.

To set the return value for a function , instead of using 'r' you would have to use the name of the function.  That is why I changed the line of code from "Set r = rTemp" to "Set DupeKiller = rTemp"

Sub main()
   
   Dim myRange as Range
   Set myRange = DupKiller() 

End Sub


Function DupeKiller() as Range
Dim wsTemp As Worksheet, lRows As Long, rTemp As Range
Set wsTemp = ThisWorkbook.Worksheets("Temp")
    With wsTemp
        .Range("A1").PasteSpecial xlPasteValues
        lRows = .Range("A" & Rows.Count).End(xlUp).Row
        Set rTemp = .Range("A1:A" & lRows)
        With rTemp
            .RemoveDuplicates Columns:=1, Header:=xlNo
        End With
        lRows = .Range("A" & Rows.Count).End(xlUp).Row
        Set rTemp = .Range("A1:A" & lRows)
        Set DupeKiller = rTemp
        rTemp.ClearContents
    End With
End Function

Open in new window

0
 

Author Comment

by:newparadigmz
ID: 39613999
Exiting this function does not retain the values inside the range.

It could be a range or an array with the values. I just need something to pass back to the main sub.
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39614265
Not sure what you mean by "it does not retain the values inside the range".

A range is a group of cells.  It can be a column, it can be a row, it can be just 5 cells selected that are part of a column.  Each cell in a range of cells is going to have some value, whether that is a text string or a NULL or whatever value.  By selecting or identifying a cell or a group of cells, it cannot "lose" its value.

What your original question asked is... "What is the correct way to call DupeKiller and return back r?"   What I did was modify your routine so that it would return an object known as a range.  That range is a group of one or more cells.

That being said... If the Function is not working the way you expected it to, then I'll need you to expand on your question a little in order to identify specifically what behavior you are expecting upon return from the function and/or Subroutine.   Once we know what you are expecting, we can change the code accordingly.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:newparadigmz
ID: 39614816
In the attached file, running sub main, I need avRange populated with values.

Thanks
Book1.xlsm
0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
ID: 39615346
Here is what your code does in DupeKiller

Step 1 Copy Column 'A' data from "Not Temp" worksheet to "Temp" worksheet.
Step 2 Remove Duplicate items from list in "Temp" Worksheet"
Step 3 Sets a new Range that is equal to the length of Column with Duplicates removed
Step 4 Sets Dupkiller return value to that new range object
Step 5 "Rtemp.clearcontents"  this command deletes all the values you just caclulated in the "Temp" worksheet.
Step 6 upon return to Main... you set the aVrange equal to return value from function. (but now that range as been cleared of data.)

So this code has 2 basic changes

(1) I removed the clearContents in the DupeKiller function
(2) I added a 2 msgbox calls in the Main routine to display values from avRange.

(NOTE: avRange.values are really just cells values from worksheet "Temp")

so if you clear the temp worksheet values, you in effect clear avRange.values as well.

Option Explicit

Sub main()
   Dim myRange As Range, avRange As Variant
   ThisWorkbook.Worksheets("Not Temp").Range("A1:A12").Copy
   Set myRange = DupeKiller()
   avRange = myRange.Value
   MsgBox "Cell A1=" & avRange(1, 1)
   MsgBox "Cell A2=" & avRange(2, 1)

End Sub

Function DupeKiller() As Range
Dim wsTemp As Worksheet, lRows As Long, rTemp As Range
Set wsTemp = ThisWorkbook.Worksheets("Temp")
    With wsTemp
        .Range("A1").PasteSpecial xlPasteValues
        lRows = .Range("A" & Rows.Count).End(xlUp).Row
        Set rTemp = .Range("A1:A" & lRows)
        With rTemp
            .RemoveDuplicates Columns:=1, Header:=xlNo
        End With
        lRows = .Range("A" & Rows.Count).End(xlUp).Row
        Set rTemp = .Range("A1:A" & lRows)
        Set DupeKiller = rTemp
    End With
End Function

Open in new window

0
 

Author Closing Comment

by:newparadigmz
ID: 39616860
thank you very much!
i was confusing the reference to the range with the contents of the range.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question