For loop stop on blank

Weeble

Board Regular
Joined
Nov 30, 2016
Messages
95
Office Version
  1. 365
So i've written a code to go thrue my drop down list and print every sheet.
I wan't to take it to the next level and do a For loop to do this for me.

Could anyone help me modify my code so that the loop goes thrue all the values but if it hits a "" space, stops.

Code:
Sub NastaButik()    Dim v As Variant
    With Sheets("Utskrift").Range("D2")
        If .Value = "" Then
            .Value = Sheets("Butiker").Range("D2").Value
        Else
            v = Application.Match(.Value, Sheets("Butiker").Range("D2:D75"), 0)
            If IsNumeric(v) Then
                .Value = Sheets("Butiker").Range("D2:D75").Cells(v + 1, 1).Value
                If Sheets("Utskrift").Range("K2") > 0 Then
                   ' ActiveSheet.PrintOut
                   MsgBox ("Printed")
                End If
            Else
                .Value = ""
            End If
        End If
    End With
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You could try

Code:
Sub NastaButik()    Dim v As Variant
    With Sheets("Utskrift").Range("D2")
        If .Value = "" Then
            .Value = Sheets("Butiker").Range("D2").Value
        Else
            v = Application.Match(.Value, Sheets("Butiker").Range("D2:D75"), 0)
            If IsNumeric(v) Then
                .Value = Sheets("Butiker").Range("D2:D75").Cells(v + 1, 1).Value
            If Trim(.Value) ="" then Exit Sub
                 If Sheets("Utskrift").Range("K2") > 0 Then
                   ' ActiveSheet.PrintOut
                   MsgBox ("Printed")
                End If
            Else
                .Value = ""
            End If
        End If
    End With [COLOR=#333333]End Sub
[/COLOR]
 
Last edited:
Upvote 0
Thank you Nemmi, this probably fixes stop issue, but not it looping thrue the list
 
Upvote 0
Bit confused as to what the steps are in this. Can you explain them?
 
Upvote 0
Assuming utskrift is a sheet you want to vary, something along these lines

Code:
Sub NastaButik()    
Dim v As Variant
Dim sht as Worksheet

For Each sht in ActiveWorkbook.Sheets
  If sht.name <> "Butiker" then
    With Sht.Range("D2")
        If .Value = "" Then
            .Value = Sheets("Butiker").Range("D2").Value
        Else
            v = Application.Match(.Value, Sheets("Butiker").Range("D2:D75"), 0)
            If IsNumeric(v) Then
                .Value = Sheets("Butiker").Range("D2:D75").Cells(v + 1, 1).Value
            If Trim(.Value) ="" then Exit Sub
                 If Sht.Range("K2") > 0 Then
                   ' ActiveSheet.PrintOut
                   MsgBox ("Printed")
                End If
            Else
                .Value = ""
            End If
        End If
    End With 
  End if
Next sht
End Sub
 
Last edited:
Upvote 0
Sheet will always be "Butiker", I have a drop down list in D2 that can varey in length. My explination was abit bad.

Sheet is fixed on 'Butiker' drop down list is in D2.

This code

Code:
[COLOR=#333333]  Else[/COLOR]            v = Application.Match(.Value, Sheets("Butiker").Range("D2:D75"), 0)
            If IsNumeric(v) Then
                .Value = Sheets("Butiker").Range("D2:D75").Cells(v + 1, 1).Value
                If Sheets("Utskrift").Range("K2") > 0 Then
                   ' ActiveSheet.PrintOut
                   MsgBox ("Printed")
                End If [COLOR=#333333]            Else[/COLOR]
goes to next item in the list.. I think this is the part that needs to modify in able to FOR loop.
 
Upvote 0
Try

Code:
LastRowNo = [COLOR=#333333]Sheets("Butiker")[/COLOR].Range("D65536").End(xlUp).Row 'Starts half way down the sheet upwards
[COLOR=#333333][COLOR=#333333] 
 Else[/COLOR]            v = Application.Match(.Value, Sheets("Butiker").Range("D2:D" & LastRowNo), 0)
            If IsNumeric(v) Then
                .Value = Sheets("Butiker").Range("D2:D" & [/COLOR]LastRowNo[COLOR=#333333]).Cells(v + 1, 1).Value
                If Sheets("Utskrift").Range("K2") > 0 Then
                   ' ActiveSheet.PrintOut
                   MsgBox ("Printed")
                End If [COLOR=#333333]            Else[/COLOR][COLOR=#333333]
[/COLOR]
[/COLOR]
 
Upvote 0
Thank you Nemmi69. I figued it out eventualy. I was too blurry when I tried to explained I feel.
This is how I solved it.

Code:
Sub NastaButik()    Dim v As Variant
    With Sheets("Utskrift").Range("D2")
     '   If .Value = "" Then
           .Value = Sheets("Butiker").Range("D2").Value
      Do While Sheets("Utskrift").Range("D2") > ""
            v = Application.Match(.Value, Sheets("Butiker").Range("D2:D75"), 0)
            If IsNumeric(v) Then
                .Value = Sheets("Butiker").Range("D2:D75").Cells(v + 1, 1).Value
                If Sheets("Utskrift").Range("L2") > 0 And Sheets("Utskrift").Range("L3") > 0 Then
                   ActiveSheet.PrintOut From:=1, To:=1
                   'MsgBox ("Printed")
                   Else
                   ActiveSheet.PrintOut From:=1, To:=2
                End If
            Else
                .Value = ""
            End If
        Loop
        
      
    End With
End Sub

Thank you for the support though!
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,668
Members
448,977
Latest member
moonlight6

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