gpt4 book ai didi

vba - 仅在 VBA 中的 MID 位置替换

转载 作者:行者123 更新时间:2023-12-04 19:53:25 25 4
gpt4 key购买 nike

我想使用替换函数只替换 MID 的确切位置,而不是单元格中出现的所有位置。
前任。我想将 +++++###****### 中的 ### 替换为位置六(第一次出现)而不是第二个.

For Each c In .Columns(3).Cells
If c.Value = "" Then
Else
Do While Len(c.Value) < 20
c.Value = c.Value & " "
Loop
transNum = Mid(c.Value, 6, 3)
c.Value = Replace(c.Value, Mid(c.Value, 6, 3), "")
c.Value = "0" & transNum & c.Value
End If
y.Sheets(1).Range("E" & c.Row) = y.Sheets(1).Range("E" & c.Row) & c.Value
Next c

最佳答案

Mid 既是语句又是函数。使用语句版本做你想做的。 Mid 在帮助中分别作为语句和函数。

C = "CatDog"
Mid(C, 2) = "Fr"
MsgBox C

Mid Statement

Replaces a specified number of characters in a Variant (String) variable with characters from another string.

Syntax

Mid(stringvar, start[, length]) = string

The Mid statement syntax has these parts:

stringvar Required. Name of string variable to modify. start Required; Variant (Long). Character position in stringvar where the replacement of text begins.

length Optional; Variant (Long). Number of characters to replace. If omitted, all of string is used.

string Required. String expression that replaces part of stringvar.

Remarks

The number of characters replaced is always less than or equal to the number of characters in stringvar.

Note Use the MidB statement with byte data contained in a string. In the MidB statement, start specifies the byte position within stringvar where replacement begins and length specifies the numbers of bytes to replace.

关于vba - 仅在 VBA 中的 MID 位置替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050661/

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