Convert If InStr Excel VBA to Access Expression?

rhaas128

Board Regular
Joined
Jul 5, 2013
Messages
84
I have the below code in Excel VBA. I was wanting to try and put this into Access as an expression perhaps? The goal is to look at each record in the table and parse out the Access Profile Name. Anyone able to help me out?

Code:
Dim R As Long, Data As Variant
  Data = Range("G2", Cells(Rows.Count, "G").End(xlUp))
  For R = 1 To UBound(Data)
    If InStr(Data(R, 1), "Access Profile Name : ") Then
      Data(R, 1) = Split(Split(Data(R, 1), "Access Profile Name : ", , vbTextCompare)(1), vbLf)(0)
    Else
      Data(R, 1) = "AP Name Unknown"
    End If
  Next
  Range("J2").Resize(UBound(<wbr>Data)) = Data
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Can you post a few example of sample data with expected output?
 
Upvote 0
Can you post a few example of sample data with expected output?
DescriptionExpected Output
Request: NeededAccess
Access Number : 01
Access Profile Name : ANAME




Business Justification: New hire for AKFE.
ANAME
re-add previous accessNo Match
Line 1 Blah
Line 2 Blah


Request: NeededAccess
Access Number : 01
Access Profile Name : ANAME




Business Justification: New hire for AKFE.
ANAME

<tbody>
</tbody>
 
Last edited:
Upvote 0
So, are you going to have the whole entire block of text like that in a single field?
Are there soft-carriage returns in there like that?
What is the Data Type of the fields holding this data, Text or Memo?
 
Upvote 0
It is a memo. The actual data looks like this. There are 6 spaces between each new line or what a new line would be in excel.
Request: NeededAccess Access Number : 01 Access Profile Name : ANAME Business Justification: New hire for AKFE.


<tbody>
</tbody>
ANAME

<tbody>
</tbody>
 
Upvote 0
For lines where an Access Profile Name exists, will it ALWAYS be immediately followed by "Business Justification" (ignoring all the extra spaces)?
 
Upvote 0
Are there are rules regarding the length of this Profile Name?
Just trying to figure out the best way of attacking this. Trying to pull something out of a bunch of text can be a little tricky.
 
Upvote 0
Not that I know of. We have some as short as 4 characters and others upwards of 30. I don't mind the questions and greatly appreciate the help!
 
Upvote 0
For something this complex, I like to create my own User Defined Functions in VBA.
Here is what I came up with:
Code:
Function AccessProfileName(myString As Variant) As String

    Dim myHeader As String
    Dim myStart As Long
    Dim myTemp As String
    Dim mySpace As Long
    
'   Enter header to look for
    myHeader = "Access Profile Name : "
    
'   Find where header starts
    myStart = InStr(myString, myHeader)
    
'   If no header found, return "No Match"
    If myStart = 0 Then
        AccessProfileName = "No Match"
    Else
'   ...else pull everything after the header
        myTemp = Trim(Mid(myString, myStart + Len(myHeader), 100))
'       Then find first space of the new string
        mySpace = InStr(myTemp, " ")
'       If no spaces, just return the string...
        If mySpace = 0 Then
            AccessProfileName = myTemp
        Else
'       ...else return everything up to that first space
            AccessProfileName = Left(myTemp, mySpace - 1)
        End If
    End If
    
End Function
So then you would just use this like any other function in a Query, Report, VBA, etc., i.e.
AccessProfileName([YourMemoField])
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,976
Members
448,934
Latest member
audette89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top