Array or Loop how to question

Nothnless

Board Regular
Joined
Apr 28, 2016
Messages
142
Hi guys, I'm still learning and I'm not familiar with using loops which I think would be the solution for this issue.

So I have some code here that I wrote but I'm sure there is a much better way to input this using a loop or array:

Code:
If Range("B9").Value = "" Then    GoTo Handler
    Else
    Line2 = Range("B9").Value & "   " & Range("C9").Value & vbCrLf
    End If
    
    If Range("B10").Value = "" Then
    GoTo Handler
    Else
    Line3 = Range("B10").Value & "   " & Range("C10").Value & vbCrLf
    End If
    
    If Range("B11").Value = "" Then
    GoTo Handler
    Else
    Line4 = Range("B11").Value & "   " & Range("C11").Value & vbCrLf
    End If

I have like 20 more lines for this, please help
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What are Line2, Line3 and Line4? What does "Handler" do? It might be easier to help if you could explain in words what you are trying to do. Be as detailed as possible referring to specific cells, rows, columns and sheets.
 
Upvote 0
What are Line2, Line3 and Line4? What does "Handler" do? It might be easier to help if you could explain in words what you are trying to do. Be as detailed as possible referring to specific cells, rows, columns and sheets.

So what I'm doing here is referencing cells to have them displayed on an email.

The "Line"'s are just defined as strings and show what ever is in that cell on the email.
The handler is just saying if the cell is blank then go to the end of the email message and display the signature.

The cell references are to a table so if Line 3 is blank then that would be the end of the table so no need to look any further go to the handler.

Thanks for any help with this, the code works just fine I just know there is a better way.
 
Upvote 0
I'm not sure this is any better but you can try it. Change the range (in red) to suit your needs. You will have to add all the additional "Case Is" statements for the rest of the lines.
Code:
Sub test()
    Dim rng As Range, Line2 As String, Line3 As String, Line4 As String, Line5 As String [COLOR="#FF0000"]'add the additional strings for the remaining lines[/COLOR]
    For Each rng In Range("[COLOR="#FF0000"]B9:B30[/COLOR]")
        If rng = "" Then
            GoTo handler
        Else
            Select Case rng.Row
                Case Is = 9: Line2 = rng.Value & "   " & rng.Offset(0, 1).Value & vbCrLf
                Case Is = 10: Line3 = rng.Value & "   " & rng.Offset(0, 1).Value & vbCrLf
                Case Is = 11: Line4 = rng.Value & "   " & rng.Offset(0, 1).Value & vbCrLf
                Case Is = 12: Line5 = rng.Value & "   " & rng.Offset(0, 1).Value & vbCrLf
            End Select
        End If
    Next rng
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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