Excel VBA Range FindNext not working

nutbolt

New Member
Joined
Apr 2, 2021
Messages
17
Office Version
  1. 2019
I'm getting error unable to get the FindNext property of the range class? What am I doing wrong in the code?

VBA Code:
Private Sub cmdInsertPhoto1_Click()
'insert the photo1 from the folder into each worksheet
Dim ws As Worksheet
Dim fso As FileSystemObject
Dim folder As folder
Dim rng As Range, cell As Range
Dim strFile As String
Dim imgFile As String
Dim localFilename As String
Dim pic As Picture
Dim findit As String

 
 Application.ScreenUpdating = True
 
 'delete the two sheets if they still exist
 For Each ws In ActiveWorkbook.Worksheets
  If ws.Name = "PDFPrint" Then
        Application.DisplayAlerts = False
        Sheets("PDFPrint").Delete
        Application.DisplayAlerts = True
  End If
 Next
 
 For Each ws In ActiveWorkbook.Worksheets
  If ws.Name = "DataSheet" Then
        Application.DisplayAlerts = False
        Sheets("DataSheet").Delete
        Application.DisplayAlerts = True
  End If
 Next
        
   
    Set fso = New FileSystemObject
    Set folder = fso.GetFolder(ActiveWorkbook.Path & "\Photos1\")
      
    'Loop through all worksheets
    For Each ws In ThisWorkbook.Worksheets
    ws.Select
        
         Set rng = Range("A:A")
        
         For Each cell In rng
          If cell = "CG Code" Then
          'find the next adjacent cell value of CG Code
           strFile = cell.Offset(0, 1).Value 'the cg code value
           imgFile = strFile & ".png" 'the png imgFile name
           localFilename = folder & "\" & imgFile 'the full location
                   
           'find Photo1 cell and select the adjacent cell to insert the image
           findit = Range("A:A").Find(what:="Photo1", MatchCase:=True).Offset(0, 1).Select
           ActiveCell.EntireRow.RowHeight = 200 'max row height is 409.5
                
           Set pic = ws.Pictures.Insert(localFilename)
             With pic
                .ShapeRange.LockAspectRatio = msoFalse
                .ShapeRange.Width = 200
                .ShapeRange.Height = ActiveCell.MergeArea.Height
                .ShapeRange.Top = ActiveCell.MergeArea.Top
                .ShapeRange.Left = ActiveCell.MergeArea.Left
                .Placement = xlMoveAndSize
             End With
             
            'find next photo1
           Dim finditnext As Range
           Set finditnext = rng.FindNext("Photo1")
           
            
          End If
            
            'delete photo after insert
            'Kill localFilename
            
         Next cell
    
    Next ws
    
 
    
    Application.ScreenUpdating = True
    
     ' let user know its been completed
     MsgBox ("Worksheets created")
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Coud you check it again ?

Dim folder As folder

There is no such thing.

try,

Dim folder As String
Its part of the FileSystemObject class in Excel
Screenshot 2023-01-23 211759.jpg
 
Upvote 0
Could you change the variable name something like fldr instead of keeping it as object name...
 
Upvote 0
The argument to FIndNext should be the previously found cell, not the search text.
 
Upvote 0
The argument to FIndNext should be the previously found cell, not the search text.
You mean like Set finditnext = rng.FindNext(findit) ? I tried that as well which doesn't give me the findnext
 
Upvote 0
You should keep using the same variable:

Code:
Set findit = rng.FindNext(findit)

and you need to check to make sure it’s not the one you found in the first place or you’ll get stuck in a loop. I do t understand your current structure though - why are you looping through every cell in the range and doing the same find operation(s) for each?
 
Upvote 0
You should keep using the same variable:

Code:
Set findit = rng.FindNext(findit)

and you need to check to make sure it’s not the one you found in the first place or you’ll get stuck in a loop. I do t understand your current structure though - why are you looping through every cell in the range and doing the same find operation(s) for each?
There is two things I need to search for in each worksheet, the CG Code and the Photo1 rows, it picks up the CG Code which matches the external png image files, which it will then inserts the png file in the photo1 row. If you have a code example of how to get this working would be great, I'm not so great on find and find next perhaps it could be done with For Each?
 
Upvote 0
What is not clear is your data layout and what you are trying to do.
1) You are looking in Column A for the 1st occurence of "CG Code"
2) You are then looking in the "same" column A for an occurence of "Photo1"
3) You are then looking in the "same" column A for a 2nd occurence of "Photo1"

• Does it stop after finding the 2nd occurence of Photo1 or are you looking for all occurences ?
• What happens when you move on to the next and following occurences of "GC Code" you have already used up all your Photo1 slots ?
 
Upvote 0

Forum statistics

Threads
1,215,227
Messages
6,123,738
Members
449,116
Latest member
alexlomt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top