VBA for selecting a range of cells based on their value

danb96

New Member
Joined
Mar 4, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello, I am new to this forum so apologies if this is in the wrong place.

I'm writing a VBA code to open an Excel file and create a scatter graph using only two columns, which is pretty simple. However, I'd like to know how to select only the cells that have a value. For example, the excel sheet may be empty apart from data between S112:T7800. If I select the whole of both columns (S1:T1400000), the graph displays a lot of blanks spaces (as well as being inefficient code!). The data array will always be in column S to T, but the rows at which the array starts and finishes varies.

Any help would be appreciated as I'm at a bit of a loss.

Thanks in advance,
Dan
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Maybe this way
VBA Code:
Sub MM1()

    Dim fRow As Long, lRow As Long, myRange As Range
    If Cells(1, "S") <> "" Then
        fRow = 1
    Else
        fRow = Cells(1, "S").End(xlDown).Row
    End If
    lRow = Cells(Rows.Count, "T").End(xlUp).Row
    If lRow >= fRow Then
        Set myRange = Range(Cells(fRow, "S"), Cells(lRow, "T"))
    End If
   myRange.Select
End Sub
 
Upvote 0
Michael M,

Thanks very much for your reply. I think because of the way my S and T columns were copied, unfortunately the code didn't work. However, it gave me the idea of putting unused cells as #N/A, which solved my problem. Thanks again,

Dan
 
Upvote 0
Ok....as long as you got the required result..:cool:(y)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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