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









share|improve this question
























  • 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

















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









share|improve this question
























  • 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















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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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





share|improve this answer























  • 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


















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.






share|improve this answer



















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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





share|improve this answer























  • 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















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





share|improve this answer























  • 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













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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












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.






share|improve this answer



















  • 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















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.






share|improve this answer



















  • 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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














  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation