Avoiding repeating code in VBA

berian

New Member
Joined
Jul 8, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm currently trying to get my head around avoiding copying and pasting the same code, but looking into functions and sub routines, as got me a bit baffled.

Would anyone be able to help show me how I could avoid repeating myself in the example code below. Three different ranges that get duplicates highlighted.

VBA Code:
Sub HightlightDuplicates()
    Dim Rng As Range
    Dim Unq As UniqueValues

    Set Rng = MasterStock.Range("A3:A50000")
    Set Unq = Rng.FormatConditions.AddUniqueValues
    Unq.DupeUnique = xlDuplicate
    Unq.Interior.Color = 13551615
    Unq.Font.Color = RGB(156, 0, 6)
       
    Set Rng = MasterStock.Range("F3:F50000")
    Set Unq = Rng.FormatConditions.AddUniqueValues
    Unq.DupeUnique = xlDuplicate
    Unq.Interior.Color = 13551615
    Unq.Font.Color = RGB(156, 0, 6)
       
    Set Rng = MasterStock.Range("AD3:AD50000")
    Set Unq = Rng.FormatConditions.AddUniqueValues
    Unq.DupeUnique = xlDuplicate
    Unq.Interior.Color = 13551615
    Unq.Font.Color = RGB(156, 0, 6)

End Sub

Much Appreciated,
Berian
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi Berian,

Try changing this:
VBA Code:
Set Rng = MasterStock.Range("A3:A50000")

To this
VBA Code:
Set Rng = MasterStock.Range("A3:A50000,F3:F50000,AD3:AD50000")
and use your formatting block just once.
 
Upvote 1
Solution
Even though I liked this response a lot:
VBA Code:
        Set Rng = MasterStock.Range("A3:A50000,F3:F50000,AD3:AD50000")

Note that each of those are called "areas" of the range. (There are 3 areas in the above range.) There is a limit to the number of areas you can have in a range before Excel throws an error. (It's 51 in 365, I believe, but I believe it was 28 in 2016.)

So, on the other hand, using a loop (which allows ANY number of broken up ranges) is safest. And this is a very condensed way to go about it.
VBA Code:
Sub test()

Dim Rng As Range
Dim Unq As UniqueValues

Dim arrayString() As String, i As Integer
arrayString = Split("A,F,AD", ",") 'Put as many column letters here in a comma list as you need.

For i = 0 To UBound(arrayString)
    Set Rng = MasterStock.Range(arrayString(i) & "3" & ":" & arrayString(i) & "50000")
    Set Unq = Rng.FormatConditions.AddUniqueValues
    Unq.DupeUnique = xlDuplicate
    Unq.Interior.Color = 13551615
    Unq.Font.Color = RGB(156, 0, 6)
Next i

End Sub
 
Upvote 1
There is a limit to the number of areas you can have in a range before Excel throws an error. (It's 51 in 365, I believe, but I believe it was 28 in 2016.)

That's not actually true. There is, however, a limit of 255 characters for the address string that you can pass as the Range argument.
 
Upvote 1
That's not actually true. There is, however, a limit of 255 characters for the address string that you can pass as the Range argument.
Oh, okay. So if done progamatically with Union(), there is no limit to the number of areas? But in this thread, the proposed solution was writing: Range("A3:A50000,F3:F50000,AD3:AD50000"). Wouldn't that count as being a limit in this sense? Each of those areas are 9 characters. Including the commas, it's roughly just 28 total allowed, right?
 
Upvote 1
In this particular instance, if they were all that length, then yes, but since that is not what you said (and it wouldn't differ between versions), I felt it important to make the correction.
 
Upvote 1

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,091
Latest member
gaurav_7829

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