VBA用于斜杠之间的字文件名
我构建了一个宏,它根据名称列表保存工作簿。 我得到了一个saveAs错误,因为一些新列表项在文件名中有/是不允许的 我需要一些帮助来编写一个代码来实现这一点:请注意,球队、名字、联赛和奖杯的列表每行都会发生变化VBA用于斜杠之间的字文件名,vba,excel,Vba,Excel,我构建了一个宏,它根据名称列表保存工作簿。 我得到了一个saveAs错误,因为一些新列表项在文件名中有/是不允许的 我需要一些帮助来编写一个代码来实现这一点:请注意,球队、名字、联赛和奖杯的列表每行都会发生变化 list item: team/ league what I want: league list item: team / league / cup What I want: league-cup List item; t
list item: team/ league
what I want: league
list item: team / league / cup
What I want: league-cup
List item; team / league / cup / score
what I want: league-cup-score
我让它在第一个场景中工作,在第一个场景中只有一个/但在其余场景中无法计算
InStr(Value, "/") > 0 Then
filename = Right(Value, (Len(Value) - InStr(Value, "/")))
Else
filename = Value
End If
这就是我目前得到的
感谢使用您已有的代码,您可以使用VBA替换功能将“/”替换为“-”
使用已有的代码,可以使用VBA替换函数将“/”替换为“-”
您需要使用替换 例如:
Dim oldString as String
Dim newString as String
newString=Replace(oldString,"/","_")
您需要使用替换 例如:
Dim oldString as String
Dim newString as String
newString=Replace(oldString,"/","_")
您可以拆分、修剪和连接:
Function MakeFileName(s As String) As String
Dim v As Variant, w As Variant
Dim i As Long, n As Long
v = Split(s, "/")
n = UBound(v)
ReDim w(1 To n)
For i = 1 To n
w(i) = Trim(v(i))
Next i
MakeFileName = Join(w, "-")
End Function
例如:
Sub test()
Debug.Print MakeFileName("team/ league")
Debug.Print MakeFileName("team / league / cup")
Debug.Print MakeFileName("team / league / cup / score")
End Sub
输出:
league
league-cup
league-cup-score
您可以拆分、修剪和连接:
Function MakeFileName(s As String) As String
Dim v As Variant, w As Variant
Dim i As Long, n As Long
v = Split(s, "/")
n = UBound(v)
ReDim w(1 To n)
For i = 1 To n
w(i) = Trim(v(i))
Next i
MakeFileName = Join(w, "-")
End Function
例如:
Sub test()
Debug.Print MakeFileName("team/ league")
Debug.Print MakeFileName("team / league / cup")
Debug.Print MakeFileName("team / league / cup / score")
End Sub
输出:
league
league-cup
league-cup-score
另一种解决方案,这次使用
InStr
。编辑以处理不存在联盟的情况
Sub Example()
Dim str1 As String: str1 = "list item: team/ league"
Dim str2 As String: str2 = "list item: team / league / cup "
Dim str3 As String: str3 = "List item; team / league / cup / score"
Debug.Print getTeamText(str1)
Debug.Print getTeamText(str2)
Debug.Print getTeamText(str3)
End Sub
Function getTeamText(ByVal StringIn As String) As String
Dim strPos As Long
Dim lookFor As String: lookFor = "league"
strPos = InStr(1, LCase(StringIn), lookFor)
If strPos > 0 Then
getTeamText = Replace(Replace(Mid(StringIn, strPos, (Len(StringIn) - strPos) + 1), " ", ""), "/", "-")
Else
getTeamText = "Could not find match"
End If
End Function
输出应与预期一致。另一种解决方案,这次使用
InStr
。编辑以处理不存在联盟的情况
Sub Example()
Dim str1 As String: str1 = "list item: team/ league"
Dim str2 As String: str2 = "list item: team / league / cup "
Dim str3 As String: str3 = "List item; team / league / cup / score"
Debug.Print getTeamText(str1)
Debug.Print getTeamText(str2)
Debug.Print getTeamText(str3)
End Sub
Function getTeamText(ByVal StringIn As String) As String
Dim strPos As Long
Dim lookFor As String: lookFor = "league"
strPos = InStr(1, LCase(StringIn), lookFor)
If strPos > 0 Then
getTeamText = Replace(Replace(Mid(StringIn, strPos, (Len(StringIn) - strPos) + 1), " ", ""), "/", "-")
Else
getTeamText = "Could not find match"
End If
End Function
输出应与预期一致。您可以使用
filename = Join(Split(Replace(Replace(Value, " ", ""), "team/", ""), "/"), "-")
你可以用
filename = Join(Split(Replace(Replace(Value, " ", ""), "team/", ""), "/"), "-")
请避免链接的答案,因为它们可能是毫无意义的情况下,链接曾经死亡。离开链接是好的(无论出于何种原因,它可能是有用的),但是,也要在答案本身中放置有意义的内容。请避免只使用链接的答案,因为它们可能在链接消失的情况下毫无意义。离开链接是好的(无论出于何种原因,它可能是有用的),但是,也要在答案本身中放置有意义的内容。虽然这是一个很好的解决方案,但这取决于数据。如果
StringIn
中没有“league”
,则代码将中断,并出现运行时错误“5”:无效的过程调用或参数
尽管这是一个不错的解决方案,但这取决于数据。如果StringIn
中没有“league”
,则代码将中断,并出现运行时错误“5”:无效的过程调用或参数
虽然您可能已经解决了此用户的问题,但仅代码的答案对将来遇到此问题的用户没有多大帮助。请编辑您的答案,以解释为什么您的代码解决了原始问题。@JoeC,例如“您可以拆分、修剪和加入”或“另一种解决方案,这次使用InStr.edit来处理没有联盟的情况。”或“使用您已有的代码,您可以使用VBA替换函数将“/”替换为“-”或“您需要使用替换”与没有解释一样有帮助。虽然您可能已经解决了此用户的问题,但仅代码的答案对将来遇到此问题的用户没有多大帮助。请编辑您的答案以解释代码解决原始问题的原因。@JoeC,例如“您可以拆分、修剪和联接”或另一个解决方案,这次使用InStr。编辑以处理不存在联盟的情况。”或“使用您已有的代码,您可以使用VBA替换函数将“/”替换为“-”或“您需要使用替换”与不进行解释一样有帮助