在不以" PDF"结尾的单元格上运行代码

时间:2017-07-19 17:29:31

标签: excel vba excel-vba

我正在尝试重命名,如果最后3个字符= pdf,我不希望代码运行。应该很简单,但是当我运行宏时,重命名工作正常,但它会删除以pdf结尾的每个单元格。

SearchChar = "pdf"

For Each bCell In rng.Cells
Select Case Len(bCell)
Case 2
    If Right(bCell, 3) <> SearchChar Then 'This must be wrong
        val = SearchSite & Left(bCell, 1) & "00" & Mid(bCell, 2, 1) & "1.pdf"
    End If
End Select
bCell.Value = val
Next

2 个答案:

答案 0 :(得分:1)

您是否尝试更改If语句以检查右侧3个字符是否为pdf,然后“没有”其他代码?类似于:

If Right(bCell,3)=SearchChar Then
'Nothing
Else
val = SearchSite & Left(r, 1) & "00" & Mid(r, 2, 1) & "1.pdf"
bCell.Value=val
End If

我的猜测是,最后3个字符是pdf,val =没有,所以打印出的是bCell.Value = nothing,所以删除它。我把它移到了其他部分。

答案 1 :(得分:0)

您可以尝试重命名不以“pdf”结尾的选择项

Set rng = Selection
SearchChar = "pdf"
Select Case n
    Case 2
        For Each r In rng
            If Right(r, 3) <> SearchChar Then 'This must be wrong
                r = SearchSite & Left(r, 1) & "00" & Mid(r, 2, 1) & "1.pdf"
            End If
        Next
End Select