Solved

Update VBA loop

Posted on 2015-01-19
4
73 Views
Last Modified: 2015-01-19
Have existing VBA would like to have it updated to address the potential scenario


1) If the user changes his mind after selecting 'yes' and changes it to 'no' or 'n/a', the data will still be in the parts request sheet.

2) To take it a step further if he does yes then no and then yes again, the information will be there twice.

Thanks

===========
Prior related question: http:Q_28598192.html
0
Comment
Question by:jmac001
  • 2
4 Comments
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40558328
Try this
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lngLastRow As Long
Dim lngRow As Long
Dim lngReplace As Long

With Sheets("PARTS REQUEST")
    For lngRow = 9 To .UsedRange.Rows.Count
        If .Cells(lngRow, 3) = "" Then
            lngLastRow = lngRow
            Exit For
        End If
    Next
End With
If lngLastRow = 0 Then
    MsgBox "Too much data is already on the PARTS REQUEST sheet. Add more lines"
    Exit Sub
End If
If Not Intersect(Target, Range("S:S")) Is Nothing Then
    With Sheets("PARTS REQUEST")
        If LCase(Target) = "yes" Then
            .Cells(lngLastRow, 2) = Sheets("Sheet1").Cells(Target.Row, 2)
            .Cells(lngLastRow, 3) = Sheets("Sheet1").Cells(Target.Row + 5, 1)
        Else
            For lngRow = 9 To lngLastRow
                If .Cells(lngRow, 2) = Sheets("Sheet1").Cells(Target.Row, 2) Then
                    For lngReplace = lngRow To lngLastRow
                        If .Cells(lngReplace, 2) <> "" Then
                            .Cells(lngReplace, 2) = .Cells(lngReplace + 1, 2)
                            .Cells(lngReplace, 3) = .Cells(lngReplace + 1, 3)
                        Else
                            Exit For
                        End If
                    Next
                    Exit For
                End If
            Next
        End If
    End With
End If

End Sub

Open in new window

0
 

Author Closing Comment

by:jmac001
ID: 40558635
Thank you for thinking of what-if scenario. Works great
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40558637
You're welcome.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question