Return to original worksheet

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Hello, I have a WB with multiple WS's and on these sheets are a variety of userforms. Some sheets actually have more than one userform on their sheets, depending on what the sheet is for. Everything works fine, all of the userforms play together well :) so that is fine. The users have noted that they would like to send me a quick email right from the userforms without breaking out of the WB. So I whipped another userform with a couple of text boxes in it to allow the users to add in comments and their names and such, this works fine as well. The only thing that they do not like is that it moves them out of the WS that they are in when they access the comment userform. So then they have to navigate back to it.

What has got me stumped is that if I want to add a "comment" button to all of the userforms, how would I tweak the following codes so that after the email is sent you are returned to whatever worksheet you were already on; instead of the suggestion log? Basically they want to stay on the same sheet that they are working on when they call and then close out the suggestion userform. Can anyone point me in a direction to take on trying to do this? I appreciate any input.

This is my Comments code:
VBA Code:
Sub SendButton_Click()


With Sheets("Suggestions")


nextrow = .Range("A" & Rows.Count).End(xlUp).Row + 1

.Range("A" & nextrow) = Now()
.Range("B" & nextrow) = CommentBox.Value
.Range("C" & nextrow) = NameBox.Value

Worksheets("Suggestions").Activate

ActiveSheet.Range("F1:H2").Select

ActiveWorkbook.EnvelopeVisible = True

With ActiveSheet.MailEnvelope
.Introduction = " The following comment or suggestion for the Inspection App is being submitted. "
.Item.To = "username@email.com;"
.Item.Subject = "Inspection App Comment or Suggestion"
.Item.Send
End With
End With

    CommentBox.Value = ""
    NameBox.Value = ""

    Unload SuggestionEmail


End Sub

This is an example of the code that I am using to call the comment userform, I have this same code populating userforms on 9 sheets:

VBA Code:
Private Sub SendComment_Click()

    SuggestionEmail.Show


End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I have implemented a "Back" system , which allowed a user to return to the previous worksheet from anywhere in the entire workbook.
I used two "named ranges" ( just because they are saved with the workbook and continue to work after closing and reopening the workbook)
This could easily be adapted to do what you want.
VBA Code:
Private Sub CommandButton4_Click() ' this is the back button on every sheet
Call Goback
End Sub

Sub Goback()         ' this is module code
Dim str As String
Dim Lent As Integer

 str = (ActiveWorkbook.Names("prevsheet"))
 Lent = Len(str)
 str = Mid(str, 3, Lent - 3)
Worksheets(str).Activate

End Sub


Private Sub Worksheet_Activate()  ' this is in every worksheet activate 

    ActiveWorkbook.Names.Add Name:="prevsheet", RefersTo:=ActiveWorkbook.Names("currsheet")
    ActiveWorkbook.Names.Add Name:="currsheet", RefersTo:=ActiveSheet.Name
 
Upvote 0
Another option
Rich (BB code):
Sub SendButton_Click()
   Dim Ws As Worksheet
   Set Ws = ActiveSheet
   
   With Sheets("Suggestions")
   
      
      nextrow = .Range("A" & Rows.Count).End(xlUp).Row + 1
      
      .Range("A" & nextrow) = Now()
      .Range("B" & nextrow) = CommentBox.Value
      .Range("C" & nextrow) = NameBox.Value
      
      Worksheets("Suggestions").Activate
      
      ActiveSheet.Range("F1:H2").Select
      
      ActiveWorkbook.EnvelopeVisible = True
      
      With ActiveSheet.MailEnvelope
         .Introduction = " The following comment or suggestion for the Inspection App is being submitted. "
         .Item.To = "username@email.com;"
         .Item.Subject = "Inspection App Comment or Suggestion"
         .Item.Send
      End With
   End With

    CommentBox.Value = ""
    NameBox.Value = ""
    Ws.Activate
    Unload SuggestionEmail


End Sub
 
Upvote 0
Another option
Rich (BB code):
Sub SendButton_Click()
   Dim Ws As Worksheet
   Set Ws = ActiveSheet
   
   With Sheets("Suggestions")
  
     
      nextrow = .Range("A" & Rows.Count).End(xlUp).Row + 1
     
      .Range("A" & nextrow) = Now()
      .Range("B" & nextrow) = CommentBox.Value
      .Range("C" & nextrow) = NameBox.Value
     
      Worksheets("Suggestions").Activate
     
      ActiveSheet.Range("F1:H2").Select
     
      ActiveWorkbook.EnvelopeVisible = True
     
      With ActiveSheet.MailEnvelope
         .Introduction = " The following comment or suggestion for the Inspection App is being submitted. "
         .Item.To = "username@email.com;"
         .Item.Subject = "Inspection App Comment or Suggestion"
         .Item.Send
      End With
   End With

    CommentBox.Value = ""
    NameBox.Value = ""
    Ws.Activate
    Unload SuggestionEmail


End Sub
Hi Fluff, thanks for the suggestion, it worked great except for some of my userforms are modeless (I got the "can't show non-modal form when modal form is displayed" run time error, some of my forms are "calculators" so they have to stay visible, have to noodle on that one and start another thread if I have to, I appreciate the assistance.
 
Upvote 0
Hi offthelip, i am working with your suggestion now (tried the easy one first :)) it will take me a little longer to try out, I do appreciate the help.
 
Upvote 0
I have implemented a "Back" system , which allowed a user to return to the previous worksheet from anywhere in the entire workbook.
I used two "named ranges" ( just because they are saved with the workbook and continue to work after closing and reopening the workbook)
This could easily be adapted to do what you want.
VBA Code:
Private Sub CommandButton4_Click() ' this is the back button on every sheet
Call Goback
End Sub

Sub Goback()         ' this is module code
Dim str As String
Dim Lent As Integer

str = (ActiveWorkbook.Names("prevsheet"))
Lent = Len(str)
str = Mid(str, 3, Lent - 3)
Worksheets(str).Activate

End Sub


Private Sub Worksheet_Activate()  ' this is in every worksheet activate

    ActiveWorkbook.Names.Add Name:="prevsheet", RefersTo:=ActiveWorkbook.Names("currsheet")
    ActiveWorkbook.Names.Add Name:="currsheet", RefersTo:=ActiveSheet.Name
oops!
Hi offthelip, i am working with your suggestion now (tried the easy one first :)) it will take me a little longer to try out, I do appreciate the help.
 
Upvote 0
Hi Fluff and offlthelip, thanks for your help. I actually used both of your idea to get this to work. I used fluff's code for the initial WS return, but then I used offthelips input to get past my modal errors. adding some code to load and unload the userforms in the workbook activate took care of everything. I sure appreciate the assistance. ;)
 
Upvote 0
Glad you were able to sort it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,896
Messages
6,122,132
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top