Excel教程下载中心,Microsoft技术社区联盟成员,全球领先的Excel2003/2007/2010门户,培训学习Office的最佳社区

 34 1234
发新话题
打印

急! 怎样从string里提取想要的东东     hits : 4141

急! 怎样从string里提取想要的东东

cells(1,1)="C:\Documents and Settings\Matthew\My Documents\测评周报20060508-20060514\满意度统计5.8-5.14" 问题1: 怎样用VBA取得cells(1,1)里最后那个文件名"满意度统计5.8-5.14" 问题2: 怎样用VBA把“满意度统计5.8-5.14明细”里的日期“5.8-5.14"提取出来

TOP

Sub aa()
Dim i
Dim x
i = 0
For Each x In VBA.Split(Cells(1, 1), "\")
i = i + 1

Next
Cells(2, 1) = VBA.Split(Cells(1, 1), "\")(i - 1)

Cells(3, 1) = Right(Cells(2, 1), Len(Cells(2, 1)) - 5)

End Sub

TOP

谢谢答复。

第二个问题我好象没有说清楚。

应该是怎样从不定长string中提取一段字符串

如“肌肤反对法5.12-10.13大幅度幅度”,提取中间的5.12-10.13

TOP

没有规律可能比较难以提取
来生再续缘,和你共缠绵!

TOP

上面的解答有点小问题,下面可算完美了。 Dim strDate(1 To 3) As String '第3位存放0或者1,以表示处理的左支还是右支。 Sub aa() Dim l%, i%, mstr$ Dim x1, x2 On Error Resume Next i = 0 For Each x In VBA.Split(Cells(1, 1), "\") i = i + 1 Next Cells(2, 1) = VBA.Split(Cells(1, 1), "\")(i - 1) i = 0 For Each x1 In VBA.Split(Cells(2, 1), "-") i = i + 1 If i > 1 Then mstr = Right(VBA.Split(Cells(2, 1), "-")(i - 2), 5) k = 0 For Each x2 In VBA.Split(mstr, ".") k = k + 1 Next If k >= 2 Then strDate(1) = VBA.Split(mstr, ".")(k - 2) strDate(2) = VBA.Split(mstr, ".")(k - 1) strDate(3) = "0" End If If blDate() Then mstr = strDate(1) & "." & strDate(2) mstr1 = Left(x1, 5) strDate(3)="" k = 0 For Each x2 In VBA.Split(mstr1, ".") k = k + 1 Next If k >= 2 Then strDate(1) = VBA.Split(mstr1, ".")(0) strDate(2) = VBA.Split(mstr1, ".")(1) strDate(3) = "1" End If If blDate() Then mstr1 = strDate(1) & "." & strDate(2) Cells(8 + l, 1) = mstr & "-" & mstr1 strDate(3) = "" l = l + 1 End If End If End If Next End Sub Private Function blDate() As Boolean blDate = False If strDate(3) = "0" Then If Len(strDate(2)) >= 3 Then Exit Function If Right(strDate(2), 1) = "" Or Not VBA.IsNumeric(strDate(2)) Or Val(strDate(2)) > 31 Then Exit Function strDate(1) = Right(strDate(1), 2) If Asc(Left(strDate(1), 1)) < Asc("1") Or Asc(Left(strDate(1), 1)) > Asc("9") Then strDate(1) = Right(strDate(1), 1) If Right(strDate(1), 1) = "" Or Not VBA.IsNumeric(strDate(1)) Or Val(strDate(1)) > 12 Then Exit Function blDate = True End If If strDate(3) = "1" Then If Len(strDate(1)) >= 3 Then Exit Function If Left(strDate(1), 1) = "" Or Not VBA.IsNumeric(strDate(1)) Or Val(strDate(1)) > 12 Then Exit Function strDate(2) = Left(strDate(2), 2) If Asc(Right(strDate(2), 1)) < Asc("1") Or Asc(Right(strDate(2), 1)) > Asc("9") Then strDate(2) = Left(strDate(2), 1) If Right(strDate(1), 1) = "" Or Not VBA.IsNumeric(strDate(1)) Or Val(strDate(1)) > 12 Then Exit Function blDate = True End If End Function

[此贴子已经被作者于2006-5-29 8:18:00编辑过]

TOP

满意度3.7-4.31…...统…04.25-05…...19-…..dsafds3.15-4.21gfdhgfjghkjhl"

3.7-4.31
3.15-4.21

只要不出现连续的"--------"等情况,应该无大问题。
满意度3.7-4.31…...统计…04.25-05…...19-…..dsafds3.15-4.21gfdhgfjghkjhl"

满意度3.7-4.31…...统计…04.25-05…...19-…..dsafds3.15-4.21gfdhgfjghkjhl"

TOP

上面的解答有点小问题,下面可算完美了。

Dim strDate(1 To 3) As String '第3位存放0或者1,以表示处理的左支还是右支。


Sub aa()
Dim l%, i%, mstr$
Dim x1, x2
On Error Resume Next
i = 0
For Each x In VBA.Split(Cells(1, 1), "\")
i = i + 1
Next
Cells(2, 1) = VBA.Split(Cells(1, 1), "\")(i - 1)

i = 0
For Each x1 In VBA.Split(Cells(2, 1), "-")
i = i + 1
If i > 1 Then
mstr = Right(VBA.Split(Cells(2, 1), "-")(i - 2), 5)
k = 0
For Each x2 In VBA.Split(mstr, ".")
k = k + 1
Next
Debug.Print k

If k >= 2 Then
strDate(1) = VBA.Split(mstr, ".")(k - 2)
strDate(2) = VBA.Split(mstr, ".")(k - 1)
strDate(3) = "0"
End If
If blDate() Then
mstr = strDate(1) & "." & strDate(2)
mstr1 = Left(x1, 5)
strDate(3)=""
k = 0
For Each x2 In VBA.Split(mstr1, ".")
k = k + 1
Next
If k >= 2 Then
strDate(1) = VBA.Split(mstr1, ".")(0)
strDate(2) = VBA.Split(mstr1, ".")(1)
strDate(3) = "1"
End If
If blDate() Then
mstr1 = strDate(1) & "." & strDate(2)
Cells(8 + l, 1) = mstr & "-" & mstr1
strDate(3) = ""
l = l + 1
End If
End If
End If
Next
End Sub

Private Function blDate() As Boolean
blDate = False
If strDate(3) = "0" Then
If Len(strDate(2)) >= 3 Then Exit Function
If Right(strDate(2), 1) = "" Or Not VBA.IsNumeric(strDate(2)) Or Val(strDate(2)) > 31 Then Exit Function
strDate(1) = Right(strDate(1), 2)
If Asc(Left(strDate(1), 1)) < Asc("1") Or Asc(Left(strDate(1), 1)) > Asc("9") Then strDate(1) = Right(strDate(1), 1)
If Right(strDate(1), 1) = "" Or Not VBA.IsNumeric(strDate(1)) Or Val(strDate(1)) > 12 Then Exit Function
blDate = True
End If
If strDate(3) = "1" Then
If Len(strDate(1)) >= 3 Then Exit Function
If Left(strDate(1), 1) = "" Or Not VBA.IsNumeric(strDate(1)) Or Val(strDate(1)) > 12 Then Exit Function
strDate(2) = Left(strDate(2), 2)
If Asc(Right(strDate(2), 1)) < Asc("1") Or Asc(Right(strDate(2), 1)) > Asc("9") Then strDate(2) = Left(strDate(2), 1)
If Right(strDate(1), 1) = "" Or Not VBA.IsNumeric(strDate(1)) Or Val(strDate(1)) > 12 Then Exit Function
blDate = True
End If


End Function

[此贴子已经被作者于2006-5-29 8:21:11编辑过]

TOP

instr()

用这个函数方便很多,自己想想

一讲就会,一做就错。。。 动手做做,才算真正学会^_^ 最近学access,菜鸟一只 -_-!

TOP

instr()

用这个函数方便很多,自己想想

——————————————————————

楼上你具体实现一下看看?这问题其实是比较复杂的,因为它对输入数据没有任何限制,这使得你在编程中必须对许多不符规范情况进行排除。我上面的程序可以筛选出任何符合XX。XX-XX。XX规范的日期,不管输入数据是什么。instr()只能给出“."或"-"的位置而已,这些数据的前后情况怎样你必须自己去解决。

TOP

引用:
以下是引用[I]loverbeast[/I]在2006-5-26 20:36:45的发言:[BR]cells(1,1)="C:\Documents and Settings\Matthew\My Documents\测评周报20060508-20060514\满意度统计5.8-5.14" 问题2: 怎样用VBA把“满意度统计5.8-5.14明细”里的日期“5.8-5.14"提取出来
Function FilterStr(AnyVal As String) Dim RegEx Set RegEx = CreateObject("vbscript.regexp") RegEx.Global = True RegEx.Pattern = "[^\d-.]+" FilterStr = RegEx.Replace(AnyVal, "") Set RegEx = Nothing End Function Sub Test() Dim TempStr As String TempArr = Split(Range("A1"), "\") TempStr = TempArr(UBound(TempArr)) MsgBox FilterStr(TempStr) End Sub
Have no passion for Excel especially at   "Home"

TOP

 34 1234
发新话题
本论坛言论纯属发表者个人意见,与Excel Home立场无关,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!