Consulting

Results 1 to 7 of 7

Thread: VBA Command

  1. #1

    VBA Command

    I have created a Quote Tool with Excel VBA.
    when I open it, it is prompting for a project name and then create a sheet with that name from a template.
    On the new created sheet, I have a button to go to a sheet called Total.
    When I am on the sheet Total, I would like to create a button to go back to new created sheet which has the new project name.
    Since this project name changes everytime I open the application, the button has to reference to this new sheet.
    How do I create a macro to go back to the said sheet.
    Thanks
    Rick

  2. #2
    Is there a cell in a sheet that has that particular project name?
    Or, where is the new sheet placed? At the end? At the beginning? Following a certain sheet?
    If the project sheet is added to the end, this should do.
    Sub GoTo_Project()
        ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Select
    End Sub
    If the project name (same as sheet name) is in Cell B2 of Sheet1.
    Sub GoTo_Project()
    Dim shName As String
    shName = Sheets("Sheet1").Cells(2, 2).Value    '<---- Change as required
    ThisWorkbook.Sheets(shName).Select
    End Sub
    Last edited by jolivanes; 05-01-2021 at 03:15 PM.

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    In the Template, (soon to be the new Project Sheet,) Code page
    Option Explicit
    
    Private Sub Worksheet_Activate()
    'Because  ProjectName will be erased when Excel Closes
        If Me.Name <> "Template" And ProjectName = "" Then ProjectName = Me.Name
    End Sub
    
    Sub Button_Click
        Sheets("Total").Activate
    End Sub
    In a Module
    Option Explicit
    
    Public ProjectName As String
    On the Totals Code page
    Option Explicit
    
    Sub Button_Click()
        If ProjectName <> "" Then 
           Sheets(ProjectName).Activate
       Else: MsgBox "You must Activate the Project sheet once before this will work"
       End If
    End Sub
    In the Sub that copies the Template to a new sheet
    'Set the value of the Public Variable
    ProjectName = InputBox("Select Name of Project")
    Sheets("Template").Copy Name = ProjectName
    All that is just one way to keep the last activated sheet in memory. This version is just for one specific sheet, the one named "ProjectName"
    Last edited by SamT; 05-01-2021 at 05:50 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4

    Thanks jolivanes and SamT.

    Quote Originally Posted by jolivanes View Post
    Is there a cell in a sheet that has that particular project name?
    Or, where is the new sheet placed? At the end? At the beginning? Following a certain sheet?
    If the project sheet is added to the end, this should do.
    Sub GoTo_Project()
        ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Select
    End Sub
    If the project name (same as sheet name) is in Cell B2 of Sheet1.
    Sub GoTo_Project()
    Dim shName As String
    shName = Sheets("Sheet1").Cells(2, 2).Value    '<---- Change as required
    ThisWorkbook.Sheets(shName).Select
    End Sub

  5. #5
    Everything under control now.
    Thanks to you, I realized what I was doing wrong.
    Richard

  6. #6
    Thanks for letting us know and good luck

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •