Microsoft技术社区联盟成员,全球领先的Excel2003/2007/2010门户,Office培训学习的最佳社区
设为首页收藏本站|繁體中文 切换到窄版

Excel Home论坛

 找回密码
 免费注册

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

魔方网表,Excel终结者,永久免费 Excel服务器2010软件和教程下载 菜鸟啃Excel: 样章试读
Excel不给力? 何不试试FoxTable! 2012年Excel免费培训班2-6月开课计划 新人必读:ExcelHome最佳学习方法 免费下载Excel行业应用视频教程
精粹:成为Excel高手的捷径 免费下载39集新Excel精粹视频教程 免费学习Excel数据透视表视频教程 入门必看《循序渐进学Excel》视频
Excel辅助工具的巅峰之作--Kutools 免费学习Excel 2007精粹视频教程 Office Tab,Office界面的革命性创新 搞不定老板要的报表?我们来帮您
  • 134财富
  • 0鲜花
  • 0技术
    • 等级 2EH初级
    积分排行
    3000+
    帖子
    37
    精华
    0
    分享
    0

    急! 怎样从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"提取出来
  • 1027财富
  • 0鲜花
  • 1技术
    • 等级 3EH中级
    积分排行
    1513
    帖子
    433
    精华
    0
    分享
    0
    发表于 2006-5-26 20:57:44 |显示全部楼层
    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

  • 134财富
  • 0鲜花
  • 0技术
    • 等级 2EH初级
    积分排行
    3000+
    帖子
    37
    精华
    0
    分享
    0
    发表于 2006-5-26 22:13:06 |显示全部楼层

    谢谢答复。

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

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

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

  • 23090财富
  • 1鲜花
  • 12技术
  • 积分排行
    28
    帖子
    11678
    精华
    2
    分享
    0
    发表于 2006-5-26 22:18:24 |显示全部楼层
    没有规律可能比较难以提取
  • 1027财富
  • 0鲜花
  • 1技术
    • 等级 3EH中级
    积分排行
    1513
    帖子
    433
    精华
    0
    分享
    0
    发表于 2006-5-27 20:39:07 |显示全部楼层
    上面的解答有点小问题,下面可算完美了。 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编辑过]

  • 1027财富
  • 0鲜花
  • 1技术
    • 等级 3EH中级
    积分排行
    1513
    帖子
    433
    精华
    0
    分享
    0
    发表于 2006-5-27 20:43:03 |显示全部楼层
    满意度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"
  • 1027财富
  • 0鲜花
  • 1技术
    • 等级 3EH中级
    积分排行
    1513
    帖子
    433
    精华
    0
    分享
    0
    发表于 2006-5-28 13:38:15 |显示全部楼层

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

    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编辑过]

  • 819财富
  • 0鲜花
  • 0技术
    • 等级 3EH中级
    积分排行
    1853
    帖子
    323
    精华
    0
    分享
    0
    发表于 2006-5-28 14:08:03 |显示全部楼层

    instr()

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

  • 1027财富
  • 0鲜花
  • 1技术
    • 等级 3EH中级
    积分排行
    1513
    帖子
    433
    精华
    0
    分享
    0
    发表于 2006-5-28 15:14:57 |显示全部楼层

    instr()

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

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

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

  • 3337财富
  • 0鲜花
  • 12技术
  • 积分排行
    358
    帖子
    1558
    精华
    1
    分享
    0
    发表于 2006-5-28 16:38:54 |显示全部楼层
    以下是引用[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"

    发表回复

    您需要登录后才可以回帖 登录 | 免费注册

    发帖时请遵守我国法律,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任。
    回顶部