How to get the Range of Cells with "Values" within a Range of formulas

jrtraylor

New Member
Joined
Jun 16, 2012
Messages
14
I have a range of formulas (E12:O21) The Header Row is (E11-O11) - Total Row is (E21:O21).
I want to use VBA to search that range of formulas and find the cells with values to return the "actual" used range within that range. I will use this new range to create a chart eliminating blank chart areas. The formulas in the range will always display values in order meaning they will fill down and across and will not have values scattered around the formula range. The below example would return a new Range of (E12:I16).
EFGHIJKLMN
11Group 1Group 2Group 3Group 4Group 5Group 6Group 7Group 8Group 9Group 10
12VVVVV
13VVVV
14VVV
15VV
16V
17
18
19
20
21
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I did try to look at each cell to create an array of the cell addresses that contain the values but once i had that, I could not figure out how to convert the array of cells back to a range.
VBA Code:
Sub RangeTest()
Dim grpRng As Range, NewgrpRng As Range
Dim ws1 As Worksheet
Dim x As Variant


Set ws1 = ThisWorkbook.Sheets("Affinity (3)")


Set grpRng = ws1.Range("E12:K19")




For Each cel In grpRng
    If cel.Value > "" Then
      x = x + cel.Address
    End If
Next cel


 MsgBox x
End Sub
 
Upvote 0
If by the method of looping over all the cells of the range, then rather this way:
VBA Code:
Sub AAA()
    Dim grpRng As Range
    Dim cel As Range
    Dim ws1 As Worksheet
    Dim lColStart As Long, lColEnd As Long
    Dim lRowStart As Long, lRowEnd As Long
    Dim rngResult As Range

    Set ws1 = ActiveSheet

    Set grpRng = ws1.Range("E12:O20")

    lRowEnd = grpRng.Row
    lColEnd = grpRng.Column
    lRowStart = grpRng.Row
    lColStart = grpRng.Column

    For Each cel In grpRng
        If cel.Value <> "" Then
            If cel.Row < lRowStart Then
                lRowStart = cel.Row
            End If
            If cel.Row > lRowEnd Then
                lRowEnd = cel.Row
            End If
            If cel.Column < lColStart Then
                lColStart = cel.Column
            End If
            If cel.Column > lColEnd Then
                lColEnd = cel.Column
            End If
        End If
    Next cel

    Set rngResult = ws1.Range(ws1.Cells(lRowStart, lColStart), ws1.Cells(lRowEnd, lColEnd))
    MsgBox rngResult.Address(0, 0)

End Sub

Artik
 
Upvote 0
Solution
Give this a try...
VBA Code:
Sub SetUsedRange()

    Dim LastRow As Long,  LastCol As Long
    Dim UsedRange As Range

    LastRow = Range("E12:N21").Find("V", , xlValues, , xlRows, xlPrevious).Row
    LastCol = Range("E12:N21").Find("V", , xlValues, , xlByColumns, xlPrevious).Column
    Set UsedRange = Range("E12", Cells(LastRow, LastCol))

    MsgBox UsedRange.Address(0, 0)

End Sub
 
Last edited:
Upvote 0
If by the method of looping over all the cells of the range, then rather this way:
VBA Code:
Sub AAA()
    Dim grpRng As Range
    Dim cel As Range
    Dim ws1 As Worksheet
    Dim lColStart As Long, lColEnd As Long
    Dim lRowStart As Long, lRowEnd As Long
    Dim rngResult As Range

    Set ws1 = ActiveSheet

    Set grpRng = ws1.Range("E12:O20")

    lRowEnd = grpRng.Row
    lColEnd = grpRng.Column
    lRowStart = grpRng.Row
    lColStart = grpRng.Column

    For Each cel In grpRng
        If cel.Value <> "" Then
            If cel.Row < lRowStart Then
                lRowStart = cel.Row
            End If
            If cel.Row > lRowEnd Then
                lRowEnd = cel.Row
            End If
            If cel.Column < lColStart Then
                lColStart = cel.Column
            End If
            If cel.Column > lColEnd Then
                lColEnd = cel.Column
            End If
        End If
    Next cel

    Set rngResult = ws1.Range(ws1.Cells(lRowStart, lColStart), ws1.Cells(lRowEnd, lColEnd))
    MsgBox rngResult.Address(0, 0)

End Sub

Give this a try...
VBA Code:
Sub SetUsedRange()

    Dim LastRow As Long,  LastCol As Long
    Dim UsedRange As Range

    LastRow = Range("E12:N21").Find("V", , xlValues, , xlRows, xlPrevious).Row
    LastCol = Range("E12:N21").Find("V", , xlValues, , xlByColumns, xlPrevious).Column
    Set UsedRange = Range("E12", Cells(LastRow, LastCol))

    MsgBox UsedRange.Address(0, 0)
End Sub
Thank you to both of you. Both solutions work perfectly. I am going to have to work with the Find method a little more. I had seen some other posts that mentioned using it to find "Values" but could not figure out how to get it to work for me so turned to the for each cell option.
 
Upvote 0

Forum statistics

Threads
1,216,742
Messages
6,132,453
Members
449,729
Latest member
davelevnt

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