+ Reply to Thread
Results 1 to 15 of 15

Loop & Offset assistance required

  1. #1
    Registered User
    Join Date
    11-13-2004
    Posts
    36

    Loop & Offset assistance required

    Hi all – hoping you may be able to offer advice; apologise if numpty question but its got me flummoxed

    I have a number (>100) of excel workbooks (consistent format) on a server and I am trying to create a master spreadsheet with links to specific cells. I started using formulae based on Concatenate and Indirect functions but always had to use F2 & F9 to get a result. I am now working on a macro to build semi dynamic links, I am some way there but need some expert help please.

    In Col A I have the path and file name for all my source files - I have used the Filename *.xls & FoundFiles for this.
    In Col B is my specific file name(s) extracted from Col A using an If , Len and Text function.
    My macro declares variables; File = Range (“B#”) and Data(21) - an array of 21 specific worksheet cell references
    I then select C1 and with active.formula insert the consistent path, File and Data(1). Then C2 with path name, File and Data(2). Then C3 path name, File and Data(3). & So on for the 21 variables in the array - not elegant but it works okay

    I am now at the point where I need to move down the rows and repeat this for each source. I am wondering (hoping ) there is a simple way of offsetting this for the 100 rows / source files. I would also like to be able to set the offset repeat by counting the number of populated rows in Col A.

    The code (from memory so syntax may be a bit wonkey):

    Please Login or Register  to view this content.
    Any pointers on this much appreciated
    Hope this make sense - ta muchly D:-)
    Last edited by mudraker; 06-15-2007 at 06:28 PM.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    This should point you in the right direction

    Please Login or Register  to view this content.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    11-13-2004
    Posts
    36
    Mudraker

    Thats great - would have taken me a long time (if ever) to get there.

    Is there a way that I can make the number of repeats conditional on number of rows populated in Col A
    (where the path/file info is returned using Filename *.xls & FoundFiles)

    This would be a great help as the number of input files is dynamic &>100.

    Great forum D:-)

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    I have change the iRow as integer to l4Row as long because integers have a max value of 32,767

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-13-2004
    Posts
    36
    Mudraker

    Works a treat – brilliant
    (not sure I understand the significance of “14row” - does the 14 have any relevance )

    I have had a go at using your routine to help me build a hyperlink to my source file using the path in Col A and File Name in Col B. I would like to put the hyperlink in Col C using the filename as text. The code partly works - it inserts the hyperlink but only in one cell, the loop / repeat doesn’t seem to work.

    I would be really grateful if you would have a look see if you think I am on right track.

    Please Login or Register  to view this content.
    Many Many Thanks D:-)

  6. #6
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Hi Digory,

    I wanted to answer your 14Row question. It is actually L4Row with a small L. Mudraker declared this variable to be a Long thus the small L. This is simply a naming convention used be some programmers to help identify what type of variable they are using. It helps when you are trying to debug code.

    Not sure about the 4, but I would imagine it is because the l4Row is being used as a counter in a For loop. Just my guess, Mudraker will have to answer that one for us to be sure.

    I was working with someone's code the other day and we were looping through over 50,0000 values. The row counter had been declared as an Integer so the program crashed 32,767. Changed the declaration to a Long and everything worked fine.

    Examples
    Please Login or Register  to view this content.
    This definitely not something you have to do, but it might help your debugging efforts in the future.

    Sorry, probably more information than you were looking for.
    Sincerely,
    Jeff

  7. #7
    Registered User
    Join Date
    11-13-2004
    Posts
    36
    Jeff

    Thanks for that - not too much info at all
    Nice to pick up good practice from you guys

    D:-)

  8. #8
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    boylejob

    You are correct in your assumtions on my variable l4Row

    l is lowercase L for long type

    4 is because I use it in a for loop

    Row is because it will hold a row number

    This makes it easy for me to see at a glance what I intend or are using the variable for & helps with debugging

  9. #9
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Digory

    The following part of the for loop command looks up column A to get the last used row in column A. If this is row 1 then the for loop will only loop once.

    As you are populating column A & B witha a value I think that tha column A in the following command will need to be changed to another column or the whole command changed to a number (see the code in my 1st reply)

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    11-13-2004
    Posts
    36
    Mudraker

    Many thanks reply

    I may have confused things in the way I expanded my original question – I shall try and add some clarity

    Assume worksheet with Col A and B pre-populated for say 100 rows

    Col A is Path
    Col B is Name

    In Col C I would like to return a Hyperlink using the Path from Col A and Name from Col B for each row.

    I have attempted to adapt your routine (see first code below) but it only returns a single hyperlink to the cell that the cursor is in
    (& not just in Col C – anywhere !)

    I wondered if the problem was to do with syntax of my hyperlink command up so I tried using the formula syntax (see second code below) and this worked fine

    I am guessing that the problem is with the hyperlink rather than the row

    Grateful of your thoughts

    Doesnt work :

    Please Login or Register  to view this content.

    Does work:

    Please Login or Register  to view this content.

    Many Thanks^2 D:-)

  11. #11
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    I don't use hyperlinks so had to play around with your code & do a bit of googgling

    Is this what you were trying to achieve

    Please Login or Register  to view this content.
    A couple of things to note when declaring variables

    Certain words are used and reserved by Excel for its own use and should not be used for variable names e.g Path, Cells, Rows, Row, Columns, Date & hundreds more of them. Using them as variable names can give unexpected results

    In your code
    Please Login or Register  to view this content.
    Path is declared as a variable type & mssg is declared as a string type

    To declare them correctly you need to use As Type after each variable name.

    It is also a good idea to use the 1st letter or several letters to denote what type the variable is.

    When I declare variables I always use the following 1st letter for
    s = String
    i = Integer
    l = Long
    b = Boolean
    r = Range
    ws = Worksheets/Sheets

    This helps to eliminate coding errors in complex macros & also helps to eliminate the accidental use of reserved words

  12. #12
    Registered User
    Join Date
    11-13-2004
    Posts
    36
    Mudraker

    Thanks again - good advice on nomenclature
    (I will have to get better at this )

    Thanks also for experimenting & googgling
    I tried your routine but returned an error - "hyperlinks" is an undefined variable

    Did it work okay for you

    tvm
    D:-)

  13. #13
    Registered User
    Join Date
    11-13-2004
    Posts
    36
    Mudraker

    Have had another play - think I have cracked it now
    Needed the bit in blue :


    Please Login or Register  to view this content.
    Ta Muchly for your help
    Best forum ever
    D:-)

  14. #14
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    It works ok for me on Excel 2003 with 1 minor change

    After getting it working I modified the delcaring of variable Path to sPath but did not change it within the rest of the code

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    11-13-2004
    Posts
    36
    Mudraker

    Many Thanx

    D:-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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