Dynamic "Columns:=Array(1,2,3)" in excel VBA

mhrsolanki2020

New Member
Joined
May 21, 2017
Messages
3
Hello,
I am working on a project in which I need to keep a list of unique patient. As and when new entries are added to the main database sheet, i will add the name to the patientMaster list and then use remove duplicate method of Range function. The code being
Code:
patientMaster.Range(patientMaster).RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes

The problem is, currently I have 3 Columns viz, Name, Date Of Birth and Family Details. However, down the line, new columns might be added. Say i add 2 more columns, then i would have to re-edit the code to
Code:
patientMaster.Range(patientMaster).RemoveDuplicates Columns:=Array(1, 2, 3,[U][B]4,5[/B][/U]), Header:=xlYes

My question is: " Is there any way that I can do this dynamically?"

I do have a solution using IIf statements which counts the number of columns and then sets the Array
Code:
'numOfCol has the number of columns
patientMaster.Range(patientMaster).RemoveDuplicates Columns:=IIf(numOfCol = 7, (Array(1, 2, 3,4,5,6,7),IIf(numOfCol = 6,Array(1,2,3,4,5,6),IIf(numOfCol = 5,Array(1,2,3,4,5),IIf(numOfCol=4,Array(1,2,3,4),Array(1,2,3)))))  , Header:=xlYes

But this is very inefficient. If you can help me with any other efficient solution?

Mihir Solanki
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Rich (BB code):
    'numOfCol has the number of columns
   
    Dim MyArray As Variant
    MyArray = Evaluate("Row(1:" & numOfCol & ")") 'Formula that returnas an array seq.
    patientMaster.Range(patientMaster).RemoveDuplicates Columns:=MyArray, Header:=xlYes
 
Last edited by a moderator:
Upvote 0
Code:
    [COLOR=green]'numOfCol has the number of columns[/COLOR]
    
    [COLOR=darkblue]Dim[/COLOR] MyArray [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    MyArray = Evaluate("Row(1:" & numOfCol & ")") [COLOR=green]'Formula that returnas an array seq.[/COLOR]
    patientMaster.Range(patientMaster).RemoveDuplicates Columns:=MyArray, Header:=xlYes

Thanks for you reply. Can you explain what is happening in here assuming there are 6 columns
Rich (BB code):
 Evaluate("Row(1:6)")

is it that it will return (1,2,3,4,5,6) ?? how?
It is not apparent from the documentation I read here https://msdn.microsoft.com/en-us/library/office/ff193019.aspx
 
Upvote 0
- The Evaluate command can evaluate a worksheet formula and return the result.
- The worksheet formula, in this case, uses the ROW function.
- ROW(1:6) evaluates to an array of sequential numbers (1,2,3,4,5,6).
 
Upvote 0
x=Evaluate ("Row(1:" & lastColumn & ")")
.Range(Cells(1,1), Cells (lastRow,lastColumn)). RemoveDuplicates(Columns:=x, Headers:=xlNo)
 
Upvote 0
Rich (BB code):
.Range(.Cells(1,1), .Cells(lastRow,lastColumn)).RemoveDuplicates Columns:=x, Header:=xlNo

It's showing same error. I can share you whole post.

I started a new post there.

Also,
Dim x As Variant
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top