Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Selecting multiple ranges

I am trying to copy selected columns from ne shet to another to print
them using VBA in Excel. I have found that when you select multiple
ranges in Excel it seems that if you try to unselect one of the
selected columns you get another selected range. So if I select columns
A to D and columns E to H, I have 2 ranges. Then I try to unselect
column B ( in fact it doesn't unselect but jusr remains highlighted) as
I don't want it I get another range with just column B in. When I come
to copy the selected ranges I get col B twice. Is there a way round
this?
john

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Selecting multiple ranges

Trying to copy non-contiguous blocks of cells is never easy. For example if
you select columns A and C (don't select B) and copy them and then try to
paste to a new worksheet, material gets pasted into A & B, not A & C. I
suggest that you:

1. copy with a series of contiguous copy/pastes to the second sheet
or
2. hide the columns you don't want to print and avoid copying at all
--
Gary's Student


" wrote:

I am trying to copy selected columns from ne shet to another to print
them using VBA in Excel. I have found that when you select multiple
ranges in Excel it seems that if you try to unselect one of the
selected columns you get another selected range. So if I select columns
A to D and columns E to H, I have 2 ranges. Then I try to unselect
column B ( in fact it doesn't unselect but jusr remains highlighted) as
I don't want it I get another range with just column B in. When I come
to copy the selected ranges I get col B twice. Is there a way round
this?
john


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Selecting multiple ranges


Hello John

You can select multiple ranges in code 2 ways... Theses examples selec
the entire columns of A and C through H

Example 1
Range("A:A,C:H").Selec

Exmaple 2
Application.Union.Range("A:A", "C:H").Selec

Sincerely
Leith Ros

--
Leith Ros
-----------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846
View this thread: http://www.excelforum.com/showthread.php?threadid=50646

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Selecting multiple ranges

I am trying to allow users to print only selected columns from a
datasheet. So they select the columns they want to print and tey are
pasted to a hidden worksheet contiguously and printed. I am using:
cntA = Selection.Areas.Count to count the different areas selected.
Then I can work out how many columns each area has b
icnt2 = Selection.Areas(icnt1).Columns.Count
then knowing what is selected I can copy it to the new sheet in order.
But the "deselected" column appears as a further Selection.area which
screws it up.
Is there a way to tell if a column is selected?
John

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Selecting multiple ranges

I've found that it's always a pain to get headers/footers correct. And
columnwidths and rowheights to do what I want.

One alternative is to copy the worksheet to a new workbook, change everything to
values, delete the columns, print the new worksheet and then close the new
workbook without saving.

If you think that's something you want to try:

Option Explicit
Sub testme()
Dim tempWks As Worksheet
Dim curWks As Worksheet
Dim rng As Range
Dim iCol As Long
Dim LastCol As Long

Set curWks = Worksheets("sheet1")
curWks.Select

Set rng = Nothing
On Error Resume Next
Set rng = Application.InputBox(Prompt:="Select a bunch of cells", Type:=8)
On Error GoTo 0

If rng Is Nothing Then
Exit Sub 'cancel
End If

With curWks
If rng.Parent.Name < .Name Then
MsgBox "Please select something on: " & .Name
Exit Sub
End If

Set rng = Intersect(.Rows(1), rng.EntireColumn).EntireColumn
.Copy 'to a new workbook
End With

Set tempWks = ActiveSheet
With tempWks
With .UsedRange
.Copy
.PasteSpecial Paste:=xlValues
End With
LastCol = .Cells.SpecialCells(xlCellTypeLastCell).Column
For iCol = LastCol To 1 Step -1
If Intersect(curWks.Columns(iCol), rng) Is Nothing Then
.Columns(iCol).Delete
End If
Next iCol

'save some trees
.PrintOut preview:=True

'uncomment when you're done testing
.Parent.Close savechanges:=False

End With
End Sub


wrote:

I am trying to allow users to print only selected columns from a
datasheet. So they select the columns they want to print and tey are
pasted to a hidden worksheet contiguously and printed. I am using:
cntA = Selection.Areas.Count to count the different areas selected.
Then I can work out how many columns each area has b
icnt2 = Selection.Areas(icnt1).Columns.Count
then knowing what is selected I can copy it to the new sheet in order.
But the "deselected" column appears as a further Selection.area which
screws it up.
Is there a way to tell if a column is selected?
John


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Selecting multiple ranges

Thanks I will give it a go.

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
Trouble with selecting multiple ranges of data markag Excel Worksheet Functions 2 June 23rd 06 04:35 PM
Selecting multiple ranges using 'Cells' notation Pete[_22_] Excel Programming 4 June 30th 05 05:42 PM
selecting multiple ranges Helen Excel Programming 3 January 16th 05 03:46 AM
Selecting ranges Steve Wood Excel Programming 1 August 24th 04 02:16 PM
selecting ranges MDC Excel Programming 1 July 24th 03 03:34 PM


All times are GMT +1. The time now is 11:20 AM.

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"