Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Update VBA loop

Posted on 2015-01-19
4
Medium Priority
?
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Martin Liss earned 2000 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 49

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

730 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