Tricky Dynamic Range

johngio

Board Regular
Joined
Jan 28, 2005
Messages
174
Hi all,

I'm trying to set up a dynamic range - got the columns sussed, but the rows are a bit trickeir. Here is my scenario.

I have a range of values in the Y and Z column. These can appear anywhere in the column.

I have values in the G column. These can start anywhere from Row 16 to Row 39.

I want to set up my dynamic array FROM:

The FIRST non zero entry in the G column AFTER row 15.

TO:

The last non zero entry in EITHER row Y or Z.


I know for the TO part I can do:

Cells(Rows.Count, "Z").End(xlUp).Row

Well, that covers the Z column anyways - however it doesn't restrict itself to non zeros. . .

Any ideas??

Thanks

John
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

acw

MrExcel MVP
Joined
Feb 13, 2004
Messages
4,814
John

This is extremely clunky but should get you the range.


Code:
Sub aaa()
  Dim GRow As Integer
  Dim LastCol As Integer, LastRow As Integer
  'G row
  i = 15
  Do
    i = i + 1
  Loop Until Cells(i, 7) > 0
  GRow = Cells(i, "G").Row
  
  Roww = Cells(Rows.Count, 25).End(xlUp).Row + 1
  Do
    Roww = Roww - 1
  Loop Until Cells(Roww, 25) > 0
  LastRow = Roww
  LastCol = 25
  
  Roww = Cells(Rows.Count, 26).End(xlUp).Row + 1
  Do
    Roww = Roww - 1
  Loop Until Cells(Roww, 26) > 0
  
  If Roww > LastRow Then
    LastRow = Roww
    LastCol = 26
  End If
  
  Range(Cells(GRow, "G"), Cells(LastRow, LastCol)).Select
  MsgBox GRow
End Sub


Tony
 

johngio

Board Regular
Joined
Jan 28, 2005
Messages
174
Thanks Tony.

This formula gives me the number of cells in the range, but I can muck around with it and get the first and last cells.

Only 1 thing (which I am currently trying to work on.

Basically I want to start my macro with:

For iRow = something to something
For iCol = 7 to 9

and specify the range here.

Any idea how I can adapt this formula to fit this format
 

acw

MrExcel MVP
Joined
Feb 13, 2004
Messages
4,814
John

This is a bit more compact
Code:
Sub bbb()
  Dim LastRow As Integer, LastCol As Integer, GRow As Integer
  
  'G row
  i = 15
  Do
    i = i + 1
  Loop Until Cells(i, 7) > 0
  GRow = Cells(i, "G").Row
  
  'Y or Z
  Set findit = Range("Y:Z").Find(what:="*", searchDirection:=xlPrevious, after:=Range("Z1"))
  roww = findit.Row + 1
  Do
  roww = roww - 1
  Loop Until Cells(roww, "Y") > 0 Or Cells(roww, "Z") > 0
  
  LastRow = roww
  If Cells(LastRow, "Y") > 0 Then
    LastCol = 25
  Else
    LastCol = 26
  End If
  Range(Cells(GRow, "G"), Cells(LastRow, LastCol)).Select
  
End Sub

Both bits of code give you the relevant start and end cells.

Cells(GRow,"G") gives you the first cell in column G
cells(lastrow,lastcol) will give you the last cell in either Y or Z that is > 0

I only put in the range selection statement to show how the range could be selected.

Tony
 

Forum statistics

Threads
1,137,339
Messages
5,680,910
Members
419,940
Latest member
WilliamPorter

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
Top