|
-
Oct 7th, 2009, 03:25 PM
#1
Thread Starter
PowerPoster
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.
-
Oct 7th, 2009, 04:35 PM
#2
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:
'~~> If start cell and last cell is known in Col A and Col H Sub LoopCells() Dim i As Long, j As Long For i = 2 To 98 '<~~ for Col A For j = 2 To 22 '<~~ for Col H If Range("A" & i).Value = Range("H" & j) Then '<~~ or whatever comparison you want to make '~~> Your code End If Next j Next i 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:
'~~> If start cell is known in Col A and Col H '~~> But end cell is not known Sub LoopCells() Dim rng1 As Range, rng2 As Range, aCl As Range, hCl As Range Dim strCol_A_addr As String, strCol_H_addr As String '~~> Get the Col A Range strCol_A_addr = Range(Range("A2"), Range("A2").End(xlDown)).Address Set rng1 = Range(strCol_A_addr) '~~> Get the Col H Range strCol_H_addr = Range(Range("H2"), Range("H2").End(xlDown)).Address Set rng2 = Range(strCol_H_addr) For Each aCl In rng1 '<~~ for Col A For Each hCl In rng2 '<~~ for Col H If aCl.Value = hCl.Value Then '<~~ or whatever comparison you want to make '~~> Your code End If Next Next 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
-
Oct 8th, 2009, 01:41 AM
#3
Thread Starter
PowerPoster
Re: loop value excel vba
hI Sid...
I use the second option... is really wath i want!
Tks.
-
Oct 8th, 2009, 01:42 AM
#4
Thread Starter
PowerPoster
Re: loop value excel vba
 Originally Posted by koolsid
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:
'~~> If start cell and last cell is known in Col A and Col H
Sub LoopCells()
Dim i As Long, j As Long
For i = 2 To 98 '<~~ for Col A
For j = 2 To 22 '<~~ for Col H
If Range("A" & i).Value = Range("H" & j) Then '<~~ or whatever comparison you want to make
'~~> Your code
End If
Next j
Next i
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:
'~~> If start cell is known in Col A and Col H
'~~> But end cell is not known
Sub LoopCells()
Dim rng1 As Range, rng2 As Range, aCl As Range, hCl As Range
Dim strCol_A_addr As String, strCol_H_addr As String
'~~> Get the Col A Range
strCol_A_addr = Range(Range("A2"), Range("A2").End(xlDown)).Address
Set rng1 = Range(strCol_A_addr)
'~~> Get the Col H Range
strCol_H_addr = Range(Range("H2"), Range("H2").End(xlDown)).Address
Set rng2 = Range(strCol_H_addr)
For Each aCl In rng1 '<~~ for Col A
For Each hCl In rng2 '<~~ for Col H
If aCl.Value = hCl.Value Then '<~~ or whatever comparison you want to make
'~~> Your code
End If
Next
Next
End Sub
Hope this helps...
hI Sid...
I use the second option... is really wath i want!
Tks.
-
Oct 8th, 2009, 02:18 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|