Results 1 to 5 of 5

Thread: loop value excel vba

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,941

    loop value excel vba

    Please a fast method to loop value in column from other value in other column...

    Admit in comun A (started from A2) have 98 filled cells.
    Admit in comun H (started from H2) have 22 filled cells.

    How to loop the first value of A2 in column A to all values in column H, How to to loop the first value of A3 in column A to all values in column H... ecc do until in column A the last cell is blank

    Tks.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: loop value excel vba

    Two Scenarios

    1) If you know the start cell and end cell of Col A and Col H

    for example A2:A98 and H2:H22 then use this code...

    vb Code:
    1. '~~> If start cell and last cell is known in Col A and Col H
    2. Sub LoopCells()
    3.     Dim i As Long, j As Long
    4.     For i = 2 To 98 '<~~ for Col A
    5.         For j = 2 To 22 '<~~ for Col H
    6.             If Range("A" & i).Value = Range("H" & j) Then '<~~ or whatever comparison you want to make
    7.                 '~~> Your code
    8.             End If
    9.         Next j
    10.     Next i
    11. End Sub

    2) If you know the start cell but not the end cell of Col A and Col H

    for example A2:A?? and H2:H?? then use this code...

    vb Code:
    1. '~~> If start cell is known in Col A and Col H
    2. '~~> But end cell is not known
    3. Sub LoopCells()
    4.     Dim rng1 As Range, rng2 As Range, aCl As Range, hCl As Range
    5.     Dim strCol_A_addr As String, strCol_H_addr As String
    6.    
    7.     '~~> Get the Col A Range
    8.     strCol_A_addr = Range(Range("A2"), Range("A2").End(xlDown)).Address
    9.     Set rng1 = Range(strCol_A_addr)
    10.    
    11.     '~~> Get the Col H Range
    12.     strCol_H_addr = Range(Range("H2"), Range("H2").End(xlDown)).Address
    13.     Set rng2 = Range(strCol_H_addr)
    14.    
    15.     For Each aCl In rng1  '<~~ for Col A
    16.         For Each hCl In rng2  '<~~ for Col H
    17.             If aCl.Value = hCl.Value Then '<~~ or whatever comparison you want to make
    18.                 '~~> Your code
    19.             End If
    20.         Next
    21.     Next
    22. End Sub

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,941

    Re: loop value excel vba

    hI Sid...
    I use the second option... is really wath i want!
    Tks.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,941

    Re: loop value excel vba

    Quote Originally Posted by koolsid View Post
    Two Scenarios

    1) If you know the start cell and end cell of Col A and Col H

    for example A2:A98 and H2:H22 then use this code...

    vb Code:
    1. '~~> If start cell and last cell is known in Col A and Col H
    2. Sub LoopCells()
    3.     Dim i As Long, j As Long
    4.     For i = 2 To 98 '<~~ for Col A
    5.         For j = 2 To 22 '<~~ for Col H
    6.             If Range("A" & i).Value = Range("H" & j) Then '<~~ or whatever comparison you want to make
    7.                 '~~> Your code
    8.             End If
    9.         Next j
    10.     Next i
    11. End Sub

    2) If you know the start cell but not the end cell of Col A and Col H

    for example A2:A?? and H2:H?? then use this code...

    vb Code:
    1. '~~> If start cell is known in Col A and Col H
    2. '~~> But end cell is not known
    3. Sub LoopCells()
    4.     Dim rng1 As Range, rng2 As Range, aCl As Range, hCl As Range
    5.     Dim strCol_A_addr As String, strCol_H_addr As String
    6.    
    7.     '~~> Get the Col A Range
    8.     strCol_A_addr = Range(Range("A2"), Range("A2").End(xlDown)).Address
    9.     Set rng1 = Range(strCol_A_addr)
    10.    
    11.     '~~> Get the Col H Range
    12.     strCol_H_addr = Range(Range("H2"), Range("H2").End(xlDown)).Address
    13.     Set rng2 = Range(strCol_H_addr)
    14.    
    15.     For Each aCl In rng1  '<~~ for Col A
    16.         For Each hCl In rng2  '<~~ for Col H
    17.             If aCl.Value = hCl.Value Then '<~~ or whatever comparison you want to make
    18.                 '~~> Your code
    19.             End If
    20.         Next
    21.     Next
    22. End Sub

    Hope this helps...
    hI Sid...
    I use the second option... is really wath i want!
    Tks.

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: loop value excel vba

    Well, if your query is solved then do remember to come back and mark this thread resolved
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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