Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Sheet Selection

Hello all,

I'm pretty new to all this, so go easy :P

I'm trying to write a macro that will allow me to not have to hardcode the
sheet name into the program.

Here is my code:

Sub PasteLink()

'start on Sheet 1R
Dim count As Integer
count = 0
Application.Goto Sheets("Sheet 1E").Range(Selection.Address)
Selection.Copy

If Selection.Font.ColorIndex = 3 Then
count = 1
End If

Application.Goto Sheets("Sheet 1R").Range(Selection.Address)
ActiveSheet.Paste Link:=True

With Selection.Font
.Name = "Arial"
.Size = 8
.Bold = True

If count = 1 Then
.ColorIndex = 3 'change font to red
Else: .ColorIndex = 0 'change font to black
End If

End With
End Sub

You see how I have Sheet 1E and Sheet 1R? I would like to make it so that
those are generic, and it takes the active sheet's selection then goes to the
sheet right before it, selects the same range, copies it, goes back to the
original sheet, pastes link and formats cell. The current order of sheets
a Sheet1 = "Sheet 1E"; Sheet2 = "Sheet 1R" (there are more sheets in the
workbook titled in the same fasion: Sheet 2E, Sheet 2R, Sheet 3E, Sheet 3R).
Sheet 1E and 1R are carbon copies of each other.

Any help is greatly appreciated. Also, if you could add comments so I can
learn what exactly the coding means, it would be very helpful.

Thanks,

Lance
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sheet Selection

Did you really want to apply the formatting yourself or did you want to copy the
format from the previous sheet?

I'm guessing that you wanted the format from the previous sheet:

Option Explicit
Sub myPasteLink()

Dim ActSheet As Worksheet
Dim PrevSheet As Worksheet
Dim iCtr As Long

Set ActSheet = ActiveSheet

Set PrevSheet = Nothing
For iCtr = ActSheet.Index - 1 To 1 Step -1
If TypeName(Sheets(iCtr)) = "Worksheet" Then
Set PrevSheet = Sheets(iCtr)
Exit For
End If
Next iCtr

If PrevSheet Is Nothing Then
MsgBox "No previous sheet!"
Exit Sub
End If

PrevSheet.Range(Selection.Address).Copy
ActSheet.Paste link:=True
Selection.PasteSpecial Paste:=xlPasteFormats

End Sub


Lance wrote:

Hello all,

I'm pretty new to all this, so go easy :P

I'm trying to write a macro that will allow me to not have to hardcode the
sheet name into the program.

Here is my code:

Sub PasteLink()

'start on Sheet 1R
Dim count As Integer
count = 0
Application.Goto Sheets("Sheet 1E").Range(Selection.Address)
Selection.Copy

If Selection.Font.ColorIndex = 3 Then
count = 1
End If

Application.Goto Sheets("Sheet 1R").Range(Selection.Address)
ActiveSheet.Paste Link:=True

With Selection.Font
.Name = "Arial"
.Size = 8
.Bold = True

If count = 1 Then
.ColorIndex = 3 'change font to red
Else: .ColorIndex = 0 'change font to black
End If

End With
End Sub

You see how I have Sheet 1E and Sheet 1R? I would like to make it so that
those are generic, and it takes the active sheet's selection then goes to the
sheet right before it, selects the same range, copies it, goes back to the
original sheet, pastes link and formats cell. The current order of sheets
a Sheet1 = "Sheet 1E"; Sheet2 = "Sheet 1R" (there are more sheets in the
workbook titled in the same fasion: Sheet 2E, Sheet 2R, Sheet 3E, Sheet 3R).
Sheet 1E and 1R are carbon copies of each other.

Any help is greatly appreciated. Also, if you could add comments so I can
learn what exactly the coding means, it would be very helpful.

Thanks,

Lance


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Sheet Selection

That works. Thanks Dave

"Dave Peterson" wrote:

Did you really want to apply the formatting yourself or did you want to copy the
format from the previous sheet?

I'm guessing that you wanted the format from the previous sheet:

Option Explicit
Sub myPasteLink()

Dim ActSheet As Worksheet
Dim PrevSheet As Worksheet
Dim iCtr As Long

Set ActSheet = ActiveSheet

Set PrevSheet = Nothing
For iCtr = ActSheet.Index - 1 To 1 Step -1
If TypeName(Sheets(iCtr)) = "Worksheet" Then
Set PrevSheet = Sheets(iCtr)
Exit For
End If
Next iCtr

If PrevSheet Is Nothing Then
MsgBox "No previous sheet!"
Exit Sub
End If

PrevSheet.Range(Selection.Address).Copy
ActSheet.Paste link:=True
Selection.PasteSpecial Paste:=xlPasteFormats

End Sub


Lance wrote:

Hello all,

I'm pretty new to all this, so go easy :P

I'm trying to write a macro that will allow me to not have to hardcode the
sheet name into the program.

Here is my code:

Sub PasteLink()

'start on Sheet 1R
Dim count As Integer
count = 0
Application.Goto Sheets("Sheet 1E").Range(Selection.Address)
Selection.Copy

If Selection.Font.ColorIndex = 3 Then
count = 1
End If

Application.Goto Sheets("Sheet 1R").Range(Selection.Address)
ActiveSheet.Paste Link:=True

With Selection.Font
.Name = "Arial"
.Size = 8
.Bold = True

If count = 1 Then
.ColorIndex = 3 'change font to red
Else: .ColorIndex = 0 'change font to black
End If

End With
End Sub

You see how I have Sheet 1E and Sheet 1R? I would like to make it so that
those are generic, and it takes the active sheet's selection then goes to the
sheet right before it, selects the same range, copies it, goes back to the
original sheet, pastes link and formats cell. The current order of sheets
a Sheet1 = "Sheet 1E"; Sheet2 = "Sheet 1R" (there are more sheets in the
workbook titled in the same fasion: Sheet 2E, Sheet 2R, Sheet 3E, Sheet 3R).
Sheet 1E and 1R are carbon copies of each other.

Any help is greatly appreciated. Also, if you could add comments so I can
learn what exactly the coding means, it would be very helpful.

Thanks,

Lance


--

Dave Peterson

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
Sheet selection Patrick Simonds Excel Programming 4 December 18th 06 12:35 AM
Sheet selection help Craig McK Excel Programming 6 October 30th 06 02:33 PM
Calculate active sheet on sheet selection a94andwi[_11_] Excel Programming 1 September 26th 05 05:23 PM
Selection from list on main sheet from suplemental sheet in same w Kelly New Users to Excel 1 August 12th 05 04:37 PM
Sheet selection No Name Excel Programming 1 October 6th 04 05:02 PM


All times are GMT +1. The time now is 05:00 AM.

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"