Solved

excel vba subroutine to return a range

Posted on 2013-10-30
6
559 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now