Home > Software engineering >  A new o VBA custom function
A new o VBA custom function

Time:10-07

Before learning Java and profound thought of excel is so little brother work in urgent for an example of VBA custom function want warrior, interested can add me loops: 89319572 to help each other, here is what I want to realize the function:
Will excel the address of a cell in the table are extracted from a certain part of the first written on my cell:
=the IF (ISERROR (FIND (huangcun, i2)), the IF (ISERROR (FIND (" west red gate ", i2)), and 0, "west red gate"), "huangcun town")
But nested too much more than a certain number of error, so we have to customize a function at such a function to "Beijing's daxing district huangcun town village, sun village south 500 meters" in the "huangcun town" extracted (to huangcun town ", "more than a dozen such keywords, you can write to death), there should be a switch in other programming languages case statement don't know if the VBA, method of how to write and how network by value

CodePudding user response:

And not have broken words, where use Case branch,
And you search ( huangcun ) and return (huangcun town ) keywords is different also, small using If branch,
 Function GetKeyWord (ByVal s As String) As String 
Dim sKeyWord As String

If InStr (1 s, huangcun ") Then
SKeyWord="huangcun town"
ElseIf InStr (1 s, "west red gate") Then
SKeyWord="west red door"
The Else
SKeyWord="0"
End the If

GetKeyWord=sKeyWord
End the Function

CodePudding user response:

 s="Beijing's daxing district huangcun town village, sun village south 500 meters" :? Mid (s, Instr (s, "area") + 1, Instr (s, "town") - Instr (s, "area")) 

CodePudding user response:

refer to the second floor zhao4zhong1 response:
 s="huangcun town sun village, the village committee of Beijing daxing district south 500 meters" :? Mid (s, Instr (s, "area") + 1, Instr (s, "town") - Instr (s, "area")) 


This is assuming that they can find cases,
Can't find it?

CodePudding user response:

Using Word segmentation:
 Private Sub Form_Load () 
Dim sentence As String
Dim words As String
Dim WordApp As Object
Sentence="Beijing's daxing district huangcun town village, sun village south 500 meters"
Words=""
The Set WordApp=CreateObject (" Word. Application ")
WordApp. Documents. The Add
WordApp. Selection. TypeText Text:=what
WordApp. Selection. HomeKey
Do
WordApp. Selection. MoveRight Unit:=2, the Count:=1, the Extend:=1
If WordApp. Selection. The Text=vbCr Then Exit the Do
Words=words + WordApp. Selection. The Text + vbCrLf
WordApp. Selection. MoveRight Unit:=1, the Count:=1
Loop
WordApp. Quit SaveChanges:=0
The Set WordApp=Nothing
MsgBox words
End
End Sub



  •  Tags:  
  • VBA
  • Related