我们需要程序将字符串末尾的“/”之后的值或两个符号“/”之间的值从C列复制到新的G列。 我们的程序无法正常工作,它只复制第一次出现的情况,并且必须复制最后一次出现的情况。
Sub Copyvalue()
Dim lookfor As String
Dim i As Integer
Dim j As Integer
Dim cellval As Variant
lookfor = "/"
' Column C
Const colNo = 3
' this for loop is not neccessary as you would loop
' over all columns from 1 to 20
' For j = 1 To 20
For i = 1 To 2000
' Split the string into an array
cellval = Split(Cells(i, colNo).Value, lookfor)
' In case there is no lookfor in the cell
' the next line would error out
On Error Resume Next
' in case there is a lookfor in the string
' the second item of the array will contain
' what you are after and will write it to column G
Cells(i, colNo + 4) = cellval(1)
' reset error handling
On Error GoTo 0
Next i
' Next j
结束子 `
如果我猜对了,你的代码的修复可能看起来像这样
Sub Copyvalue()
Dim lookfor As String
Dim i As Integer
Dim j As Integer
Dim cellval As Variant
lookfor = "/"
' Column C
Const colNo = 3
' this for loop is not neccessary as you would loop
' over all columns from 1 to 20
' For j = 1 To 20
For i = 1 To 2000
' Split the string into an array
cellval = Split(Cells(i, colNo).Value, lookfor)
' In case there is no lookfor in the cell
' the next line would error out
On Error Resume Next
' in case there is a lookfor in the string
' the second item of the array will contain
' what you are after and will write it to column G
Cells(i, colNo + 4) = cellval(1)
' reset error handling
On Error GoTo 0
Next i
' Next j
End Sub
进一步阅读拆分