+ Reply to Thread
Results 1 to 4 of 4

Can't figure out dynamic code

  1. #1
    Registered User
    Join Date
    12-21-2016
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    7

    Question Can't figure out dynamic code

    I'm stuck at this one, which is probably easy to fix.

    So far I have:

    Sub ListSheets()

    Dim ws As Worksheet
    Dim x As Integer

    x = 1

    Sheets("Lijst medewerker").Range("A:A").Clear

    For Each ws In Worksheets
    Sheets("Lijst medewerker").Cells(x, 1) = ws.Name
    x = x + 1
    Next ws

    Dim LastRow As Long
    Dim str As String

    str = Cells(2, 1).Value

    With Sheets("Lijst medewerker")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    With .Range("B2:B" & LastRow)
    .Formula = "=VLOOKUP($B$1,'" & str & "'!C:H,6,FALSE)"

    End With

    End With

    End Sub

    Now the problem is: The VLOOKUP formula in B2:B has to change the Worksheet name (in A2:A) but keeps returning the sheet name in A2. The str part is wrong but I don't know how to fix this.
    Does anyone have a solution? I'm not an expert in VBA programming so excuse me if this code is crap so far....
    Attached Files Attached Files
    Last edited by Roeldh; 08-11-2020 at 03:34 PM. Reason: example added

  2. #2
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: Can't figure out dynamic code

    Roeldh,

    You fixed the "str" as Cells(2, 1).Value, but don't change it, so as you work down Col B it always looks at A2. Enter str=str.offset(1,0) after each pass should do it.

    Ochimus
    Last edited by Ochimus; 08-11-2020 at 11:59 AM.

  3. #3
    Registered User
    Join Date
    12-21-2016
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    7

    Re: Can't figure out dynamic code

    Thnx Ochimus for thinking with me. I'm not sure I understand where to add this. I added an simplistic example of the file I'm working on. Maybe it makes better sense of my problem.

  4. #4
    Registered User
    Join Date
    12-21-2016
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    7

    Re: Can't figure out dynamic code

    I think I figured it out. Thnx Ochimus for your tip. I used the .offset part like this:

    Sub ListSheets()

    Dim ws As Worksheet
    Dim x As Integer
    Dim LastRow As Long

    x = 1

    Sheets("Lijst medewerker").Range("A:A").Clear

    For Each ws In Worksheets
    Sheets("Lijst medewerker").Cells(x, 1) = ws.Name
    Sheets("Lijst medewerker").Cells(x, 1).Offset(0, 1) = "=VLOOKUP($B$1,'" & ws.Name & "'!C:H,6,FALSE)"
    x = x + 1
    Next ws

    End Sub

    It's not perfect yet, but it work good enough for me right now. Thanks a lot!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Trying to figure out how to code this For Loop
    By enichols in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-03-2019, 10:58 AM
  2. Auto allot assessor for dynamic figure
    By Dean Zuki in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-15-2018, 12:49 AM
  3. Need help trying to figure out what happen to my code
    By borrowed in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2018, 06:13 AM
  4. Figure to Words without VBA code
    By pavvinod in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 01-19-2015, 01:52 AM
  5. Dynamic Chart for Test Scores...can't seem to figure out
    By blackscholes in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 06-06-2014, 04:19 AM
  6. Replies: 4
    Last Post: 02-03-2014, 08:05 AM
  7. Can anyone figure this code problem please
    By simonsmith in forum Excel General
    Replies: 1
    Last Post: 05-18-2006, 03:25 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1