我们需要在字符串末尾的“/”之后或两个符号“/”之间将值从C列复制到新的G列。O

问题描述 投票:0回答:1

我们需要程序将字符串末尾的“/”之后的值或两个符号“/”之间的值从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

结束子 `

excel vba
1个回答
0
投票

如果我猜对了,你的代码的修复可能看起来像这样

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

进一步阅读拆分

© www.soinside.com 2019 - 2024. All rights reserved.