vba help - extract value from text

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

Below is my data in txt file,
Task is Copy Currency, Period and Buy Type: if nothing is mentioned infront of it Consider (zero value)

There are around 200 lines of data , I have to find and get value.


These 3 lines are sample from txt file , Ignore Blank lines
Currency : USD Period : ABC+PQR Buy Type: Manual

Currency : INR Period : ABC+PQR Buy Type:
Currency : AUD Period : -------- Buy Type: AAA,BBB CCC DDD HHH


Below is expected Value
Currency PeriodBuy Type
USDABC+PQRManual
INRABC+PQR0
AUD--------AAA,BBB CCC DDD HHH



Thanks
mg
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi Mallesh - 200 lines of data sounds like quite a task. What have you tried so far?
 
Upvote 0
Hi Dan,
I am using instr finding specific position, or mid , left right. Its big task solving step by step.
my data is in office laptop.

pos = instr(1,str("Currency:")
pos = instr(1,str("Buy Type:")
pos = instr(1,str("Period:")

Below is sample code I am referring, I will modify as per my accordance,
Sub ImportDataFromTextFile()
Dim T As New clsText
Dim S As New clsSales
Dim Rng As Range
Workbooks.Add


FilePath = ThisWorkbook.Path & "\041 - Data.txt"

With T
.OpenTextRO FilePath

Do Until .ReadLine = False
If InStr(.CurrentLine, "Customer ID: ") > 0 Then
S.CustomerID = Replace(.CurrentLine, "Customer ID: ", "")
End If

If InStr(.CurrentLine, "Customer Name: ") > 0 Then
S.CustomerName = Replace(.CurrentLine, "Customer Name: ", "")
End If

If Not .LineContains("Customer ID:", "Customer Name:", "Product") Then
S.Product = .Trim(Mid(.CurrentLine, 1, 8))
S.Qty = .Trim(Mid(.CurrentLine, 9, 8))
S.Rate = .Trim(Mid(.CurrentLine, 17, 8))
S.Amt = .Trim(Mid(.CurrentLine, 25, 8))
Set Rng = Rng.Offset(1, 0)
With S
Rng.Value = Array(.CustomerID, .CustomerName, .Product, .Qty, .Rate, .Amt)
End With
End If
Loop
End With
End Sub



Thanks
mg
 
Upvote 0
Task is Copy Currency, Period and Buy Type: if nothing is mentioned infront of it Consider (zero value)

Where do u have the 0 value exacly.
Can you elaborate a bit on this
 
Upvote 0
Hi RadoSlaw,

Actually some time there will not be any data , infront of Buy Type:

So I am giving default value as 0 or n/a or dash or not found and putting in excel.


if I get some hint I will modify the code as per my requirement. thanks in advance for looking into this.




Thanks
mg
 
Upvote 0
is this expected result?
Book1
ABCDEFGH
1Currency PeriodBuy TypeWhat u want to filter: ===>>>>0
2USDABC+PQRManualUSDABC+PQRManual
3INRABC+PQR0AUD--------AAA,BBB CCC DDD HHH
4AUD--------AAA,BBB CCC DDD HHH
5
6Currency PeriodBuy TypeWhat u want to filter: ===>>>>Not To Be Used
7USDABC+PQRManualUSDABC+PQRManual
8INRABC+PQRNot To Be UsedAUD--------AAA,BBB CCC DDD HHH
9AUD--------AAA,BBB CCC DDD HHH
Sheet1
Cell Formulas
RangeFormula
E2:G3E2=FILTER(Table1,Table1[Buy Type]<>$F$1)
E7:G8E7=FILTER(Table13,Table13[Buy Type]<>$F$6)
Dynamic array formulas.


if so its done by
Excel Formula:
=filter()
function

Also is ur office version accurate?
its best to switch to 365.
 
Upvote 0
Hi RadoSlow,

Thanks for your help and time, But here I am looking for help in vba.

using instr , mid or regular expression style. like this.




Thanks
mg
 
Upvote 0
if you insist on VBA
or this might help you:
thats where I learned how to use adv filter in VBA.

sorry for not being able to help more
 
Upvote 0
Just formula solution if anyone wants

Book1
ABCD
7Currency : USD Period : ABC+PQR Buy Type: ManualUSDABC+PQRManual
8Currency : INR Period : ABC+PQR Buy Type:INRABC+PQR 
9Currency : AUD Period : -------- Buy Type: AAA,BBB CCC DDD HHHAUD--------AAA,BBB CCC DDD HHH
Sheet1
Cell Formulas
RangeFormula
B7:B9B7=TRIM(LEFT(SUBSTITUTE(MID(A7,FIND("Currency",A7)+11,99)," ",REPT(" ",99)),99))
C7:C9C7=TRIM(LEFT(SUBSTITUTE(MID(A7,FIND(":",SUBSTITUTE(A7,":","x",1))+2,99),"Buy",REPT(" ",99)),99))
D7:D9D7=MID(A7,FIND("Type",A7)+6,99)
 
Upvote 0
Try this. results in A to C
"c:\trabajo\data.txt" 'update path an file name

VBA Code:
Sub ExtractValuesFromText()
  Dim LineofText As Variant, xFile As Variant
  Dim a As Variant, sItems As Variant, s As Variant
  Dim i As Long
  
  ' Open the file for Input.
  xFile = "c:\trabajo\data.txt"   'update path an file name
  Open xFile For Input As #1
  
  Range("A:C").ClearContents
  Range("A1:C1").Value = Array("Currency", "Period", "Buy Type")
  i = 2
  ' Read each line of the text file into a single string
  Do While Not EOF(1)
    Line Input #1, LineofText
    If LineofText <> "" Then
      sItems = Split(LineofText, ":")
      Range("A" & i).Value = WorksheetFunction.Trim(Replace(Replace(sItems(1), "Currency", ""), "Period", ""))
      Range("B" & i).Value = WorksheetFunction.Trim(Replace(sItems(2), "Buy Type", ""))
      If WorksheetFunction.Trim(sItems(3)) = "" Then s = 0 Else s = WorksheetFunction.Trim(sItems(3))
      Range("C" & i).Value = s
      i = i + 1
    End If
  Loop
  ' Close the file.
  Close #1
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

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