Consulting

Results 1 to 11 of 11

Thread: Solved: Restart macro after blank cell

  1. #1

    Solved: Restart macro after blank cell

    Hi all, I have a code that can do some calculation for example

    Column A

    Blank
    Blank
    Blank
    5
    3
    2
    9
    8
    Blank
    100
    45
    55
    25
    0
    75
    2.5
    1.5
    3.5

    The code will find the matches between the first number after blank to the other numbers before next blank

    the equation is "x = i + j" then change font in i and j cells to be bold

    so the expected results would be

    Blank
    Blank
    Blank
    5
    3
    2
    9
    8
    Blank
    100
    45
    55
    25
    0
    75
    2.5
    1.5
    3.5

    My problem is
    1. when I run the code all numbers after number 5 will be taken into account to calculate for 5
    2. How can I stop the code and go to next 5 if the code found a blank cell?

    Any help would be appreciated.


    [VBA]Sub calculation()

    Dim finalrow As Long, _
    x As Long, _
    j As Long, _
    i As Long
    Dim lngPasteRow As Long

    finalrow = Cells(Rows.Count, 1).End(xlUp).Row
    For x = 4 To finalrow
    For j = x + 1 To finalrow
    For i = j + 1 To finalrow

    If Cells(x, 1) <> "" And Cells(x - 1, 1) = "" And Cells(x + 1, 1) <> "" Then

    If Cells(i, 1) + Cells(j, 1) = Cells(x, 1) Then


    With Cells(i, 1).Font
    .Bold = True
    End With
    With Cells(j, 1).Font
    .Bold = True
    End With

    End If


    ElseIf Cells(i, 1) Or Cells(j, 1) = "" Then

    'Go to next X

    End If

    Next i
    Next j
    Next x
    MsgBox "Finished"
    End Sub


    [/VBA]
    Attached Files Attached Files

  2. #2
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    Can you clarify what your requirements and expected results are?

    I have corrected an error in your code below.

    [VBA]ElseIf Cells(i, 1) = "" Or Cells(j, 1) = "" Then [/VBA]
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

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

    [vba]
    Sub calculation()

    Dim finalrow As Long, _
    nextblank As Long, _
    x As Long, _
    j As Long, _
    i As Long
    Dim lngPasteRow As Long

    finalrow = Cells(Rows.Count, 1).End(xlUp).Row
    For x = 4 To finalrow

    For j = x + 1 To finalrow

    nextblank = Cells(i, 1).End(xlDown).Row + 1
    For i = j + 1 To nextblank - 1

    If Cells(x, 1).Value <> "" And Cells(x - 1, 1).Value = "" And Cells(x + 1, 1).Value <> "" Then

    If Cells(i, 1).Value + Cells(j, 1).Value = Cells(x, 1).Value Then

    With Cells(i, 1).Font
    .Bold = True
    End With
    With Cells(j, 1).Font
    .Bold = True
    End With
    End If
    ElseIf Cells(i, 1).Value = "" Or Cells(j, 1).Value = "" Then

    'Go to next X
    End If
    Next i
    Next j
    Next x
    MsgBox "Finished"
    End Sub[/vba]
    ____________________________________________
    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

  4. #4
    Sorry for my late reply,

    @ BrianMH, in the expected result
    Blank
    Blank
    Blank
    5
    3
    2
    9
    8
    Blank
    100
    45
    55
    25
    0
    75
    2.5
    1.5
    3.5

    Number 3 and 2 are bold because 5 = 3+2
    Number 45 and 55, 75 and 25 are bold because 100 = 45 + 55, 100 = 75 + 25

    my problem is when I run the code, 1.5 and 3.5 are also calculated for 5. I would like to restart the code after blank cell to avoid that problem.


    @xld
    I got an error, Error 1004 Application defined or object defined error.

    Thanks,


  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    So you are basically chasing only whole numbers that sum up to the number and not decimal numbers?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    @Aussiebear,

    Thank you for your response. From the example I just wanted to show that 1.5 + 3.5 is equal to 5 (not 100) but I wanted only the combinations of numbers that're equal to 100 (first number after blank).

    To be clearer I added additional numbers.

    Blank
    Blank
    Blank
    5
    3
    2
    9
    8
    45
    55
    3.1
    1.9

    Blank
    100
    45
    55
    25
    0
    75
    2.5
    1.5
    3.5
    99
    1

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by copyt
    @xld
    I got an error, Error 1004 Application defined or object defined error.

    Thanks,

    On which code line and for which data row?
    ____________________________________________
    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
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    this line:
    [VBA]
    nextblank = Cells(i, 1).End(xlDown).Row + 1
    [/VBA]
    i is currently 0.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Thanks, t should have been

    [vba]
    nextblank = Cells(j, 1).End(xlDown).Row + 1 [/vba]
    ____________________________________________
    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

  10. #10
    @ mancubus and xld,

    Thank a lot for your kind helps.

  11. #11
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're wellcome but i did nothing. all credits must go to xld.

    and thanks for marking the thread as solved.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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