VBA Excel - how to add for next to macro ISBN

Anoniem
2024-08-06T02:42:23+00:00

Where do I add the For next statement in below macro?

Sub ISBN()

Dim ISBN As String  
Dim cell As Range  
  
ISBN = InputBox("Enter ISBN nr")  

Set cell = Range("F11:f9940").Find(What:=ISBN, LookIn:=xlValues, LookAt:=xlWhole, \_  
                    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)  
If cell Is Nothing Then   
MsgBox ("ISBN nr not found")  
Sheets("Not found ISBN nrs").Select  
ActiveCell.FormulaR1C1 = ISBN  
ActiveCell.Offset(1, 0).Range("A1").Select  
ActiveSheet.Previous.Select  
  
Else  
  cell.Activate  
With Selection.Interior  
    .Pattern = xlSolid  
    .PatternColorIndex = xlAutomatic  
    .Color = 65535  
    .TintAndShade = 0  
    .PatternTintAndShade = 0  
      

End With

End If

When I tried myself I got a statement back saying Next without for.....

Microsoft 365 en Office | Excel | Voor thuisgebruik | Windows

Vergrendelde vraag. Deze vraag is gemigreerd vanuit de Microsoft Ondersteuning-community. U kunt met een stem aangeven of de inhoud nuttig is, maar u kunt geen opmerkingen of antwoorden toevoegen of de vraag volgen.

0 opmerkingen Geen opmerkingen
Antwoord geaccepteerd door vraagauteur
Anoniem
2024-08-06T08:13:47+00:00

Dit antwoord is automatisch vertaald. Als gevolg hiervan kunnen er grammaticale fouten of vreemde formuleringen zijn.

Hoi Fedde Schaafsma,

Bedankt voor uw bezoek aan de Microsoft-community.

Het lijkt erop dat je een For each + Next in uw code om alle gevonden cellen te herhalen. Hier is een verbeterde versie van de code, die u kunt aanpassen aan uw behoeften:

Sub ISBN()

   Dim ISBN As String

   Dim cell As Range

   Dim isbnList As Range

   Dim isbnCell As Range

   ' Define the range containing the list of ISBN numbers to be searched

   Set isbnList = Range("G1:G100") ' Change this to your actual range

   ' Loop through each cell in the range

   For Each isbnCell In isbnList

       ISBN = isbnCell.Value

       ' Find the ISBN in the specified range

       Set cell = Range("F11:F9940").Find(What:=ISBN, LookIn:=xlValues, LookAt:=xlWhole, _

                      SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

       ' Check if the cell is found

       If cell Is Nothing Then

           ' ISBN not found

           MsgBox "ISBN nr " & ISBN & " not found"

           ' Record the ISBN in the "Not found ISBN nrs" sheet

          Sheets("Not found ISBN nrs").Select

           ActiveCell.FormulaR1C1 = ISBN

          ActiveCell.Offset(1, 0).Select

       Else

           ' Highlight the cell where the ISBN is found

          cell.Select

           With Selection.Interior

              .Pattern = xlSolid

              .PatternColorIndex = xlAutomatic

               .Color = 65535

              .TintAndShade = 0

              .PatternTintAndShade = 0

           End With

       End If

       ' Ensure the original sheet is selected after processing each ISBN

      ActiveSheet.Previous.Select

   Next isbnCell

End Sub

Laat het me gerust weten als je nog vragen hebt.

Hartelijke groeten

Jonathan Z - MSFT | Specialist in ondersteuning voor de Microsoft-community

Was dit antwoord nuttig?

1 persoon vond dit antwoord nuttig.
0 opmerkingen Geen opmerkingen

1 extra antwoord

Sorteren op: Oudste
  1. Anoniem
    2024-08-07T01:45:23+00:00

    Jonathan,

    I had to tweak your macro version of ISBN in order for me to work correctly. Your version worked OK but did not look-up the ISBN number nor did it add the not found ISBN nr to the sheet "Not found ISBN nrs". I missed the Inputbox I used to manually scan ISBN nrs, so I added this and re-worked the macro. I also added a few additions to the macro I did not think of yesterday.

    All in all, I am very grateful to you for helping me out with this.

    Was dit antwoord nuttig?

    0 opmerkingen Geen opmerkingen