Excel VBA : Loop Sheets that meets the value in the range

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wassup
    New Member
    • May 2007
    • 34

    Excel VBA : Loop Sheets that meets the value in the range

    Hey Guys,

    Good Morning. I have probelm here.

    I have 2 workbooks, 1 is Master.xls (my format to collect all the data), another 1 is Machine.xls (data collect from).

    Inside Master.Range("B 3:B5") have a data (etc: Line1, Line2, Line3), Machine.xls have a few sheets (etc: Line1, Line2, Line3....Line10 ).

    Now my concern is I want the macro loop Master.Range("B 3:B5") , if Master.Range = Line1 then it will loop Machine.xls untill found the sheets name is Line1 then collect data from it. It will loop Master.Range("B 3:B5") untill equal to blank.

    Thanks for your advice.
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    with a couple of IFS/THEN/ELSE and FOR/NEXT it can be done. To make reference to the workbooks, you can use their name or their index (the order they were open)

    Check this little example:

    [CODE=vb]If workbooks("myBo ok1").worksheet s("sheet1).rang e("B1") = workbooks("myBo ok2").worksheet s("sheet1).rang e("B1") then
    workbooks("myBo ok1").worksheet s("sheet2).rang e("A4") = "Hello World"
    else
    workbooks("myBo ok2").worksheet s("sheet2).rang e("C4") = "Goodbye Cruel World"
    end if[/CODE]

    You can use Indexes or CELLS to put anything into a loop, eg:

    [CODE=vb] for i = 1 to 5
    workbooks("myBo ok1").worksheet s(i) = "Hello World"
    next[/CODE]

    and you can always check the content or use the END method to go to the blank space, eg:

    [CODE=vb]i=1
    do
    i=i+1
    loop until cells(i,1) = ""[/CODE]

    HTH

    Comment

    • wassup
      New Member
      • May 2007
      • 34

      #3
      Originally posted by kadghar
      with a couple of IFS/THEN/ELSE and FOR/NEXT it can be done. To make reference to the workbooks, you can use their name or their index (the order they were open)

      Check this little example:

      [CODE=vb]If workbooks("myBo ok1").worksheet s("sheet1).rang e("B1") = workbooks("myBo ok2").worksheet s("sheet1).rang e("B1") then
      workbooks("myBo ok1").worksheet s("sheet2).rang e("A4") = "Hello World"
      else
      workbooks("myBo ok2").worksheet s("sheet2).rang e("C4") = "Goodbye Cruel World"
      end if[/CODE]

      You can use Indexes or CELLS to put anything into a loop, eg:

      [CODE=vb] for i = 1 to 5
      workbooks("myBo ok1").worksheet s(i) = "Hello World"
      next[/CODE]

      and you can always check the content or use the END method to go to the blank space, eg:

      [CODE=vb]i=1
      do
      i=i+1
      loop until cells(i,1) = ""[/CODE]

      HTH
      Thanks kadghar,

      Its make me many confusing because to many For, Do, Loop.... but I have done it. Have a nice day.

      Comment

      Working...