Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Countif for a string

I am importing a text file into excel line by line. I only want to import
lines where the first non-space character is a number or negative sign AND
only those that have two commas in the string. I have the first part
working, I just need to know how to do the AND part (strings with only two
commas).

This would be easy if I were looking at a string in a cell by using countif,
but this is a string from a text file. Is there any way of doing this
without importing the entire file and then using countif on the imported
cells?

This is what I have so far:

Sub Mikesub()
....
Open Filename For Input As #FileNum
Counter = 1
keywords = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "-")
Do While Seek(FileNum) <= LOF(FileNum)
Line Input #FileNum, ResultStr
For i = LBound(keywords) To UBound(keywords)
fstword = LTrim(ResultStr)
If Left(fstword, 1) = keywords(i) Then
bfound = True
Exit For
End If
Next
If bfound Then
ActiveCell.Value = Trim(ResultStr)
ActiveCell.Offset(1, 0).Select
bfound = False
End If
Counter = Counter + 1
Loop
....
End Sub


For example with the following data I would only like the second and fourth
lines imported.

-1234, 4321, 5.1232, 92.1
90310, 110.0, -1.0
90311, 105.1 -1.0
-90312, 125.4, 12
B2931, 94.1, 0.32


Thanks in advance for your help.

Mike

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default Countif for a string

If Left(fstword, 1) = keywords(i) Then
If Len(fstword) - Len(Replace(fstword,"-","")) =2 Then
bfound = True
Exit For
End If
End If


--
HTH

Bob Phillips

"crazybass2" wrote in message
...
I am importing a text file into excel line by line. I only want to import
lines where the first non-space character is a number or negative sign AND
only those that have two commas in the string. I have the first part
working, I just need to know how to do the AND part (strings with only two
commas).

This would be easy if I were looking at a string in a cell by using

countif,
but this is a string from a text file. Is there any way of doing this
without importing the entire file and then using countif on the imported
cells?

This is what I have so far:

Sub Mikesub()
...
Open Filename For Input As #FileNum
Counter = 1
keywords = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "-")
Do While Seek(FileNum) <= LOF(FileNum)
Line Input #FileNum, ResultStr
For i = LBound(keywords) To UBound(keywords)
fstword = LTrim(ResultStr)
If Left(fstword, 1) = keywords(i) Then
bfound = True
Exit For
End If
Next
If bfound Then
ActiveCell.Value = Trim(ResultStr)
ActiveCell.Offset(1, 0).Select
bfound = False
End If
Counter = Counter + 1
Loop
...
End Sub


For example with the following data I would only like the second and

fourth
lines imported.

-1234, 4321, 5.1232, 92.1
90310, 110.0, -1.0
90311, 105.1 -1.0
-90312, 125.4, 12
B2931, 94.1, 0.32


Thanks in advance for your help.

Mike



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default Countif for a string

Hi Mike

Sub test()
MsgBox ShouldImport("-1234, 4321, 5.1232, 92.1")
MsgBox ShouldImport("90310, 110.0, -1.0")
MsgBox ShouldImport("90311, 105.1 -1.0")
MsgBox ShouldImport("-90312, 125.4, 12")
MsgBox ShouldImport("B2931, 94.1, 0.32")
End Sub

Function ShouldImport(ByRef S As String) As Boolean
Dim L1 As Long, L2 As Long, L3 As Long
If Val(S) < 0 Then
If Len(S) - Len(Replace$(S, ",", "")) = 2 Then ShouldImport = True
End If
End Function

HTH. Best wishes Harald

"crazybass2" skrev i melding
...
I am importing a text file into excel line by line. I only want to import
lines where the first non-space character is a number or negative sign AND
only those that have two commas in the string. I have the first part
working, I just need to know how to do the AND part (strings with only two
commas).

This would be easy if I were looking at a string in a cell by using

countif,
but this is a string from a text file. Is there any way of doing this
without importing the entire file and then using countif on the imported
cells?

This is what I have so far:

Sub Mikesub()
...
Open Filename For Input As #FileNum
Counter = 1
keywords = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "-")
Do While Seek(FileNum) <= LOF(FileNum)
Line Input #FileNum, ResultStr
For i = LBound(keywords) To UBound(keywords)
fstword = LTrim(ResultStr)
If Left(fstword, 1) = keywords(i) Then
bfound = True
Exit For
End If
Next
If bfound Then
ActiveCell.Value = Trim(ResultStr)
ActiveCell.Offset(1, 0).Select
bfound = False
End If
Counter = Counter + 1
Loop
...
End Sub


For example with the following data I would only like the second and

fourth
lines imported.

-1234, 4321, 5.1232, 92.1
90310, 110.0, -1.0
90311, 105.1 -1.0
-90312, 125.4, 12
B2931, 94.1, 0.32


Thanks in advance for your help.

Mike



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default Countif for a string

Doh! Delete all the Dim L stuff, it belongs to something totelly different.
Sorry.

"Harald Staff" skrev i melding
...
Hi Mike

Sub test()
MsgBox ShouldImport("-1234, 4321, 5.1232, 92.1")
MsgBox ShouldImport("90310, 110.0, -1.0")
MsgBox ShouldImport("90311, 105.1 -1.0")
MsgBox ShouldImport("-90312, 125.4, 12")
MsgBox ShouldImport("B2931, 94.1, 0.32")
End Sub

Function ShouldImport(ByRef S As String) As Boolean
Dim L1 As Long, L2 As Long, L3 As Long
If Val(S) < 0 Then
If Len(S) - Len(Replace$(S, ",", "")) = 2 Then ShouldImport = True
End If
End Function

HTH. Best wishes Harald

"crazybass2" skrev i melding
...
I am importing a text file into excel line by line. I only want to

import
lines where the first non-space character is a number or negative sign

AND
only those that have two commas in the string. I have the first part
working, I just need to know how to do the AND part (strings with only

two
commas).

This would be easy if I were looking at a string in a cell by using

countif,
but this is a string from a text file. Is there any way of doing this
without importing the entire file and then using countif on the imported
cells?

This is what I have so far:

Sub Mikesub()
...
Open Filename For Input As #FileNum
Counter = 1
keywords = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "-")
Do While Seek(FileNum) <= LOF(FileNum)
Line Input #FileNum, ResultStr
For i = LBound(keywords) To UBound(keywords)
fstword = LTrim(ResultStr)
If Left(fstword, 1) = keywords(i) Then
bfound = True
Exit For
End If
Next
If bfound Then
ActiveCell.Value = Trim(ResultStr)
ActiveCell.Offset(1, 0).Select
bfound = False
End If
Counter = Counter + 1
Loop
...
End Sub


For example with the following data I would only like the second and

fourth
lines imported.

-1234, 4321, 5.1232, 92.1
90310, 110.0, -1.0
90311, 105.1 -1.0
-90312, 125.4, 12
B2931, 94.1, 0.32


Thanks in advance for your help.

Mike





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Countif for a string

I was kind of wondering about that...


"Harald Staff" wrote:

Doh! Delete all the Dim L stuff, it belongs to something totelly different.
Sorry.

"Harald Staff" skrev i melding
...
Hi Mike

Sub test()
MsgBox ShouldImport("-1234, 4321, 5.1232, 92.1")
MsgBox ShouldImport("90310, 110.0, -1.0")
MsgBox ShouldImport("90311, 105.1 -1.0")
MsgBox ShouldImport("-90312, 125.4, 12")
MsgBox ShouldImport("B2931, 94.1, 0.32")
End Sub

Function ShouldImport(ByRef S As String) As Boolean
Dim L1 As Long, L2 As Long, L3 As Long
If Val(S) < 0 Then
If Len(S) - Len(Replace$(S, ",", "")) = 2 Then ShouldImport = True
End If
End Function

HTH. Best wishes Harald

"crazybass2" skrev i melding
...
I am importing a text file into excel line by line. I only want to

import
lines where the first non-space character is a number or negative sign

AND
only those that have two commas in the string. I have the first part
working, I just need to know how to do the AND part (strings with only

two
commas).

This would be easy if I were looking at a string in a cell by using

countif,
but this is a string from a text file. Is there any way of doing this
without importing the entire file and then using countif on the imported
cells?

This is what I have so far:

Sub Mikesub()
...
Open Filename For Input As #FileNum
Counter = 1
keywords = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "-")
Do While Seek(FileNum) <= LOF(FileNum)
Line Input #FileNum, ResultStr
For i = LBound(keywords) To UBound(keywords)
fstword = LTrim(ResultStr)
If Left(fstword, 1) = keywords(i) Then
bfound = True
Exit For
End If
Next
If bfound Then
ActiveCell.Value = Trim(ResultStr)
ActiveCell.Offset(1, 0).Select
bfound = False
End If
Counter = Counter + 1
Loop
...
End Sub


For example with the following data I would only like the second and

fourth
lines imported.

-1234, 4321, 5.1232, 92.1
90310, 110.0, -1.0
90311, 105.1 -1.0
-90312, 125.4, 12
B2931, 94.1, 0.32


Thanks in advance for your help.

Mike








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Countif for a string

Thanks for both Bob and Harald for the quick response. You both basically
displayed the same solution. This will work great!

Mike

"Harald Staff" wrote:

Hi Mike

Sub test()
MsgBox ShouldImport("-1234, 4321, 5.1232, 92.1")
MsgBox ShouldImport("90310, 110.0, -1.0")
MsgBox ShouldImport("90311, 105.1 -1.0")
MsgBox ShouldImport("-90312, 125.4, 12")
MsgBox ShouldImport("B2931, 94.1, 0.32")
End Sub

Function ShouldImport(ByRef S As String) As Boolean
Dim L1 As Long, L2 As Long, L3 As Long
If Val(S) < 0 Then
If Len(S) - Len(Replace$(S, ",", "")) = 2 Then ShouldImport = True
End If
End Function

HTH. Best wishes Harald

"crazybass2" skrev i melding
...
I am importing a text file into excel line by line. I only want to import
lines where the first non-space character is a number or negative sign AND
only those that have two commas in the string. I have the first part
working, I just need to know how to do the AND part (strings with only two
commas).

This would be easy if I were looking at a string in a cell by using

countif,
but this is a string from a text file. Is there any way of doing this
without importing the entire file and then using countif on the imported
cells?

This is what I have so far:

Sub Mikesub()
...
Open Filename For Input As #FileNum
Counter = 1
keywords = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "-")
Do While Seek(FileNum) <= LOF(FileNum)
Line Input #FileNum, ResultStr
For i = LBound(keywords) To UBound(keywords)
fstword = LTrim(ResultStr)
If Left(fstword, 1) = keywords(i) Then
bfound = True
Exit For
End If
Next
If bfound Then
ActiveCell.Value = Trim(ResultStr)
ActiveCell.Offset(1, 0).Select
bfound = False
End If
Counter = Counter + 1
Loop
...
End Sub


For example with the following data I would only like the second and

fourth
lines imported.

-1234, 4321, 5.1232, 92.1
90310, 110.0, -1.0
90311, 105.1 -1.0
-90312, 125.4, 12
B2931, 94.1, 0.32


Thanks in advance for your help.

Mike




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a sub string in a COUNTIF PeteJ Excel Discussion (Misc queries) 2 January 7th 14 01:31 AM
Use CountIf for a literal string chadkwelch Excel Worksheet Functions 3 March 21st 10 03:45 AM
COUNTIF according to presence of string within text Ingeniero1 Excel Worksheet Functions 5 February 1st 06 06:43 PM
Countif function for instances of text string contained Garbunkel Excel Worksheet Functions 1 October 11th 05 08:09 AM
COUNTIF - everything excluding a string sans Excel Worksheet Functions 3 October 6th 05 08:32 PM


All times are GMT +1. The time now is 10:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"