Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Select only columns

Hello:

I'm using the following code to dynamically assign addresses based on
field headers for a report. They are address fields that are broken up
into 5 columns that I concatenate into 1 column. When I use
Intersect(ActiveSheet.UsedRange, Range("B:B, Z:Z")) it works fine, but
if I change to Intersect(ActiveSheet.UsedRange, Range(HAdd, BAdd)) it
treats it as Range(B:Z), how do I change this?

Here's the code!

Sub headername()
Dim MyCell As Range
Dim Rng As Range
Dim LRow As Integer
Dim HAdd As Range
Dim BAdd As Range

LRow = LastRow(ActiveSheet)
Set Rng = Range("A2:FF" & LRow)

'* Dynamically assigns addresses for HAdd and BAdd

'* Set HAdd
Set HAdd = Cells.Find(What:="HomeAddressLine1", _
After:=ActiveCell, LookIn:=xlFormulas, _
Lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
HAdd = (HAdd.EntireColumn.Address)

'* Set BAdd
Set BAdd =
Cells.Find(What:="BusinessAddressLine1", _
After:=ActiveCell, LookIn:=xlFormulas, _
Lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
BAdd = (BAdd.EntireColumn.Address)

'* Concatenates 5 address columns for Each HAdd and BAdd
For Each MyCell In Intersect(ActiveSheet.UsedRange,
Range(HAdd, BAdd))
MyCell.Formula = MyCell.Value & " " _
& MyCell.Offset(0, 1).Value & " " _
& MyCell.Offset(0, 2).Value & " " _
& MyCell.Offset(0, 3).Value & " " _
& MyCell.Offset(0, 4).Value
MyCell.Formula = LTrim(MyCell.Formula)
MyCell.Formula = RTrim(MyCell.Formula)
Next
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Select only columns

I think the problem is the location of the comma. Notice in this...

Range("B:B, Z:Z")

the comma is part of the text (it is inside the quote marks), but in this...

Range(HAdd, BAdd)

the comma is not part of the text. My guess is that this will do what you
want...

Range(HAdd & "," & BAdd)

But that may not be your entire problem as you have declared HAdd and BAdd
to be ranges, but you assigned String values (the Address property of the
range found by your Find function call) to them. Remove the Address property
call (and those surrounding parentheses... using parentheses not required by
syntax can sometimes cause unexpected results, though that is not the case
here I don't think) and use a Set statement (because we are now working with
a real Range)...

Set HAdd = HAdd.EntireColumn
......
Set BAdd = BAdd.EntireColumn

although you can just tack the EntireColumn property right onto the returned
range from the Find function call (omitting the extra Set statement in the
process) and that should work fine too....

Set HAdd = Cells.Find(What:="HomeAddressLine1", After:=ActiveCell, _
LookIn:=xlFormulas, Lookat:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).EntireColumn
......
Set BAdd = Cells.Find(What:="BusinessAddressLine1", After:=ActiveCell, _
LookIn:=xlFormulas, Lookat:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).EntireColumn

Rick


wrote in message
...
Hello:

I'm using the following code to dynamically assign addresses based on
field headers for a report. They are address fields that are broken up
into 5 columns that I concatenate into 1 column. When I use
Intersect(ActiveSheet.UsedRange, Range("B:B, Z:Z")) it works fine, but
if I change to Intersect(ActiveSheet.UsedRange, Range(HAdd, BAdd)) it
treats it as Range(B:Z), how do I change this?

Here's the code!

Sub headername()
Dim MyCell As Range
Dim Rng As Range
Dim LRow As Integer
Dim HAdd As Range
Dim BAdd As Range

LRow = LastRow(ActiveSheet)
Set Rng = Range("A2:FF" & LRow)

'* Dynamically assigns addresses for HAdd and BAdd

'* Set HAdd
Set HAdd = Cells.Find(What:="HomeAddressLine1", _
After:=ActiveCell, LookIn:=xlFormulas, _
Lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
HAdd = (HAdd.EntireColumn.Address)

'* Set BAdd
Set BAdd =
Cells.Find(What:="BusinessAddressLine1", _
After:=ActiveCell, LookIn:=xlFormulas, _
Lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
BAdd = (BAdd.EntireColumn.Address)

'* Concatenates 5 address columns for Each HAdd and BAdd
For Each MyCell In Intersect(ActiveSheet.UsedRange,
Range(HAdd, BAdd))
MyCell.Formula = MyCell.Value & " " _
& MyCell.Offset(0, 1).Value & " " _
& MyCell.Offset(0, 2).Value & " " _
& MyCell.Offset(0, 3).Value & " " _
& MyCell.Offset(0, 4).Value
MyCell.Formula = LTrim(MyCell.Formula)
MyCell.Formula = RTrim(MyCell.Formula)
Next
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Select only columns

Range is is ofteen misunderstood. It takes two arguments. The second is
optional. If I specify only the first argument then it is the range defined
by that address. If I specify both then it is the rectangular area between
the 2 addresses.

Use this...
Intersect(HAdd.Parent.UsedRange, Union(HAdd, Badd))

Note that I used Hadd.Parent in place of activesheet. Activesheet is kind of
dangerous as code changes can alter the active sheet and generate an error on
this line. By using the parent of the range Hadd which is the sheet that Hadd
is on we avoid this potential problem...
--
HTH...

Jim Thomlinson


" wrote:

Hello:

I'm using the following code to dynamically assign addresses based on
field headers for a report. They are address fields that are broken up
into 5 columns that I concatenate into 1 column. When I use
Intersect(ActiveSheet.UsedRange, Range("B:B, Z:Z")) it works fine, but
if I change to Intersect(ActiveSheet.UsedRange, Range(HAdd, BAdd)) it
treats it as Range(B:Z), how do I change this?

Here's the code!

Sub headername()
Dim MyCell As Range
Dim Rng As Range
Dim LRow As Integer
Dim HAdd As Range
Dim BAdd As Range

LRow = LastRow(ActiveSheet)
Set Rng = Range("A2:FF" & LRow)

'* Dynamically assigns addresses for HAdd and BAdd

'* Set HAdd
Set HAdd = Cells.Find(What:="HomeAddressLine1", _
After:=ActiveCell, LookIn:=xlFormulas, _
Lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
HAdd = (HAdd.EntireColumn.Address)

'* Set BAdd
Set BAdd =
Cells.Find(What:="BusinessAddressLine1", _
After:=ActiveCell, LookIn:=xlFormulas, _
Lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
BAdd = (BAdd.EntireColumn.Address)

'* Concatenates 5 address columns for Each HAdd and BAdd
For Each MyCell In Intersect(ActiveSheet.UsedRange,
Range(HAdd, BAdd))
MyCell.Formula = MyCell.Value & " " _
& MyCell.Offset(0, 1).Value & " " _
& MyCell.Offset(0, 2).Value & " " _
& MyCell.Offset(0, 3).Value & " " _
& MyCell.Offset(0, 4).Value
MyCell.Formula = LTrim(MyCell.Formula)
MyCell.Formula = RTrim(MyCell.Formula)
Next
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
error - select method failed - (columns.select) PBcorn Excel Programming 3 May 19th 08 10:49 AM
Macro won't select columns Vick Excel Discussion (Misc queries) 1 August 17th 07 02:17 AM
VB to go across and then select columns Darin Kramer Excel Programming 8 August 28th 06 11:06 PM
Select second columns only My View Excel Discussion (Misc queries) 3 July 7th 05 11:37 PM
Select from Columns ten Excel Programming 1 August 13th 03 05:56 PM


All times are GMT +1. The time now is 05:31 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"