Excel VBA Return Row number/index from Range(“rngNme”).Columns(n).Cells
up vote
1
down vote
favorite
I want to produce a mini report of several items matching a key. In my loop I get the keys returned but can't fathom out how I can access the data that I need in the report that is held in other columns.
I put in some msgbox's to trap the data and an escape mechanism to get out of the loop. These I have commented out below as well as the data lines that don't work. "cbdata" is a workbook named range covering B5:T4019. The report is being compiled on a different sheet (activesheet). For some unknown reason on looping through without outputing any data "r" gets updated to some spurious numbers like 2421 (first loop) this appears to be linked somehow to the data in "cbdata". The first entry is actually in row 2388 so it doesn't really correlate to an indexed row in the range. However, I think first of all I need to find out what I can do to get the corresponding row returned for each of my passes. "ky" returns all the entries in columns(19) but I'm only interested in those that match "ledcdeyr" which in this instance is "2012017" that bit works returning all the entries matching in the loop.
Having got the key information agreeing, how might I relate this to the row number so that I can extract the other data from that row.
(cr is vbcrlf)(r should be the row number of the receiving report)
Any pointers would be greatly appreciated.
Code:
r = r + 1 ' row 38 when entering process
For Each ky In Range("cbdata").Columns(19).Cells
'ans = MsgBox(ky & cr & r, vbOKCancel)
'If ans = vbCancel Then Exit Sub
If ky = ledcdeyr Then
ans = MsgBox(ky & cr & r, vbOKCancel)
If ans = vbCancel Then Exit Sub
Cells(r, 2) = Range("cbdata").Cells(ky, 1)
'Cells(r, 3) = Range("cbdata").Columns(2).Cells
'Cells(r, 4) = Range("cbdata").Columns(3).Cells
'Cells(r, 5) = Range("cbdata").Columns(4).Cells
'Cells(r, 6) = Range("cbdata").Columns(5).Cells
ans = MsgBox(r, vbOKCancel + vbQuestion, title)
If ans = vbCancel Then Exit Sub
End If
r = r + 1
Next
excel vba
add a comment |
up vote
1
down vote
favorite
I want to produce a mini report of several items matching a key. In my loop I get the keys returned but can't fathom out how I can access the data that I need in the report that is held in other columns.
I put in some msgbox's to trap the data and an escape mechanism to get out of the loop. These I have commented out below as well as the data lines that don't work. "cbdata" is a workbook named range covering B5:T4019. The report is being compiled on a different sheet (activesheet). For some unknown reason on looping through without outputing any data "r" gets updated to some spurious numbers like 2421 (first loop) this appears to be linked somehow to the data in "cbdata". The first entry is actually in row 2388 so it doesn't really correlate to an indexed row in the range. However, I think first of all I need to find out what I can do to get the corresponding row returned for each of my passes. "ky" returns all the entries in columns(19) but I'm only interested in those that match "ledcdeyr" which in this instance is "2012017" that bit works returning all the entries matching in the loop.
Having got the key information agreeing, how might I relate this to the row number so that I can extract the other data from that row.
(cr is vbcrlf)(r should be the row number of the receiving report)
Any pointers would be greatly appreciated.
Code:
r = r + 1 ' row 38 when entering process
For Each ky In Range("cbdata").Columns(19).Cells
'ans = MsgBox(ky & cr & r, vbOKCancel)
'If ans = vbCancel Then Exit Sub
If ky = ledcdeyr Then
ans = MsgBox(ky & cr & r, vbOKCancel)
If ans = vbCancel Then Exit Sub
Cells(r, 2) = Range("cbdata").Cells(ky, 1)
'Cells(r, 3) = Range("cbdata").Columns(2).Cells
'Cells(r, 4) = Range("cbdata").Columns(3).Cells
'Cells(r, 5) = Range("cbdata").Columns(4).Cells
'Cells(r, 6) = Range("cbdata").Columns(5).Cells
ans = MsgBox(r, vbOKCancel + vbQuestion, title)
If ans = vbCancel Then Exit Sub
End If
r = r + 1
Next
excel vba
Where is the code please?
– QHarr
Nov 10 at 21:20
Don't know it was there before posting. I'll need to look and see how I can try and post it back.
– GavRe
Nov 10 at 21:24
1
Highlight the code after pasting in and press Ctrl + K.
– QHarr
Nov 10 at 21:27
Thanks, hopefully that's done it.
– GavRe
Nov 10 at 21:31
So, you are looping a range. What are you trying to get? ky.Row will be the row number during the loop.
– QHarr
Nov 10 at 21:32
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to produce a mini report of several items matching a key. In my loop I get the keys returned but can't fathom out how I can access the data that I need in the report that is held in other columns.
I put in some msgbox's to trap the data and an escape mechanism to get out of the loop. These I have commented out below as well as the data lines that don't work. "cbdata" is a workbook named range covering B5:T4019. The report is being compiled on a different sheet (activesheet). For some unknown reason on looping through without outputing any data "r" gets updated to some spurious numbers like 2421 (first loop) this appears to be linked somehow to the data in "cbdata". The first entry is actually in row 2388 so it doesn't really correlate to an indexed row in the range. However, I think first of all I need to find out what I can do to get the corresponding row returned for each of my passes. "ky" returns all the entries in columns(19) but I'm only interested in those that match "ledcdeyr" which in this instance is "2012017" that bit works returning all the entries matching in the loop.
Having got the key information agreeing, how might I relate this to the row number so that I can extract the other data from that row.
(cr is vbcrlf)(r should be the row number of the receiving report)
Any pointers would be greatly appreciated.
Code:
r = r + 1 ' row 38 when entering process
For Each ky In Range("cbdata").Columns(19).Cells
'ans = MsgBox(ky & cr & r, vbOKCancel)
'If ans = vbCancel Then Exit Sub
If ky = ledcdeyr Then
ans = MsgBox(ky & cr & r, vbOKCancel)
If ans = vbCancel Then Exit Sub
Cells(r, 2) = Range("cbdata").Cells(ky, 1)
'Cells(r, 3) = Range("cbdata").Columns(2).Cells
'Cells(r, 4) = Range("cbdata").Columns(3).Cells
'Cells(r, 5) = Range("cbdata").Columns(4).Cells
'Cells(r, 6) = Range("cbdata").Columns(5).Cells
ans = MsgBox(r, vbOKCancel + vbQuestion, title)
If ans = vbCancel Then Exit Sub
End If
r = r + 1
Next
excel vba
I want to produce a mini report of several items matching a key. In my loop I get the keys returned but can't fathom out how I can access the data that I need in the report that is held in other columns.
I put in some msgbox's to trap the data and an escape mechanism to get out of the loop. These I have commented out below as well as the data lines that don't work. "cbdata" is a workbook named range covering B5:T4019. The report is being compiled on a different sheet (activesheet). For some unknown reason on looping through without outputing any data "r" gets updated to some spurious numbers like 2421 (first loop) this appears to be linked somehow to the data in "cbdata". The first entry is actually in row 2388 so it doesn't really correlate to an indexed row in the range. However, I think first of all I need to find out what I can do to get the corresponding row returned for each of my passes. "ky" returns all the entries in columns(19) but I'm only interested in those that match "ledcdeyr" which in this instance is "2012017" that bit works returning all the entries matching in the loop.
Having got the key information agreeing, how might I relate this to the row number so that I can extract the other data from that row.
(cr is vbcrlf)(r should be the row number of the receiving report)
Any pointers would be greatly appreciated.
Code:
r = r + 1 ' row 38 when entering process
For Each ky In Range("cbdata").Columns(19).Cells
'ans = MsgBox(ky & cr & r, vbOKCancel)
'If ans = vbCancel Then Exit Sub
If ky = ledcdeyr Then
ans = MsgBox(ky & cr & r, vbOKCancel)
If ans = vbCancel Then Exit Sub
Cells(r, 2) = Range("cbdata").Cells(ky, 1)
'Cells(r, 3) = Range("cbdata").Columns(2).Cells
'Cells(r, 4) = Range("cbdata").Columns(3).Cells
'Cells(r, 5) = Range("cbdata").Columns(4).Cells
'Cells(r, 6) = Range("cbdata").Columns(5).Cells
ans = MsgBox(r, vbOKCancel + vbQuestion, title)
If ans = vbCancel Then Exit Sub
End If
r = r + 1
Next
excel vba
excel vba
edited Nov 10 at 23:33
asked Nov 10 at 21:13
GavRe
63
63
Where is the code please?
– QHarr
Nov 10 at 21:20
Don't know it was there before posting. I'll need to look and see how I can try and post it back.
– GavRe
Nov 10 at 21:24
1
Highlight the code after pasting in and press Ctrl + K.
– QHarr
Nov 10 at 21:27
Thanks, hopefully that's done it.
– GavRe
Nov 10 at 21:31
So, you are looping a range. What are you trying to get? ky.Row will be the row number during the loop.
– QHarr
Nov 10 at 21:32
add a comment |
Where is the code please?
– QHarr
Nov 10 at 21:20
Don't know it was there before posting. I'll need to look and see how I can try and post it back.
– GavRe
Nov 10 at 21:24
1
Highlight the code after pasting in and press Ctrl + K.
– QHarr
Nov 10 at 21:27
Thanks, hopefully that's done it.
– GavRe
Nov 10 at 21:31
So, you are looping a range. What are you trying to get? ky.Row will be the row number during the loop.
– QHarr
Nov 10 at 21:32
Where is the code please?
– QHarr
Nov 10 at 21:20
Where is the code please?
– QHarr
Nov 10 at 21:20
Don't know it was there before posting. I'll need to look and see how I can try and post it back.
– GavRe
Nov 10 at 21:24
Don't know it was there before posting. I'll need to look and see how I can try and post it back.
– GavRe
Nov 10 at 21:24
1
1
Highlight the code after pasting in and press Ctrl + K.
– QHarr
Nov 10 at 21:27
Highlight the code after pasting in and press Ctrl + K.
– QHarr
Nov 10 at 21:27
Thanks, hopefully that's done it.
– GavRe
Nov 10 at 21:31
Thanks, hopefully that's done it.
– GavRe
Nov 10 at 21:31
So, you are looping a range. What are you trying to get? ky.Row will be the row number during the loop.
– QHarr
Nov 10 at 21:32
So, you are looping a range. What are you trying to get? ky.Row will be the row number during the loop.
– QHarr
Nov 10 at 21:32
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
I am not entirely sure I follow but the Range object during the loop is ky
. The row for that cell be retrieved with .Row
property
ky.Row
Somewhat random example with a conditional test:
Option Explicit
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
For Each ky In loopRange.Cells
counter = counter + 1
If ky = 1 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
Thank you QHarr. I tested out your code with some minor alterations but it produced some spurious information that I will need to look into further.
– GavRe
Nov 10 at 22:49
What are you actually after? ky.Row gives the row. I added some other lines just to produce the index where found and the cell address where found using index.
– QHarr
Nov 10 at 22:50
add a comment |
up vote
0
down vote
I ran the undernoted slightly amended code on my project but it produced some spurious results.
I had already tried the ky.row but when it didn't give me the information, I just thought that it wasn't the answer.
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
'Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
Set loopRange = Range("cbdata").Columns(19) ' workbook range name
For Each ky In loopRange.Cells
counter = counter + 1
'ans = MsgBox(counter & vbCrLf & ky & vbCrLf & ky.Row, vbOKCancel)
'If ans = vbCancel Then Exit Sub
If ky = 2012017 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
The Debug results from running the above code for the first 4 records were:
2388 2384
$CNK$5:$CNK$4091
2408 2404
$COE$5:$COE$4091
2444 2440
$CPO$5:$CPO$4091
2450 2446
$CPU$5:$CPU$4091
The numbers on the left produced by ky.row are correct. The numbers on the right relate somehow to the counter which here should be 1, 2, 3, 4. This is the same situation that occurred with my "r" and hence didn't produce the information where I expected to see it. Which caused me to think that ky.row was not working. Also my range "cbdata" is B5:T4091 The rows are correct but the "CNK" etc - I don't know where that has come from.
I thought I'd just feed back to you where I'm at and your reply certainly made me look further, as I seemed to be going round in circles.
If you have any idea how the counter would be acting so spuriously then perhaps you could let me know. Having used your code on its own the that clears up any issue with there not being a problem with any other part of my code. Thanks again.
1
This area is for answers. counter is simply letting you know which index in the range you are at. Counter = 1 is the first cell in the range. CNK is because counter is being used to index into your range to find the cell and return the address where found. So your first result was at row 2388 which was 2384 cells into your column range which makes sense given you starting point. But the return addresses do look wrong. What is the value of Debug.Print Range("cbdata").Columns(19).Address?
– QHarr
Nov 11 at 0:12
1
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post?
– GavRe
Nov 11 at 14:21
yes it is a tough one but you are right - additional info goes in question via edit.
– QHarr
Nov 11 at 14:26
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post? Run the Debug.Print Range("cbdata").Columns(19).Address Ans = $T$5:$T$4091 which is the area being searched for the key data. I don't understand your comment on the counter. Surely the counter or "r" as in my original post is controlled by me as that is signifying the receiving row for my output
– GavRe
Nov 11 at 14:30
Sorry, now realize why the counter and "r" are increasing with such magnitude. Its because it's being incremented with each pass. Obviously, if my first record in in row 2338 then there are that many increments before I get to a record i'm writing. So I'll work out how I have to handle that. Thanks again for your help - you put me in the right direction.
– GavRe
Nov 11 at 14:35
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I am not entirely sure I follow but the Range object during the loop is ky
. The row for that cell be retrieved with .Row
property
ky.Row
Somewhat random example with a conditional test:
Option Explicit
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
For Each ky In loopRange.Cells
counter = counter + 1
If ky = 1 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
Thank you QHarr. I tested out your code with some minor alterations but it produced some spurious information that I will need to look into further.
– GavRe
Nov 10 at 22:49
What are you actually after? ky.Row gives the row. I added some other lines just to produce the index where found and the cell address where found using index.
– QHarr
Nov 10 at 22:50
add a comment |
up vote
1
down vote
I am not entirely sure I follow but the Range object during the loop is ky
. The row for that cell be retrieved with .Row
property
ky.Row
Somewhat random example with a conditional test:
Option Explicit
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
For Each ky In loopRange.Cells
counter = counter + 1
If ky = 1 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
Thank you QHarr. I tested out your code with some minor alterations but it produced some spurious information that I will need to look into further.
– GavRe
Nov 10 at 22:49
What are you actually after? ky.Row gives the row. I added some other lines just to produce the index where found and the cell address where found using index.
– QHarr
Nov 10 at 22:50
add a comment |
up vote
1
down vote
up vote
1
down vote
I am not entirely sure I follow but the Range object during the loop is ky
. The row for that cell be retrieved with .Row
property
ky.Row
Somewhat random example with a conditional test:
Option Explicit
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
For Each ky In loopRange.Cells
counter = counter + 1
If ky = 1 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
I am not entirely sure I follow but the Range object during the loop is ky
. The row for that cell be retrieved with .Row
property
ky.Row
Somewhat random example with a conditional test:
Option Explicit
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
For Each ky In loopRange.Cells
counter = counter + 1
If ky = 1 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
edited Nov 10 at 21:46
answered Nov 10 at 21:35
QHarr
26k81839
26k81839
Thank you QHarr. I tested out your code with some minor alterations but it produced some spurious information that I will need to look into further.
– GavRe
Nov 10 at 22:49
What are you actually after? ky.Row gives the row. I added some other lines just to produce the index where found and the cell address where found using index.
– QHarr
Nov 10 at 22:50
add a comment |
Thank you QHarr. I tested out your code with some minor alterations but it produced some spurious information that I will need to look into further.
– GavRe
Nov 10 at 22:49
What are you actually after? ky.Row gives the row. I added some other lines just to produce the index where found and the cell address where found using index.
– QHarr
Nov 10 at 22:50
Thank you QHarr. I tested out your code with some minor alterations but it produced some spurious information that I will need to look into further.
– GavRe
Nov 10 at 22:49
Thank you QHarr. I tested out your code with some minor alterations but it produced some spurious information that I will need to look into further.
– GavRe
Nov 10 at 22:49
What are you actually after? ky.Row gives the row. I added some other lines just to produce the index where found and the cell address where found using index.
– QHarr
Nov 10 at 22:50
What are you actually after? ky.Row gives the row. I added some other lines just to produce the index where found and the cell address where found using index.
– QHarr
Nov 10 at 22:50
add a comment |
up vote
0
down vote
I ran the undernoted slightly amended code on my project but it produced some spurious results.
I had already tried the ky.row but when it didn't give me the information, I just thought that it wasn't the answer.
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
'Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
Set loopRange = Range("cbdata").Columns(19) ' workbook range name
For Each ky In loopRange.Cells
counter = counter + 1
'ans = MsgBox(counter & vbCrLf & ky & vbCrLf & ky.Row, vbOKCancel)
'If ans = vbCancel Then Exit Sub
If ky = 2012017 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
The Debug results from running the above code for the first 4 records were:
2388 2384
$CNK$5:$CNK$4091
2408 2404
$COE$5:$COE$4091
2444 2440
$CPO$5:$CPO$4091
2450 2446
$CPU$5:$CPU$4091
The numbers on the left produced by ky.row are correct. The numbers on the right relate somehow to the counter which here should be 1, 2, 3, 4. This is the same situation that occurred with my "r" and hence didn't produce the information where I expected to see it. Which caused me to think that ky.row was not working. Also my range "cbdata" is B5:T4091 The rows are correct but the "CNK" etc - I don't know where that has come from.
I thought I'd just feed back to you where I'm at and your reply certainly made me look further, as I seemed to be going round in circles.
If you have any idea how the counter would be acting so spuriously then perhaps you could let me know. Having used your code on its own the that clears up any issue with there not being a problem with any other part of my code. Thanks again.
1
This area is for answers. counter is simply letting you know which index in the range you are at. Counter = 1 is the first cell in the range. CNK is because counter is being used to index into your range to find the cell and return the address where found. So your first result was at row 2388 which was 2384 cells into your column range which makes sense given you starting point. But the return addresses do look wrong. What is the value of Debug.Print Range("cbdata").Columns(19).Address?
– QHarr
Nov 11 at 0:12
1
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post?
– GavRe
Nov 11 at 14:21
yes it is a tough one but you are right - additional info goes in question via edit.
– QHarr
Nov 11 at 14:26
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post? Run the Debug.Print Range("cbdata").Columns(19).Address Ans = $T$5:$T$4091 which is the area being searched for the key data. I don't understand your comment on the counter. Surely the counter or "r" as in my original post is controlled by me as that is signifying the receiving row for my output
– GavRe
Nov 11 at 14:30
Sorry, now realize why the counter and "r" are increasing with such magnitude. Its because it's being incremented with each pass. Obviously, if my first record in in row 2338 then there are that many increments before I get to a record i'm writing. So I'll work out how I have to handle that. Thanks again for your help - you put me in the right direction.
– GavRe
Nov 11 at 14:35
add a comment |
up vote
0
down vote
I ran the undernoted slightly amended code on my project but it produced some spurious results.
I had already tried the ky.row but when it didn't give me the information, I just thought that it wasn't the answer.
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
'Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
Set loopRange = Range("cbdata").Columns(19) ' workbook range name
For Each ky In loopRange.Cells
counter = counter + 1
'ans = MsgBox(counter & vbCrLf & ky & vbCrLf & ky.Row, vbOKCancel)
'If ans = vbCancel Then Exit Sub
If ky = 2012017 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
The Debug results from running the above code for the first 4 records were:
2388 2384
$CNK$5:$CNK$4091
2408 2404
$COE$5:$COE$4091
2444 2440
$CPO$5:$CPO$4091
2450 2446
$CPU$5:$CPU$4091
The numbers on the left produced by ky.row are correct. The numbers on the right relate somehow to the counter which here should be 1, 2, 3, 4. This is the same situation that occurred with my "r" and hence didn't produce the information where I expected to see it. Which caused me to think that ky.row was not working. Also my range "cbdata" is B5:T4091 The rows are correct but the "CNK" etc - I don't know where that has come from.
I thought I'd just feed back to you where I'm at and your reply certainly made me look further, as I seemed to be going round in circles.
If you have any idea how the counter would be acting so spuriously then perhaps you could let me know. Having used your code on its own the that clears up any issue with there not being a problem with any other part of my code. Thanks again.
1
This area is for answers. counter is simply letting you know which index in the range you are at. Counter = 1 is the first cell in the range. CNK is because counter is being used to index into your range to find the cell and return the address where found. So your first result was at row 2388 which was 2384 cells into your column range which makes sense given you starting point. But the return addresses do look wrong. What is the value of Debug.Print Range("cbdata").Columns(19).Address?
– QHarr
Nov 11 at 0:12
1
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post?
– GavRe
Nov 11 at 14:21
yes it is a tough one but you are right - additional info goes in question via edit.
– QHarr
Nov 11 at 14:26
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post? Run the Debug.Print Range("cbdata").Columns(19).Address Ans = $T$5:$T$4091 which is the area being searched for the key data. I don't understand your comment on the counter. Surely the counter or "r" as in my original post is controlled by me as that is signifying the receiving row for my output
– GavRe
Nov 11 at 14:30
Sorry, now realize why the counter and "r" are increasing with such magnitude. Its because it's being incremented with each pass. Obviously, if my first record in in row 2338 then there are that many increments before I get to a record i'm writing. So I'll work out how I have to handle that. Thanks again for your help - you put me in the right direction.
– GavRe
Nov 11 at 14:35
add a comment |
up vote
0
down vote
up vote
0
down vote
I ran the undernoted slightly amended code on my project but it produced some spurious results.
I had already tried the ky.row but when it didn't give me the information, I just thought that it wasn't the answer.
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
'Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
Set loopRange = Range("cbdata").Columns(19) ' workbook range name
For Each ky In loopRange.Cells
counter = counter + 1
'ans = MsgBox(counter & vbCrLf & ky & vbCrLf & ky.Row, vbOKCancel)
'If ans = vbCancel Then Exit Sub
If ky = 2012017 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
The Debug results from running the above code for the first 4 records were:
2388 2384
$CNK$5:$CNK$4091
2408 2404
$COE$5:$COE$4091
2444 2440
$CPO$5:$CPO$4091
2450 2446
$CPU$5:$CPU$4091
The numbers on the left produced by ky.row are correct. The numbers on the right relate somehow to the counter which here should be 1, 2, 3, 4. This is the same situation that occurred with my "r" and hence didn't produce the information where I expected to see it. Which caused me to think that ky.row was not working. Also my range "cbdata" is B5:T4091 The rows are correct but the "CNK" etc - I don't know where that has come from.
I thought I'd just feed back to you where I'm at and your reply certainly made me look further, as I seemed to be going round in circles.
If you have any idea how the counter would be acting so spuriously then perhaps you could let me know. Having used your code on its own the that clears up any issue with there not being a problem with any other part of my code. Thanks again.
I ran the undernoted slightly amended code on my project but it produced some spurious results.
I had already tried the ky.row but when it didn't give me the information, I just thought that it wasn't the answer.
Public Sub Test()
Dim ky As Range, counter As Long
Dim loopRange As Range
'Set loopRange = ThisWorkbook.Worksheets("Sheet1").Range("cbdata").Columns(19)
Set loopRange = Range("cbdata").Columns(19) ' workbook range name
For Each ky In loopRange.Cells
counter = counter + 1
'ans = MsgBox(counter & vbCrLf & ky & vbCrLf & ky.Row, vbOKCancel)
'If ans = vbCancel Then Exit Sub
If ky = 2012017 Then
Debug.Print ky.Row, counter
Debug.Print loopRange(counter).Address
End If
Next
End Sub
The Debug results from running the above code for the first 4 records were:
2388 2384
$CNK$5:$CNK$4091
2408 2404
$COE$5:$COE$4091
2444 2440
$CPO$5:$CPO$4091
2450 2446
$CPU$5:$CPU$4091
The numbers on the left produced by ky.row are correct. The numbers on the right relate somehow to the counter which here should be 1, 2, 3, 4. This is the same situation that occurred with my "r" and hence didn't produce the information where I expected to see it. Which caused me to think that ky.row was not working. Also my range "cbdata" is B5:T4091 The rows are correct but the "CNK" etc - I don't know where that has come from.
I thought I'd just feed back to you where I'm at and your reply certainly made me look further, as I seemed to be going round in circles.
If you have any idea how the counter would be acting so spuriously then perhaps you could let me know. Having used your code on its own the that clears up any issue with there not being a problem with any other part of my code. Thanks again.
edited Nov 10 at 23:30
answered Nov 10 at 23:19
GavRe
63
63
1
This area is for answers. counter is simply letting you know which index in the range you are at. Counter = 1 is the first cell in the range. CNK is because counter is being used to index into your range to find the cell and return the address where found. So your first result was at row 2388 which was 2384 cells into your column range which makes sense given you starting point. But the return addresses do look wrong. What is the value of Debug.Print Range("cbdata").Columns(19).Address?
– QHarr
Nov 11 at 0:12
1
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post?
– GavRe
Nov 11 at 14:21
yes it is a tough one but you are right - additional info goes in question via edit.
– QHarr
Nov 11 at 14:26
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post? Run the Debug.Print Range("cbdata").Columns(19).Address Ans = $T$5:$T$4091 which is the area being searched for the key data. I don't understand your comment on the counter. Surely the counter or "r" as in my original post is controlled by me as that is signifying the receiving row for my output
– GavRe
Nov 11 at 14:30
Sorry, now realize why the counter and "r" are increasing with such magnitude. Its because it's being incremented with each pass. Obviously, if my first record in in row 2338 then there are that many increments before I get to a record i'm writing. So I'll work out how I have to handle that. Thanks again for your help - you put me in the right direction.
– GavRe
Nov 11 at 14:35
add a comment |
1
This area is for answers. counter is simply letting you know which index in the range you are at. Counter = 1 is the first cell in the range. CNK is because counter is being used to index into your range to find the cell and return the address where found. So your first result was at row 2388 which was 2384 cells into your column range which makes sense given you starting point. But the return addresses do look wrong. What is the value of Debug.Print Range("cbdata").Columns(19).Address?
– QHarr
Nov 11 at 0:12
1
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post?
– GavRe
Nov 11 at 14:21
yes it is a tough one but you are right - additional info goes in question via edit.
– QHarr
Nov 11 at 14:26
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post? Run the Debug.Print Range("cbdata").Columns(19).Address Ans = $T$5:$T$4091 which is the area being searched for the key data. I don't understand your comment on the counter. Surely the counter or "r" as in my original post is controlled by me as that is signifying the receiving row for my output
– GavRe
Nov 11 at 14:30
Sorry, now realize why the counter and "r" are increasing with such magnitude. Its because it's being incremented with each pass. Obviously, if my first record in in row 2338 then there are that many increments before I get to a record i'm writing. So I'll work out how I have to handle that. Thanks again for your help - you put me in the right direction.
– GavRe
Nov 11 at 14:35
1
1
This area is for answers. counter is simply letting you know which index in the range you are at. Counter = 1 is the first cell in the range. CNK is because counter is being used to index into your range to find the cell and return the address where found. So your first result was at row 2388 which was 2384 cells into your column range which makes sense given you starting point. But the return addresses do look wrong. What is the value of Debug.Print Range("cbdata").Columns(19).Address?
– QHarr
Nov 11 at 0:12
This area is for answers. counter is simply letting you know which index in the range you are at. Counter = 1 is the first cell in the range. CNK is because counter is being used to index into your range to find the cell and return the address where found. So your first result was at row 2388 which was 2384 cells into your column range which makes sense given you starting point. But the return addresses do look wrong. What is the value of Debug.Print Range("cbdata").Columns(19).Address?
– QHarr
Nov 11 at 0:12
1
1
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post?
– GavRe
Nov 11 at 14:21
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post?
– GavRe
Nov 11 at 14:21
yes it is a tough one but you are right - additional info goes in question via edit.
– QHarr
Nov 11 at 14:26
yes it is a tough one but you are right - additional info goes in question via edit.
– QHarr
Nov 11 at 14:26
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post? Run the Debug.Print Range("cbdata").Columns(19).Address Ans = $T$5:$T$4091 which is the area being searched for the key data. I don't understand your comment on the counter. Surely the counter or "r" as in my original post is controlled by me as that is signifying the receiving row for my output
– GavRe
Nov 11 at 14:30
I realize now that this area is for answers but couldn't see where to provide this type of feedback, presumably, I would just edit the original post? Run the Debug.Print Range("cbdata").Columns(19).Address Ans = $T$5:$T$4091 which is the area being searched for the key data. I don't understand your comment on the counter. Surely the counter or "r" as in my original post is controlled by me as that is signifying the receiving row for my output
– GavRe
Nov 11 at 14:30
Sorry, now realize why the counter and "r" are increasing with such magnitude. Its because it's being incremented with each pass. Obviously, if my first record in in row 2338 then there are that many increments before I get to a record i'm writing. So I'll work out how I have to handle that. Thanks again for your help - you put me in the right direction.
– GavRe
Nov 11 at 14:35
Sorry, now realize why the counter and "r" are increasing with such magnitude. Its because it's being incremented with each pass. Obviously, if my first record in in row 2338 then there are that many increments before I get to a record i'm writing. So I'll work out how I have to handle that. Thanks again for your help - you put me in the right direction.
– GavRe
Nov 11 at 14:35
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243462%2fexcel-vba-return-row-number-index-from-rangerngnme-columnsn-cells%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where is the code please?
– QHarr
Nov 10 at 21:20
Don't know it was there before posting. I'll need to look and see how I can try and post it back.
– GavRe
Nov 10 at 21:24
1
Highlight the code after pasting in and press Ctrl + K.
– QHarr
Nov 10 at 21:27
Thanks, hopefully that's done it.
– GavRe
Nov 10 at 21:31
So, you are looping a range. What are you trying to get? ky.Row will be the row number during the loop.
– QHarr
Nov 10 at 21:32