Consulting

Results 1 to 8 of 8

Thread: Control strings

  1. #1

    Control strings

    Hi guys,

    I'm struggling to fix this and i can't find a way to go ahead.

    I have to fix two things in this dataset:

    1) including a check that skips "expired" and "null" lines from a loop

    Copy of benchmark UK expansion fix.xlsm

    In the attached you will see that there is a module that duplicates line from GANNT sheet into EXPANDED PERIODS times the value of duration cells. GANNT sheet is populated with another module from another source (that i had to remove to have 1MB file). When the sub breakSeasonality runs, it stops when the first row contains "expired" or "null" values. I need to add a string that checks if there are such values and if yes skips the line until a good one is found.

    I've tried to put something but i miserably failed

    Hope i can get your help

  2. #2
    VBAX Contributor D_Marcel's Avatar
    Joined
    Feb 2012
    Location
    Tokyo
    Posts
    117
    Location
    Welcome to the forum albe.btz!
    Unfortunately I can't open your file right now, but instead of using Do While and Loop, you can set the entire range and then use FOR and NEXT.

    [VBA]LAST_ROW = ActiveSheet.UsedRange.Rows.Count[/VBA]

    And then:

    [VBA]FOR I = 1 TO LAST_ROW
    'Your code here
    NEXT I[/VBA]

    This way, you'll perform the desirable instructions to the last row of your worksheet.

    Hope it helps!

    Regards,

    Douglas
    "The only good is knowledge and the only evil is ignorance". Socrates

  3. #3

    Control Strings

    Quote Originally Posted by D_Marcel View Post
    Welcome to the forum albe.btz!
    Unfortunately I can't open your file right now, but instead of using Do While and Loop, you can set the entire range and then use FOR and NEXT.

    [VBA]LAST_ROW = ActiveSheet.UsedRange.Rows.Count[/VBA]

    And then:

    [VBA]FOR I = 1 TO LAST_ROW
    'Your code here
    NEXT I[/VBA]

    This way, you'll perform the desirable instructions to the last row of your worksheet.

    Hope it helps!

    Regards,

    Douglas

    Hi Douglas,

    let me see if i got it right, this is my current code:

    Sub breakSeasonality()
    
    Dim DayCount
    Dim My_Range As Range
    Dim Ceiling_Range As Range
    Dim CellaCerc
    Sheets("GANNT").Activate
    Set My_Range = Sheets("GANNT").Range("A1")
    Sheets("EXPANDED PERIODS").Activate
    Set Ceiling_Range = Sheets("EXPANDED PERIODS").Range("A1")
    Do While My_Range.Offset(1, 0) <> ""
        Set My_Range = My_Range.Offset(1, 0)
        For DayCount = 1 To My_Range.Offset(0, 12)
            Set Ceiling_Range = Ceiling_Range.Offset(1, 0)
            'Ceiling_Range.Select
            Ceiling_Range = My_Range.Offset(0, 9) + DayCount - 1
            Ceiling_Range.Offset(0, 1) = "TBD" ' QF SEASONAL
            For Each CellaCerc In Sheets("Key Criteria").Range("A2", "A23")
                If (((My_Range.Offset(0, 9) + DayCount - 1) >= CellaCerc.Offset(0, 1)) And ((My_Range.Offset(0, 9) + DayCount - 1) <= CellaCerc.Offset(0, 2))) Then
                    Ceiling_Range.Offset(0, 1) = CellaCerc
                    Exit For
                End If
            Next
            Ceiling_Range.Offset(0, 2) = My_Range.Offset(0, 1) ' ORI
            Ceiling_Range.Offset(0, 3) = My_Range.Offset(0, 2) ' DEST
            Ceiling_Range.Offset(0, 4) = My_Range.Offset(0, 0) ' CXR
            Ceiling_Range.Offset(0, 5) = My_Range.Offset(0, 3) ' Fare Basis
            Ceiling_Range.Offset(0, 6) = My_Range.Offset(0, 4) ' RBD
            Ceiling_Range.Offset(0, 7) = My_Range.Offset(0, 5) ' CLASS
            Ceiling_Range.Offset(0, 8) = My_Range.Offset(0, 6) ' LEVEL
            Ceiling_Range.Offset(0, 9) = My_Range.Offset(0, 7) ' TFC
            Ceiling_Range.Offset(0, 10) = My_Range.Offset(0, 8) ' AIF
        Next
    Loop
    End Sub
    Where would you insert your fixes?

    cheers and thank you

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Change

            For Each CellaCerc In Sheets("Key Criteria").Range("A2", "A23")
                If (((My_Range.Offset(0, 9) + DayCount - 1) >= CellaCerc.Offset(0, 1)) And ((My_Range.Offset(0, 9) + DayCount - 1) <= CellaCerc.Offset(0, 2))) Then
                    Ceiling_Range.Offset(0, 1) = CellaCerc
                    Exit For
                End If
            Next
    to
           
            For Each CellaCerc In Sheets("Key Criteria").Range("A2", "A23")
            
                If My_Range.Offset(0, 9).Value = "expired" Then
                
                    'do nothing
                ElseIf (My_Range.Offset(0, 9).Value + DayCount - 1) >= CellaCerc.Offset(0, 1) And _
                    (My_Range.Offset(0, 9).Value + DayCount - 1) <= CellaCerc.Offset(0, 2) Then
                    Ceiling_Range.Offset(0, 1) = CellaCerc
                    Exit For
                End If
            Next
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5

    Control Strings

    Quote Originally Posted by albe.btz View Post
    Hi Douglas,

    let me see if i got it right, this is my current code:

    Sub breakSeasonality()
    
    Dim DayCount
    Dim My_Range As Range
    Dim Ceiling_Range As Range
    Dim CellaCerc
    Sheets("GANNT").Activate
    Set My_Range = Sheets("GANNT").Range("A1")
    Sheets("EXPANDED PERIODS").Activate
    Set Ceiling_Range = Sheets("EXPANDED PERIODS").Range("A1")
    Do While My_Range.Offset(1, 0) <> ""
        Set My_Range = My_Range.Offset(1, 0)
        For DayCount = 1 To My_Range.Offset(0, 12)
            Set Ceiling_Range = Ceiling_Range.Offset(1, 0)
            'Ceiling_Range.Select
            Ceiling_Range = My_Range.Offset(0, 9) + DayCount - 1
            Ceiling_Range.Offset(0, 1) = "TBD" ' QF SEASONAL
            For Each CellaCerc In Sheets("Key Criteria").Range("A2", "A23")
                If (((My_Range.Offset(0, 9) + DayCount - 1) >= CellaCerc.Offset(0, 1)) And ((My_Range.Offset(0, 9) + DayCount - 1) <= CellaCerc.Offset(0, 2))) Then
                    Ceiling_Range.Offset(0, 1) = CellaCerc
                    Exit For
                End If
            Next
            Ceiling_Range.Offset(0, 2) = My_Range.Offset(0, 1) ' ORI
            Ceiling_Range.Offset(0, 3) = My_Range.Offset(0, 2) ' DEST
            Ceiling_Range.Offset(0, 4) = My_Range.Offset(0, 0) ' CXR
            Ceiling_Range.Offset(0, 5) = My_Range.Offset(0, 3) ' Fare Basis
            Ceiling_Range.Offset(0, 6) = My_Range.Offset(0, 4) ' RBD
            Ceiling_Range.Offset(0, 7) = My_Range.Offset(0, 5) ' CLASS
            Ceiling_Range.Offset(0, 8) = My_Range.Offset(0, 6) ' LEVEL
            Ceiling_Range.Offset(0, 9) = My_Range.Offset(0, 7) ' TFC
            Ceiling_Range.Offset(0, 10) = My_Range.Offset(0, 8) ' AIF
        Next
    Loop
    End Sub
    Where would you insert your fixes?

    cheers and thank you

    I tried to change do while and loop but when it finds a row with the current situations it crashes:

    CXR ORI DEST Fare basis RBD Class Level TFC AIF Start Travel Date End Travel Date Sale End Date Duration
    CX LON AKL KLARRGB8 K Y 1279 226 1504.5 expired expired 31/03/2019 null



    that was the purpose of my thread: finding a solution to skip these lines
    Last edited by albe.btz; 08-23-2017 at 08:13 AM. Reason: correction

  6. #6
    Quote Originally Posted by xld View Post
    Change

            For Each CellaCerc In Sheets("Key Criteria").Range("A2", "A23")
                If (((My_Range.Offset(0, 9) + DayCount - 1) >= CellaCerc.Offset(0, 1)) And ((My_Range.Offset(0, 9) + DayCount - 1) <= CellaCerc.Offset(0, 2))) Then
                    Ceiling_Range.Offset(0, 1) = CellaCerc
                    Exit For
                End If
            Next
    to
           
            For Each CellaCerc In Sheets("Key Criteria").Range("A2", "A23")
            
                If My_Range.Offset(0, 9).Value = "expired" Then
                
                    'do nothing
                ElseIf (My_Range.Offset(0, 9).Value + DayCount - 1) >= CellaCerc.Offset(0, 1) And _
                    (My_Range.Offset(0, 9).Value + DayCount - 1) <= CellaCerc.Offset(0, 2) Then
                    Ceiling_Range.Offset(0, 1) = CellaCerc
                    Exit For
                End If
            Next
    it seems working, however it gets to a row and stops saying: runtime error 113, type mismatch

    i checked the row (after putting a debug.print) and it says row 69 but there is nothing wrong in that one....+

    the error sits in this string: For DayCount = 1 To My_Range.Offset(0, 12)

    i just tried to check it with a dumm data where after 16 duplications (the specified value in the cell) it stopped....

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Probably something wrong in this cell My_Range.Offset(0, 12)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8

    SOLVED

    It's solved. I just put an IF statement before the loop. easy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •