我有以下Sub和Function,我希望函数的结果替换每个Postcode的内容。基本上该函数增加了一个空间来将邮政编码分解为正确的格式。一切都很好,只是在功能结束时复制结果,我无法弄清楚
Sub Postcode()
Dim Postcode As Range, Postcodes As Range
Dim lastRow As Integer
Dim X As String
lastRow = Worksheets("UserInfo").Range("A" & Rows.Count).End(xlUp).row
Set Postcodes = Worksheets("EeeDetails").Range("K2:K" & lastRow)
For Each Postcode In Postcodes
If Len(Postcode) > 6 Then
X = Add_Space_4(Postcode.Value)
Postcode = X
End If
Next Postcode
End Sub
Function Add_Space_4(Postcode As String)
Dim i As Long
Dim result As String
For i = 1 To Len(Postcode) Step 4
On Error Resume Next
result = result & Left(Postcode, 4) & " "
Postcode = Mid(Postcode, 5, Len(Postcode) - 4)
Next i
Postcode = Left(result, Len(result) - 1)
End Function
答案 0 :(得分:3)
为了让函数返回值,您需要将值设置为函数名称。所以把它改成这样(假设你想把最后一行作为传递的值):
Add_Space_4 = Left(result, Len(result) - 1)