Consulting

Results 1 to 5 of 5

Thread: How to a reference to a new sheet in same workbook

  1. #1
    VBAX Newbie
    Joined
    Jun 2020
    Posts
    3
    Location

    Post How to a reference to a new sheet in same workbook

    Hi. Im trying to make a reference to a new sheet in the same document.
    The macro is making a new sheet and then i would like to make a reference on the first sheet to e.g. B2 in the new sheet.

    This is what I have tried to make. Can anyone help?



    Sub createNewSheet()


    'Lav et nyt ark der hedder hvad der står i B16
    sheet_name_to_create = Ark1.Range("B16").Value




    'hvis der er fejl kommer der en tekstboks op med denne fejl
    For rep = 1 To (Worksheets.Count)
    If LCase(Sheets(rep).Name) = LCase(sheet_name_to_create) Then
    MsgBox "Der er allerede en bil med dette navn"
    Exit Sub
    End If
    Next

    Sheets.Add after:=Sheets(Sheets.Count)
    Sheets(ActiveSheet.Name).Name = sheet_name_to_create

    'Indsæt alle info i arket
    Range("B3") = "Bil:"
    Range("C3") = sheet_name_to_create

    Range("B4") = "Reg nummer"
    Range("B6") = "Omkostninger"
    Range("B7") = "Købt for"
    Range("B8") = "Andre omk."
    Range("C4") = Ark1.Range("B17").Value

    Sheets("Ark1").Select


    Cells(2, Columns.Count).End(xlToLeft).Offset(0, 1).Select
    Selection = sheet_name_to_create


    ActiveCell.Offset(1, 0).Select
    ActiveCell.Offset(1, 0).Select


    Selection = ActiveWorksheet(sheet_name_to_create).Range("B2")

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    this logic might be simpler, but I'm guessing at some thing

    Don't need to .Select an object (worksheet, cell, etc.) if you can just refer to it




    Option Explicit
    
    
    Sub test()
        Dim wsNew As Worksheet
        Dim sheet_name_to_create As String
        
        sheet_name_to_create = Ark1.Range("B16").Value
    
    
        'hvis der er fejl kommer der en tekstboks op med denne fejl
        For rep = 1 To (Worksheets.Count)
            If LCase(Sheets(rep).Name) = LCase(sheet_name_to_create) Then
                MsgBox "Der er allerede en bil med dette navn"
                Exit Sub
            End If
        Next
    
    
        Worksheets.Add after:=Worksheets(Worksheets.Count)
        Set wsNew = ActiveSheet
        
        With wsNew
            .Name = sheet_name_to_create
    
    
            'Indsæt alle info i arket
            .Range("B3") = "Bil:"
            .Range("C3") = sheet_name_to_create
            
            .Range("B4") = "Reg nummer"
            .Range("B6") = "Omkostninger"
            .Range("B7") = "Købt for"
            .Range("B8") = "Andre omk."
            .Range("C4") = Ark1.Range("B17").Value
        End With
    
    
        With Ark1
            .Cells(2, .Columns.Count).End(xlToLeft).Offset(0, 1).Value = wsNew.Name
        End With
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Jun 2020
    Posts
    3
    Location
    The thing is, i would like to make several small accounting in each sheet. When I press "ny bil" button, there will come a new small accounting in that sheet.
    In this sheet there will come a result when it is done, and I need this result on the first page, that is why I need to refer to a cell in the new sheet.

    It is easy to make a reference bye myself and write "='Sheet-name'!B5", but I would like VBA to do this for me. If you understand?

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by mikkelw View Post
    The thing is, i would like to make several small accounting in each sheet. When I press "ny bil" button, there will come a new small accounting in that sheet.
    In this sheet there will come a result when it is done, and I need this result on the first page, that is why I need to refer to a cell in the new sheet.

    It is easy to make a reference bye myself and write "='Sheet-name'!B5", but I would like VBA to do this for me. If you understand?

    No, but this refers to a cell on the new sheet and puts the value on Ark1, or did you want a formula

    With Ark1
           .Cells(2, .Columns.Count).End(xlToLeft).Offset(0, 1).Value = wsNew.range("B5")
    
    
           .Cells(2, .Columns.Count).End(xlToLeft).Offset(0, 1).Formula = "=" & wsNew.Name & "!B5"
    
     End With
    Last edited by Paul_Hossler; 06-11-2020 at 01:15 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
       with sheets.add(,sheets(sheets.count))
         .name= Ark1.Range("B16").Value
         .Range("B3:C3") = array("Bil:",.name)
         .Range("B4:C4") = array("Reg nummer",Ark1.Range("B17").Value)
         .Range("B6:B8") = application.transpose(array("Omkostninger","Købt for", "Andre omk."))
         Ark1.Cells(2, Columns.Count).End(xlToLeft).Offset(, 1).value=.name
         Ark1.Cells(2, Columns.Count).End(xlToLeft).Offset(1)="='" & .name  & "'!B2"
       end with
    End Sub

Posting Permissions

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