Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Macro to Specific Cells in a Range

Hello,

I am trying to create a macro that would select specific cells within a
horizontal vector, so that I could perform an operation only on the selected
cells. For example, I have two horizontal ranges, Range 1 is A1:V1 and Range
2 is A12:V12. I would like to create a macro that would select the cells in
Range 2 only for which the corresponding cells in Range 1 are non blanks, so
that I could perform another operation on cells in Range 2 for which the
corresponding cells in Range 1 are non blanks. Any assistance with this would
be greatly appreciated!

Magnivy

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Macro to Specific Cells in a Range

Sub selectcells()
Dim rng1 As Range
Dim rng2 As Range
Dim rng As Range

On Error Resume Next
Set rng1 = Range("A1:V1").SpecialCells(xlCellTypeConstants)
Set rng2 = Range("A1:V1").SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If rng1 Is Nothing And rng2 Is Nothing Then
MsgBox "No blank cells"
Else
Set rng = rng1
If rng1 Is Nothing Then
Set rng = rng2
ElseIf rng2 Is Nothing Then
Set rng = rng1
Else
Set rng = Union(rng1, rng2)
End If
Intersect(rng.EntireColumn, Range("A12:V12")).Select
End If


End Sub


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Magnivy" wrote in message
...
Hello,

I am trying to create a macro that would select specific cells within a
horizontal vector, so that I could perform an operation only on the

selected
cells. For example, I have two horizontal ranges, Range 1 is A1:V1 and

Range
2 is A12:V12. I would like to create a macro that would select the cells

in
Range 2 only for which the corresponding cells in Range 1 are non blanks,

so
that I could perform another operation on cells in Range 2 for which the
corresponding cells in Range 1 are non blanks. Any assistance with this

would
be greatly appreciated!

Magnivy



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Macro to Specific Cells in a Range

Sub SelectRange()
dim cell as range, rng as Range
dim rng1 as Range, rng2 as Range
Dim rng3 as Range

set rng = Range("A1:V1")
for each cell in rng
if not isempty(cell) then
if rng1 is nothing then
set rng1 = cell
else
set rng1 = union(rng,cell)
end if
end if
Next
If not rng1 is nothing then
set rng2 = range("A12:V12")
set rng3 = intersect(rng1.entireColumn,rng2)
rng3.Select
end if
end Sub

If I know if the non empty cells contained constants or formulas
exclusively, you could use specialcells the occupied cells in your range.
Since I don't, it is just as easy to loop over such a small range and check.

--
Regards,
Tom Ogilvy


"Magnivy" wrote:

Hello,

I am trying to create a macro that would select specific cells within a
horizontal vector, so that I could perform an operation only on the selected
cells. For example, I have two horizontal ranges, Range 1 is A1:V1 and Range
2 is A12:V12. I would like to create a macro that would select the cells in
Range 2 only for which the corresponding cells in Range 1 are non blanks, so
that I could perform another operation on cells in Range 2 for which the
corresponding cells in Range 1 are non blanks. Any assistance with this would
be greatly appreciated!

Magnivy

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Macro to Specific Cells in a Range

Got it! Thanks a million Tom!

"Tom Ogilvy" wrote:

Sub SelectRange()
dim cell as range, rng as Range
dim rng1 as Range, rng2 as Range
Dim rng3 as Range

set rng = Range("A1:V1")
for each cell in rng
if not isempty(cell) then
if rng1 is nothing then
set rng1 = cell
else
set rng1 = union(rng,cell)
end if
end if
Next
If not rng1 is nothing then
set rng2 = range("A12:V12")
set rng3 = intersect(rng1.entireColumn,rng2)
rng3.Select
end if
end Sub

If I know if the non empty cells contained constants or formulas
exclusively, you could use specialcells the occupied cells in your range.
Since I don't, it is just as easy to loop over such a small range and check.

--
Regards,
Tom Ogilvy


"Magnivy" wrote:

Hello,

I am trying to create a macro that would select specific cells within a
horizontal vector, so that I could perform an operation only on the selected
cells. For example, I have two horizontal ranges, Range 1 is A1:V1 and Range
2 is A12:V12. I would like to create a macro that would select the cells in
Range 2 only for which the corresponding cells in Range 1 are non blanks, so
that I could perform another operation on cells in Range 2 for which the
corresponding cells in Range 1 are non blanks. Any assistance with this would
be greatly appreciated!

Magnivy

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Macro to Specific Cells in a Range

Bob, thanks a lot for your help!

"Bob Phillips" wrote:

Sub selectcells()
Dim rng1 As Range
Dim rng2 As Range
Dim rng As Range

On Error Resume Next
Set rng1 = Range("A1:V1").SpecialCells(xlCellTypeConstants)
Set rng2 = Range("A1:V1").SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If rng1 Is Nothing And rng2 Is Nothing Then
MsgBox "No blank cells"
Else
Set rng = rng1
If rng1 Is Nothing Then
Set rng = rng2
ElseIf rng2 Is Nothing Then
Set rng = rng1
Else
Set rng = Union(rng1, rng2)
End If
Intersect(rng.EntireColumn, Range("A12:V12")).Select
End If


End Sub


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Magnivy" wrote in message
...
Hello,

I am trying to create a macro that would select specific cells within a
horizontal vector, so that I could perform an operation only on the

selected
cells. For example, I have two horizontal ranges, Range 1 is A1:V1 and

Range
2 is A12:V12. I would like to create a macro that would select the cells

in
Range 2 only for which the corresponding cells in Range 1 are non blanks,

so
that I could perform another operation on cells in Range 2 for which the
corresponding cells in Range 1 are non blanks. Any assistance with this

would
be greatly appreciated!

Magnivy




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
Macro - specific range puiuluipui Excel Discussion (Misc queries) 3 May 10th 09 11:33 AM
modify a macro to apply to a specific range of cells Dave F Excel Discussion (Misc queries) 2 April 25th 07 03:00 AM
Set Range as cells with specific value DoooWhat Excel Discussion (Misc queries) 1 March 2nd 07 02:12 PM
Need help - Macro to copy a specific range Dileep Chandran Excel Worksheet Functions 9 December 5th 06 09:10 AM
macro that sumif a specific range jane Excel Programming 1 January 21st 04 11:20 PM


All times are GMT +1. The time now is 01:44 PM.

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

About Us

"It's about Microsoft Excel"