0

First post here so apologies for any lack of basic decorum. I have a pretty simple understanding of VBA, and I'm hoping someone may be able to point me in the right direction for a problem I'm trying to solve.

Background:
I have an excel spreadsheet tool that I used to generate printable license images (.png files), which are in turn printed using a wax-resin to PVC printer. I'm presently working on a few minor quality of life improvements for the tool.

The challenge:
I'd like to see if I can find a way to simultaneously, conditionally select up to 8 specific non-contiguous range clusters (these specific ranges below). Basically, if a defined cell has something, select the range cluster. If the defined cell is empty, do not. Expressed in total noob-speak, an example of how it might play out would be 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

Right now I have a super basic bit of 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

Thank you for any help you may be able to offer. I'm having fun learning to use VBA to extend some of the tools I use, but I'm still extremely green with basic coding and

BigBen
  • 38,994
  • 6
  • 24
  • 37
  • 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
  • 137,250
  • 8
  • 88
  • 114