Results 1 to 4 of 4

Thread: VBA Code: Referencing a cell in a seperate sheet dynamically

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    13

    VBA Code: Referencing a cell in a seperate sheet dynamically

    Hey everyone I'll try and keep this short. Basically I'm trying to create a Macro that wil create a summary sheet that I can re-use multiple times and I'm having trouble referencing the cells in the other workbooks.

    I've bolded the particular bit of code giving me trouble:
    If x = 1 Then N = N - 1
    For i = 2 To N
    With Sheets("Summary Sheet")
    shtName = Sheets(i).Name
    'Add a hyperlink to A1 of each sheet.
    .Range("B" & nRow).Hyperlinks.Add _
    Anchor:=.Range("B" & nRow), Address:="#'" & _
    shtName & "'!A1", TextToDisplay:=shtName
    .Range("B" & nRow).HorizontalAlignment = xlLeft
    .Range("A" & nRow).Value = nRow - 4
    .Range("C" & nRow).Value = "='shtName'!$D$5"
    nRow = nRow + 1
    End With

    If you were to click on a cell in the 'C' column I want it to read "='WhatEverThisParticularSheetsNameIs'!$D$5 . My problem is that I'm using a variable I've defined as 'shtName' instead of going through and writting in each seperate sheets name. When I include shtName in the quotes it puts it as plane text rather than using the variable.

    Any help would be appreciated! Keep in mind I'm pretty bad at VBA haha.

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: VBA Code: Referencing a cell in a seperate sheet dynamically

    you could do something like this (you may want to dim a workbook variable first to avoid confusion):

    Code:
    dim shtName as worksheet
    set shtName=worksheets("whatever your sheet is named.xls")
    .range("c" & nRow).value=shtName.range("d5").value

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Code: Referencing a cell in a seperate sheet dynamically

    try like
    Code:
    .Range("C" & nRow).formula = "='" & shtName & "'!$D$5"
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    13

    Re: VBA Code: Referencing a cell in a seperate sheet dynamically

    @westconn1

    Thanks that worked perfectly!

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width