本文介绍了如何在VBA中循环Range.Find?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBA的新手,所以请不要觉得太苛刻.话虽如此,以下是我对 Range.Find 的问题.

I am new to VBA so please don't judge too harsh. Having said that below is my issue with Range.Find.

我有一个交叉表,其中的一列具有指向图片的"https"链接;而且我有一个工作正常的VBA,可以将这些链接转换为该列每个单元格中的实际图片.但是,我的问题是,当我在交叉表中添加另一列或四处移动列时,我的VBA停止工作,并且最终得到的链接没有实际图片(因为图片代码设置为链接所在的初始列).

I have a crosstab with a column that has "https" link to pictures; and I have a working VBA to turn these links into actual pictures in each cell for that column. However, my issue is when I add another column into the Crosstab or move column around, my VBA stops working and I end up with my links without actual pictures (since, the picture code is set to the initial column where my links reside).

我认为应该有一种方法可以通过使用 Range.Find 使它更加动态.我已经设法找到有关 Range.Find 的信息,但是我的代码无法正常工作.反正有人可以帮忙吗?

I figured there should be a way to make it more dynamic by using Range.Find. I have managed to find information on Range.Find, but my code won't work. Is there anyway anyone could help out?

这是代码:

Function picRng() As Range
    Set picRng = ActiveSheet.Range("A1:Z1000")
    Set rngFindValue = ActiveSheet.Range("A1:Z1000").Find(what:="http", Lookat:=xlPart)
    Do
      Set rngFindValue = Search.FindNext(rngFindValue)
    Loop While Not rngFindValue is Nothing
End Function

推荐答案

如果要遍历搜索参数的所有实例,此处是对代码的更正

if you want to loop thru all the instances of the search arguments here is the correction of your code

Function picRng() As Range
    Set picRng = ActiveSheet.Range("A1:Z1000")
    Set rngfindvalue = picRng.Find(what:="http", Lookat:=xlPart)
    If Not rngfindvalue Is Nothing Then
        rngFirstAddress = rngfindvalue.Address
        Do
            MsgBox rngfindvalue.Address
            Set rngfindvalue = picRng.FindNext(rngfindvalue)
        Loop Until rngfindvalue Is Nothing Or rngfindvalue.Address = rngFirstAddress
    End If
End Function

这篇关于如何在VBA中循环Range.Find?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:38