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
 
I managed to fix this problem with using two collections and an array to handle the import.
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I managed to fix this problem with using two collections and an array to handle the import.
The original question's answer is "the wrong parameter used with the FindNext method" and this is explained in posts #6 and #8, and more information about your data structure and the current code is also asked in both #8 and #10 to provide better solutions since there are logical and structural mistakes in the current code. On the other side, how you managed to fix the problem by using some other objects is not very clear unless you provide a working code. Therefore, I removed the solution mark from your own post. You can either post the working code and then it would be perfectly fine to mark your own post as the solution or respond to helpers' questions to seek more help. Either way, it will be more helpful for future readers.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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