Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    Loop or for/next?

    Ive put the code I want to use in a loop and I get subscript out of range:

    Code:
    Sub datasort()
    Dim celltxt As String
    Dim celltxt2 As String
     
    
    'copy name
    Range("r1").Select
    Selection.Copy
    Range("i2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'copy date
    Range("c2").Select
    Selection.Copy
    Range("j2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'copy PO
    Range("d2").Select
    Selection.Copy
    Range("k2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'copy hours
    Range("f2").Select
    Selection.Copy
    Range("l2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
     Range("A2:F2").Select
        Application.CutCopyMode = False
        Selection.ListObject.ListRows(1).Delete
        
    'copy miles
    Range("f2").Select
    Selection.Copy
    Range("m2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
     Range("A2:F2").Select
        Application.CutCopyMode = False
        Selection.ListObject.ListRows(1).Delete
        
    'validate expenses'''''''''''
     
     
    celltxt2 = ActiveSheet.Range("e2").Text
        celltxt = ActiveSheet.Range("s1").Text
        
        
    If InStr(1, celltxt2, "Exp") Then
        If InStr(1, celltxt, "TRUE") > 0 Then
      
        Range("A2:F2").Select
        Application.CutCopyMode = False
        Selection.ListObject.ListRows(1).Delete
        Else
        MsgBox ("Expenses")
        Exit Sub
        End If
    ''''''''''''''''''''''''''''
    Else
    MsgBox "raw data is formatted incorrectly"
    Exit Sub
    Workbook.Save
     
     
     
    End If
     
     
     
    
    'insert new line
      Range("I2:M2").Select
       
        Application.CutCopyMode = False
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        
        '''''''''''loop before here''''''''''''''''''''
    
    
        '''''email''''
         Dim rng As Range
     rng = "test@test.co.uk"
     ActiveWorkbook.SendMail Recipients:=rng, Subject:="The report you need"
     'Add a button to the sheet
    Exit Sub
        
        
    End Sub

    Everything that happens before the annotation " loop before here" is what I want to loop. If cell B2 is not null then....

    I have deleted the loop when I was trying to use the for and next function but that was even less successful.

    The loop DID work until the error, then it did not pass the loop to the email.

    I'm hoping someone can help. thanks.

  2. #2
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Code:
     Do While Not IsEmpty(Range("b2"))
    
    CODE
    
    loop
    Is the code I am/was using.. a different error now. Error 91. object variable or with block variable not set.

    this happens where B2 is null

  3. #3
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    It was the new variable rng causing the issue. Seems okay now. I was defining it as a range and entering just text. That's what I get

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 12
    Last Post: 06-05-2015, 04:27 PM
  2. Replies: 13
    Last Post: 08-20-2014, 09:17 AM
  3. Replies: 17
    Last Post: 04-07-2014, 07:48 PM
  4. Replies: 3
    Last Post: 03-10-2013, 07:04 AM
  5. VBA Loop
    By DreamOn in forum Programming
    Replies: 4
    Last Post: 06-25-2010, 03:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums