Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Dynamic Range based on cell contents

Excel 2003

I am trying to use a macro to search column a for the Text "RD". I have
used the code below many times to find each instance in a column. Now, I
want to find each instance, but after one instance, assign the value to a
variable, go to the next instance, assign that to a variable, and then
perform some operations. on that range. I'm only concerned at this point
with getting the selection and variable assignments to work. The code below
works for the first instance, but always assigns 5 to J. I am stumped.
Thanks in advance for any help.

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
For I = Range("A65536").End(xlUp).Row To 1 Step -1
If Range("A" & I).Value = "RD" Then
EndCell = I - 1
For J = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
Exit For
End If

Next J
End If
Next I
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Dynamic Range based on cell contents

I'm not sure what you ae trying to do, but you need to keep on advancing the
end point. Your code is always going back to 65536. I think the change
below is what you may need.

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
EndCell = 65536
For I = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & I).Value = "RD" Then
EndCell = I - 1
For J = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
Exit For
End If

Next J
EndCell = BegCell - 1
End If
Next I
End Sub


"Phil Trumpy" wrote:

Excel 2003

I am trying to use a macro to search column a for the Text "RD". I have
used the code below many times to find each instance in a column. Now, I
want to find each instance, but after one instance, assign the value to a
variable, go to the next instance, assign that to a variable, and then
perform some operations. on that range. I'm only concerned at this point
with getting the selection and variable assignments to work. The code below
works for the first instance, but always assigns 5 to J. I am stumped.
Thanks in advance for any help.

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
For I = Range("A65536").End(xlUp).Row To 1 Step -1
If Range("A" & I).Value = "RD" Then
EndCell = I - 1
For J = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
Exit For
End If

Next J
End If
Next I
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Dynamic Range based on cell contents

Joel, Thank you for the quick response. Your help is much appreciated. I'm
sorry if I wasn't clear in my previous post. Here is a small description of
the problem I am encountering.

The sample sheet that I have contains 695 rows of data in column A. THis
number could vary which is why I am trying to set up a dynamic range. The
first loop starts at Row 695 as I would expect. The first time the text "RD"
appears in column A is at Row 694. Then, The EndCell variable is assigned a
variable of 693 as I would also expect. The problem I am having is that when
the second loop starts, it always begins with Row 5 and counts down to 0.
The next time "RD" appears is at Row 641, but I can't get the loop to start
searching at "A693" as I would like. Your code to assign EndCell a value of
65536 does exactly what my code did before. BegCell is never receiving a
value, so all the line of code you added at the end does is assign -1 to
EndCell. Again, forgive me if I am not being clear in my problem. I would
expect the code that I have to start the 2nd loop at Row 693 and search until
the next "RD" appearance at Row 641. At this point, I will Select the range,
do a few things, and then exit back to the first loop to get the next 2
occurences of "RD" in column A.

"Joel" wrote:

I'm not sure what you ae trying to do, but you need to keep on advancing the
end point. Your code is always going back to 65536. I think the change
below is what you may need.

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
EndCell = 65536
For I = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & I).Value = "RD" Then
EndCell = I - 1
For J = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
Exit For
End If

Next J
EndCell = BegCell - 1
End If
Next I
End Sub


"Phil Trumpy" wrote:

Excel 2003

I am trying to use a macro to search column a for the Text "RD". I have
used the code below many times to find each instance in a column. Now, I
want to find each instance, but after one instance, assign the value to a
variable, go to the next instance, assign that to a variable, and then
perform some operations. on that range. I'm only concerned at this point
with getting the selection and variable assignments to work. The code below
works for the first instance, but always assigns 5 to J. I am stumped.
Thanks in advance for any help.

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
For I = Range("A65536").End(xlUp).Row To 1 Step -1
If Range("A" & I).Value = "RD" Then
EndCell = I - 1
For J = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
Exit For
End If

Next J
End If
Next I
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Dynamic Range based on cell contents

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
EndCell = Range("A" & Rows.Count).End(xlUp).Row
Do While EndCell 0
If Range("A" & EndCell).Value = "RD" Then
EndCell = EndCell - 1
BegCell = EndCell
For J = BegCell To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
BegCell = BegCell - 1
Exit For
End If
Next J
EndCell = BegCell
End If
EndCell = EndCell - 1
Loop
End Sub
"Phil Trumpy" wrote:

Joel, Thank you for the quick response. Your help is much appreciated. I'm
sorry if I wasn't clear in my previous post. Here is a small description of
the problem I am encountering.

The sample sheet that I have contains 695 rows of data in column A. THis
number could vary which is why I am trying to set up a dynamic range. The
first loop starts at Row 695 as I would expect. The first time the text "RD"
appears in column A is at Row 694. Then, The EndCell variable is assigned a
variable of 693 as I would also expect. The problem I am having is that when
the second loop starts, it always begins with Row 5 and counts down to 0.
The next time "RD" appears is at Row 641, but I can't get the loop to start
searching at "A693" as I would like. Your code to assign EndCell a value of
65536 does exactly what my code did before. BegCell is never receiving a
value, so all the line of code you added at the end does is assign -1 to
EndCell. Again, forgive me if I am not being clear in my problem. I would
expect the code that I have to start the 2nd loop at Row 693 and search until
the next "RD" appearance at Row 641. At this point, I will Select the range,
do a few things, and then exit back to the first loop to get the next 2
occurences of "RD" in column A.

"Joel" wrote:

I'm not sure what you ae trying to do, but you need to keep on advancing the
end point. Your code is always going back to 65536. I think the change
below is what you may need.

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
EndCell = 65536
For I = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & I).Value = "RD" Then
EndCell = I - 1
For J = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
Exit For
End If

Next J
EndCell = BegCell - 1
End If
Next I
End Sub


"Phil Trumpy" wrote:

Excel 2003

I am trying to use a macro to search column a for the Text "RD". I have
used the code below many times to find each instance in a column. Now, I
want to find each instance, but after one instance, assign the value to a
variable, go to the next instance, assign that to a variable, and then
perform some operations. on that range. I'm only concerned at this point
with getting the selection and variable assignments to work. The code below
works for the first instance, but always assigns 5 to J. I am stumped.
Thanks in advance for any help.

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
For I = Range("A65536").End(xlUp).Row To 1 Step -1
If Range("A" & I).Value = "RD" Then
EndCell = I - 1
For J = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
Exit For
End If

Next J
End If
Next I
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Dynamic Range based on cell contents

Thank you Joel. This worked great! I appreciate your help and patience.

"Joel" wrote:

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
EndCell = Range("A" & Rows.Count).End(xlUp).Row
Do While EndCell 0
If Range("A" & EndCell).Value = "RD" Then
EndCell = EndCell - 1
BegCell = EndCell
For J = BegCell To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
BegCell = BegCell - 1
Exit For
End If
Next J
EndCell = BegCell
End If
EndCell = EndCell - 1
Loop
End Sub
"Phil Trumpy" wrote:

Joel, Thank you for the quick response. Your help is much appreciated. I'm
sorry if I wasn't clear in my previous post. Here is a small description of
the problem I am encountering.

The sample sheet that I have contains 695 rows of data in column A. THis
number could vary which is why I am trying to set up a dynamic range. The
first loop starts at Row 695 as I would expect. The first time the text "RD"
appears in column A is at Row 694. Then, The EndCell variable is assigned a
variable of 693 as I would also expect. The problem I am having is that when
the second loop starts, it always begins with Row 5 and counts down to 0.
The next time "RD" appears is at Row 641, but I can't get the loop to start
searching at "A693" as I would like. Your code to assign EndCell a value of
65536 does exactly what my code did before. BegCell is never receiving a
value, so all the line of code you added at the end does is assign -1 to
EndCell. Again, forgive me if I am not being clear in my problem. I would
expect the code that I have to start the 2nd loop at Row 693 and search until
the next "RD" appearance at Row 641. At this point, I will Select the range,
do a few things, and then exit back to the first loop to get the next 2
occurences of "RD" in column A.

"Joel" wrote:

I'm not sure what you ae trying to do, but you need to keep on advancing the
end point. Your code is always going back to 65536. I think the change
below is what you may need.

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
EndCell = 65536
For I = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & I).Value = "RD" Then
EndCell = I - 1
For J = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
Exit For
End If

Next J
EndCell = BegCell - 1
End If
Next I
End Sub


"Phil Trumpy" wrote:

Excel 2003

I am trying to use a macro to search column a for the Text "RD". I have
used the code below many times to find each instance in a column. Now, I
want to find each instance, but after one instance, assign the value to a
variable, go to the next instance, assign that to a variable, and then
perform some operations. on that range. I'm only concerned at this point
with getting the selection and variable assignments to work. The code below
works for the first instance, but always assigns 5 to J. I am stumped.
Thanks in advance for any help.

Sub SelectRDRows()
Dim I As Long
Dim J As Long
Dim BegCell As Long
Dim EndCell As Long
For I = Range("A65536").End(xlUp).Row To 1 Step -1
If Range("A" & I).Value = "RD" Then
EndCell = I - 1
For J = Range("A" & EndCell).End(xlUp).Row To 1 Step -1
If Range("A" & J).Value = "RD" Then
BegCell = J + 1
Range("A" & BegCell & ":" & "A" & EndCell).Select
Exit For
End If

Next J
End If
Next I
End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hyperlink to Named Range Based On Cell Contents TKS_Mark Excel Worksheet Functions 1 January 9th 08 04:14 PM
Reference Data Range based on cell contents PCLIVE Charts and Charting in Excel 0 February 27th 06 03:01 PM
copying dynamic range based on cell outside of range xcelelder Excel Programming 3 September 29th 05 05:08 PM
Set range based on cell contents - help required N E Body[_4_] Excel Programming 5 July 27th 04 02:48 PM
Dynamic Range Names & Clear Cell Contents Q John[_78_] Excel Programming 1 June 13th 04 01:59 PM


All times are GMT +1. The time now is 03:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"