0

I have an Excel spreadsheet tool to generate license images (.png files), which are printed using a wax-resin to PVC printer.

I'd like to simultaneously, conditionally select up to eight specific non-contiguous range clusters. If a defined cell has something, select the range cluster.

Something like:

if E4 is not blank, select D3:G18
if L4 is not blank, select K3:M18
if S4 is not blank, select R3:U18
if Y4 is not blank, select X3:Z18
if E24 is not blank, select D23:G38
if L24 is not blank, select K23:M38
if S24 is blank, don't select R23:U38
if Y24 is blank, don't select X23:Z38

I have code that's selecting all of the range clusters, but with no "intelligence".

Sub Select_Licenses()  
Range("D3:G18,K3:M18,R3:U18,X3:Z18,D23:G38,K23:M38,R23:U38,X23:Z38").Select  
End Sub
Community
  • 1
  • 1
  • 1
    You can test each cell in turn using `Application.Union()` to build the range to select. Eg see `BuildRange` here: https://stackoverflow.com/a/64778203/478884 – Tim Williams Jan 27 '22 at 19:50

1 Answers1

0

You can test each cell in turn using Application.Union() to build the range to select.

Sub Tester()
    Dim ws as worksheet, rng As Range
    Set ws = ActiveSheet
    
    if Len(ws.range("E4").Value) > 0 Then BuildRange rng, ws.Range("D3:G18")
    '...
    '...
    if Len(ws.range("Y44").Value) = 0 Then BuildRange rng, ws.Range("X23:Z38")

    if not rng is nothing then rng.select

End Sub

'utility sub for building ranges using Union
Sub BuildRange(ByRef rngTot As Range, rngAdd As Range)
    If rngTot Is Nothing Then
        Set rngTot = rngAdd
    Else
        Set rngTot = Application.Union(rngTot, rngAdd)
    End If
End Sub


Tim Williams
  • 154,628
  • 8
  • 97
  • 125