Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default Looping thru the active selection

How can I loop through all the selected sheets in an Excel
workbook. I have a partial macro called Duplicate. Also I
have already created the form frmReplace and the function
ReplaceText. I want to be able to copy all the selected
sheets to just after the last sheet in the active
selection and rename them according to the ReplaceText
criteria.


Sub Duplicate()
Dim sht As Object
frmReplace.Show

**Somehow get the active selected sheets and
**copy to just after the end of the last selected sheet.
ActiveSheets.Copy After:= **no of the last selected sheet
For Each sht In **select the sheets that were just copied
sht.Name = ReplaceText(sht.Name, frmReplace.tbFind,
frmReplace.tbReplace)
Next sht
End Sub






  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 599
Default Looping thru the active selection

You can use the SelectedSheets property of the Windows collection object.

Sub Duplicate()
Dim sht As Object
frmReplace.Show

**Somehow get the active selected sheets and
**copy to just after the end of the last selected sheet.
ActiveSheets.Copy After:= **no of the last selected sheet
For Each sht In **select the sheets that were just copied
sht.Name = ReplaceText(sht.Name, frmReplace.tbFind,
frmReplace.tbReplace)
Next sht
End Sub


I'm not sure how this will work. You show the userform in this sub. If the
userform's ShowModal is True, then the rest of sub won't execute until the
uf is closed and frmReplace.tbFind won't be available at that time. If
ShowModal is False, the sub will continue to execute but the user won't have
enough time to complete the textboxes.

I think this sub should just have one line

frmReplace.Show

and you should have a commandbutton on the form with this in its Click event

Private Sub CommandButton1_Click()

Dim sht As Object
Dim sSelShts As Sheets

Set sSelShts = ActiveWorkbook.Windows(1).SelectedSheets

For Each sht In sSelShts
sht.Copy after:=sSelShts(sSelShts.Count)
ActiveSheet.Name = ReplaceText(sht.Name, Me.tbFind.Text,
Me.tbReplace.Text)
Next sht

Unload Me

End Sub

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Looping thru the active selection

Something like

Dim sh As Worksheet
Dim lastSh as worksheet

For Each sh In ActiveWindow.SelectedSheets
Set lastSh = sh
Next sh

For Each sh In ActiveWindow.SelectedSheets
sh.copy after:=Worksheets(lastSh.name)
Next sh


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

wrote in message
...
How can I loop through all the selected sheets in an Excel
workbook. I have a partial macro called Duplicate. Also I
have already created the form frmReplace and the function
ReplaceText. I want to be able to copy all the selected
sheets to just after the last sheet in the active
selection and rename them according to the ReplaceText
criteria.


Sub Duplicate()
Dim sht As Object
frmReplace.Show

**Somehow get the active selected sheets and
**copy to just after the end of the last selected sheet.
ActiveSheets.Copy After:= **no of the last selected sheet
For Each sht In **select the sheets that were just copied
sht.Name = ReplaceText(sht.Name, frmReplace.tbFind,
frmReplace.tbReplace)
Next sht
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
Selection starting from active cell Ronald Dodge Excel Discussion (Misc queries) 3 October 10th 06 12:46 AM
Active range/selection? anita Excel Worksheet Functions 9 September 20th 06 01:35 PM
Looping a selection of rows Andre Kruger Excel Discussion (Misc queries) 1 December 15th 05 04:18 PM
Determining the Selection on a non-active Sheet? Jim S.[_4_] Excel Programming 2 April 17th 04 03:59 AM
Direction for looping through a Selection. Bob J. Excel Programming 2 September 16th 03 01:49 PM


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