【问题标题】:Excel VBA address validationExcel VBA 地址验证
【发布时间】:2020-07-03 17:55:33
【问题描述】:

我正在创建一个数据输入表单,并在 addressBeforeUpdate() 例程中清理地址,然后将其存储到电子表格中。我想将所有“Street”更改为“St”但是,如果该人输入“123 Streetsboro Street”,我不想将实际街道名称中的“Street”更改为“St”。我也不能使用 Right() 函数,因为该人可能会在字符串末尾输入一个邮政信箱。

  • 示例:
  • ABC 街 123 号 --> ABC 街 123 号
  • 123 Streetsboro 街 --> 123 Streetsboro 街
  • 123 ABC Street, PO Box 1 --> 123 ABC St, PO Box 1

出于某种原因,这真的难倒我。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这是一个示例,我认为它可以提供帮助

    dim idx%, lenStr%
    dim adr$, newAdr$
    
    adr = "123 Streetsboro Street"
    
    newAdr = adr 'copy
    
    lenStr = len(adr)
    
    idx = instr(1, adr, "Street", CompareMethod.Text)
    
    if(lenStr = idx + 6) then 'end string 
      newAdr = replace(adr, "Street", "st", idx, 1) 'Replace One 
    else if (lenStr > idx + 6) then
      'check the next char
      if mid(adr, idx + 6 + 1) <> " " then ' this is name Streetsboro, replace the rest of string
         newAdr = replace(adr, "Street", "st", idx + 6 + 1)
      else
         newAdr = replace(adr, "Street", "st", idx, 1)
      end if
    end if
    

    【讨论】:

    • 我试过了,但它正在删除部分地址。我得到“boro St”。看来在 newAdr = replace(adr, "Street", "st", idx, 1) 中,newAdr 只是从替换点向右移动。
    • VinhCC,感谢您的帮助。我发现替换确实从替换开始的地方截断了字符串的左侧。为了规避它,我将原始地址的副本保存在另一个变量中,使用 MID() 函数从中取出一个子字符串到 REPLACE 开始的左侧,然后在替换完成后将它们组合在一起。再次感谢您的帮助。
    • @Papa 如果这对您有帮助或者是答案,请至少投票或接受它作为答案。
    猜你喜欢
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多