0

I am attempting to pull a column of letter codes using InStr from existing text files. I would like to allow this macro to be flexible enough to allow the option to manually input the letter code being searched.

Where I am running into an issue is when common numbers are in the letter code (i.e. C4 and C45). Originally the code was written to search for 2 specific letter codes, and I was hoping to be able to replace those 2 identifiers with a link to 2 cells.

No such luck, as I keep getting responses that are not exact. Please see the code below:

Private Sub CmdLettersGetfile_Click()

Dim objFSO As Object
Dim myDir As String, myList()
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim sFolder As String
Dim fd As Office.FileDialog

Dim row As Long
Dim row1 As Long
Dim FCount As Integer
Dim FCount1 As Integer
Dim Val As String

Dim Pos As Integer
Dim Last1 As Long  'Mark the last row
Dim Start2 As Long
Dim Last2 As Long  'Mark the last row
Set fd = Application.FileDialog(msoFileDialogFilePicker)

       For Each cn In ThisWorkbook.Connections
cn.Delete
Next cn


 With ActiveSheet
     Lastrow = Sheets("MAIN").Cells(.Rows.Count, "E").End(xlUp).row
End With
'
Worksheets("REPORT").Range("A6:AA1000000").ClearContents
Worksheets("REPORT").Range("A6:AA1000000").ClearFormats
row1 = 6  'Start of REPORT ROW

For row = 12 To Lastrow
sFile = Worksheets("MAIN").Cells(row, "E").Value

 
 Pos = InStr(1, sFile, "org")
 
 If Pos = 0 Then
 Val = Worksheets("MAIN").Cells(9, "H")
 Else
 Val = Worksheets("MAIN").Cells(10, "H")
 End If
 
 Pos = 0
 


Dim strFilename As String
Dim strTextLine As String
Dim iFile As Integer

iFile = FreeFile
strFilename = folderName & "\" & sFile
Open strFilename For Input As #iFile


Last1 = 0
Last2 = 0

Do Until EOF(1)
    Line Input #1, strTextLine
    FCount = FCount
    Pos = InStr(1, strTextLine, Val)

I cleaned up some of the irrelevant code in an attempt to save space and focus on the lines where I am having the issue.

********Update (26-Apr-2018 @ 18:12 EST): When I tried to format the Val to be one uppercase letter and 2 numbers

Dim Val As String: Val = "([A-Z]{1})([0-9]{1})([0-9]{1})"

I get the following:

Resulting data

Column I is the letter code that I need to get narrowed down.

*********Update 2 (27-Apr-2018 @ 14:37 EST):

Expanded resulting data

I was able to mass redact what I needed to. I hope this offers more insight into what I'm looking to do.

Community
  • 1
  • 1
Justin F
  • 7
  • 6
  • 2
    Now looks like a good time to research *regular expressions* =) – Mathieu Guindon Apr 26 '18 at 17:03
  • That's so incredibly helpful, thx. – Justin F Apr 26 '18 at 19:00
  • [Believe it or not, it actually is](https://stackoverflow.com/a/22542835/1188513) (assuming some level of entitled sarcasm here, sorry if that's not the case). – Mathieu Guindon Apr 26 '18 at 19:04
  • And to expand, I don't even know what I need to research in order to resolve this question. THAT is why I ask for help. – Justin F Apr 26 '18 at 19:09
  • 1
    Okay, you cooled down yet? "I'm new to XYZ" is fluff indeed, just like "Hello" and "Any help appreciated", routinely removed in every single post in this site (some automatically by the system itself). "vba regular expression" is what I googled to get the above link as the first result. That's what I meant with "entitled": you're expecting others to do your research for you. FWIW I give fishing rods, not fish. No free lunch here. – Mathieu Guindon Apr 26 '18 at 19:10
  • Forgive the anger, I am extremely frustrated. I've spent 2 hours looking up something that would seem to be relatively simple. No, I haven't cooled down yet, and no I am not asking others to do the research for me. I am asking for help. I have no idea what "regular expressions" are. When I clicked on your provided link I found that information to be extremely helpful. More than I have time to sit and read right now, but I'm pretty sure it will help me when I do have the time. – Justin F Apr 26 '18 at 19:13
  • 2
    Click on the link Mathieu posted. There is no need to keep bad mouthing. He was really trying to help. I'd say give it half an hour and you will be able to sort it out yourself, if not come back @ me and I will personally help you out. Regex is extremely powerful and easy to use. – Andreas Apr 26 '18 at 19:14
  • Thank you, Andreas. I did try to include that the information Mathieu posted was helpful. I'm losing my cool, and I know that's not ok. But I have a lot of people looking to me for answers, and I'm struggling. Thank you both for your assistance. – Justin F Apr 26 '18 at 19:17
  • 1
    And, FWIW, I'm a soldier. I'd rather have someone teach me how to do something than do it for me. – Justin F Apr 26 '18 at 19:18
  • 1
    That's the spirit! Now, if you need help with the match pattern we'll need more information about your data. Do the strings *start with* `C` or just *contain* it? Can it be another letter? Is it always uppercase? Is there more to the strings after the code you want to extract? A handful of sample rows would be useful. Also it's not clear exactly where in that code you're trying to match the C42 stuff. The last line? See if you can come up with a self-contained [mcve] that reproduces the problem and thus makes this question and eventual answers helpful for others having a similar issue. Cheers! – Mathieu Guindon Apr 26 '18 at 19:43
  • @Justin I'm not a soldier and sometimes I have spent days trying to figure out a problem. Just recently I spent three full working days to figure out how to solve a VBA problem I had at work. Coding can take lots of time, and sometimes it's impossible to ask for help. But when you do have a problem you can ask for help with try to always include a few examples, both correct and incorrect examples to show the complexity of the problem. – Andreas Apr 26 '18 at 19:51
  • Not all strings will start with "C". They can have several different letters to start with, but always will have only 1 uppercase letter. Also, they will always have either 1 or 2 numbers following the letter. When I attempt to use the code above to retrieve "C1", I get anything that STARTS with a "C1", to include C10 and C15. Same when I try to pull C4. I get C4, C44, and C45. Oh, and if I leave the 2nd variable cell blank, it returns EVERYTHING. – Justin F Apr 26 '18 at 20:09
  • 2
    I'm sorry I was short and abrasive earlier. – Justin F Apr 26 '18 at 20:20
  • I see that you updated your question. I see the image, is your next challenge to remove duplicates? If not I'm not sure what you need. Your regex pattern can be simplified to `([A-Z]{1}[0-9]{1,2})` <- means match 1 capital letter and one or two 0-9 characters. – Andreas Apr 27 '18 at 03:24
  • @Andreas: Duplicates are good. They relate to an account number receiving the letter, so that is ok. I will add that simplification on the regex pattern. But to eliminate the C10/15 responses, maybe an {If} statement to say if there is only 1 # following the capital letter to only return those records? – Justin F Apr 27 '18 at 10:41
  • @JustinF A) thank you for serving (whatever country it is you serve), and B) thanks for being man enough to cool your jets, apologize for being short tempered, and come back with some more good follow-up questions. Too many people get offended and storm off in a huff. I'm by no means RegEx expert enough to help you with your problem today, but it's a good one, and someone will help you get it sorted out because you stuck through the initial struggle. – FreeMan Apr 27 '18 at 11:49
  • I still don't understand what the problem is. *C10/15 responses* ? I don't know anything about this. Can you update your question with a few input strings and what is expected to come out of them? We don't see what you see. It's hard to help when we don't have the information needed to help you. – Andreas Apr 27 '18 at 12:37
  • @Andreas: C10 and C15 are internal company codes that refer to a letter that gets mailed to a customer. The C and the number are the relevant parts. The code, so far, reads the correct column in the text files, but I need it to only return the specific letter code I am asking for. For example, I want letter C1, but not C10 or C15. Or I want letter V4, but not V45 or V46. Unfortunately I work in a financial institution and cannot display more info than I already have. – Justin F Apr 27 '18 at 15:44
  • In that case we can't help you more than we have. Keep reading up about regex and test different patterns. There are a online regex testers that can help you with direct feedback when you build the pattern. Example https://regex101.com – Andreas Apr 27 '18 at 16:43
  • Thank you for what you were able to do. I know it wasn't much to go on. It was a shot in the dark. – Justin F Apr 27 '18 at 18:30

0 Answers0