Solved

Update VBA loop

Posted on 2015-01-19
4
78 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 47

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 47

Expert Comment

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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

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