gpt4 book ai didi

vba - 将函数结果复制到 Sub

转载 作者:行者123 更新时间:2023-12-04 21:54:05 24 4
gpt4 key购买 nike

我有以下子和函数,我希望函数的结果替换每个邮政编码的内容。基本上,该函数添加了一个空间来将邮政编码分解为正确的格式。一切正常,只是在函数末尾复制了我无法弄清楚的结果

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

最佳答案

为了让函数将值传回,您需要将值设置为函数名称。因此,将其更改为如下所示(假设您希望最后一行作为传回的值):

Add_Space_4 = Left(result, Len(result) - 1)

关于vba - 将函数结果复制到 Sub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48343906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com