zookeepertx
Well-known Member
- Joined
- May 27, 2011
- Messages
- 589
- Office Version
- 365
- Platform
- Windows
Hello all!
I have a short question that is probably simple but I just can't see it.
I have a macro that works correctly; I just can't figure out why it does. Someone else wrote parts of the code for me; that's why I'm having trouble.
The data being evaluated is quite a number of rows; way too many to do manually.
In this example, the cell that the code is looking at shows this: Vendor ID: 21069,Vendor: 19 RM Limited USD
I need it to end up like this: 21069-19 RM Limited USD. It works but I can't tell how it does.
Here's the code for that portion of the macro:
So, I've figured out the first part, that gets the first value of "vendor" (although I don't know what the $ does in there):
Trim$(Mid$(temp, InStr(LCase(temp), "vendor id:") + Len("vendor id:"), InStr(temp, ",")- InStr(LCase(temp), "vendor id:") - Len("vendor id:")))
temp = the text in the cell
InStr(LCase(temp), "vendor id:") + Len("vendor id:") = Finds the start number. Position of "vendor id:" which is 1, plus the length of "vendor id:" which is 11. This makes the start number = 12.
InStr(temp, ",")- InStr(LCase(temp), "vendor id:") - Len("vendor id:"))) = Number of characters. I'm not TOTALLY clear on this one. I know what each bit means, individually: first is the position of the comma, which is 17, then the position of "vendor id", which is 1, then the length of "vendor id:" which is 11.
It does give the correct answer = 5, but what I don't get is why we're subtracting each of those last 2 from the 1st one - position of the comma.
Anyway, given just the math, "vendor" = 21069. What gives me trouble isn't the math part, it's the verbiage part. This equates to a word problem to me and word problems are the absolute bane of my existence!! In fact, I've already spent about 45 minutes just trying to make this question make even a little bit of sense to you guys!
The next step is where I really get in trouble:
vendor = vendor & "-" & Trim$(Mid$(temp, InStr(LCase(temp), "vendor:") + Len("vendor:")))
vendor is obviously the 21069 found previously and we're adding a dash after it.
temp = still the text in the cell
InStr(LCase(temp), "vendor:") = Finds the start number, which is 17.
Len("vendor id:"))) = obviously the # of characters in "vendor id:" which is 10
First off, with the MID formula, doesn't there need to be a part of the formula giving the number of characters? I thought that was required!
Secondly, why is it adding together the 17 + 10? The correct # of characters to pull should be 17 - 19 RM Limited USD
When I run the macro, it gives the correct answer every time; I just want to know HOW it works, in case I need to borrow the code for another macro.
Anyway, if anyone is still reading this novel, I'd love it if you could tell me how/why this works, in plain english, like you're talking to an idiot, because sometimes I am.
I have a short question that is probably simple but I just can't see it.
I have a macro that works correctly; I just can't figure out why it does. Someone else wrote parts of the code for me; that's why I'm having trouble.
The data being evaluated is quite a number of rows; way too many to do manually.
In this example, the cell that the code is looking at shows this: Vendor ID: 21069,Vendor: 19 RM Limited USD
I need it to end up like this: 21069-19 RM Limited USD. It works but I can't tell how it does.
Here's the code for that portion of the macro:
VBA Code:
For i = 1 To lr
If LCase(Range("B" & i).Value) Like "*vendor id:*,*vendor:*" Then
temp = Range("B" & i).Value
vendor = Trim$(Mid$(temp, InStr(LCase(temp), "vendor id:") + Len("vendor id:"), InStr(temp, ",") _
- InStr(LCase(temp), "vendor id:") - Len("vendor id:")))
vendor = vendor & "-" & Trim$(Mid$(temp, InStr(LCase(temp), "vendor:") + Len("vendor:")))
End If
Next i
Trim$(Mid$(temp, InStr(LCase(temp), "vendor id:") + Len("vendor id:"), InStr(temp, ",")- InStr(LCase(temp), "vendor id:") - Len("vendor id:")))
temp = the text in the cell
InStr(LCase(temp), "vendor id:") + Len("vendor id:") = Finds the start number. Position of "vendor id:" which is 1, plus the length of "vendor id:" which is 11. This makes the start number = 12.
InStr(temp, ",")- InStr(LCase(temp), "vendor id:") - Len("vendor id:"))) = Number of characters. I'm not TOTALLY clear on this one. I know what each bit means, individually: first is the position of the comma, which is 17, then the position of "vendor id", which is 1, then the length of "vendor id:" which is 11.
It does give the correct answer = 5, but what I don't get is why we're subtracting each of those last 2 from the 1st one - position of the comma.
Anyway, given just the math, "vendor" = 21069. What gives me trouble isn't the math part, it's the verbiage part. This equates to a word problem to me and word problems are the absolute bane of my existence!! In fact, I've already spent about 45 minutes just trying to make this question make even a little bit of sense to you guys!
The next step is where I really get in trouble:
vendor = vendor & "-" & Trim$(Mid$(temp, InStr(LCase(temp), "vendor:") + Len("vendor:")))
vendor is obviously the 21069 found previously and we're adding a dash after it.
temp = still the text in the cell
InStr(LCase(temp), "vendor:") = Finds the start number, which is 17.
Len("vendor id:"))) = obviously the # of characters in "vendor id:" which is 10
First off, with the MID formula, doesn't there need to be a part of the formula giving the number of characters? I thought that was required!
Secondly, why is it adding together the 17 + 10? The correct # of characters to pull should be 17 - 19 RM Limited USD
When I run the macro, it gives the correct answer every time; I just want to know HOW it works, in case I need to borrow the code for another macro.
Anyway, if anyone is still reading this novel, I'd love it if you could tell me how/why this works, in plain english, like you're talking to an idiot, because sometimes I am.