Range selection with generated row location

zoog25

Active Member
Joined
Nov 21, 2011
Messages
418
Hello all,

I'm having a debugging issue on a set of code.

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Count > 1 Then Exit Sub


'Initiate Work
Dim lrow As Long
Dim NOCws As Worksheet
Dim TPws As Worksheet


Set NOCws = ThisWorkbook.Worksheets("NOC")
Set TPws = ThisWorkbook.Worksheets("Tract Parcels")


'Disable other sheet events
Application.EnableEvents = False
'Find last row in NOC Log
lrow = NOCws.Cells(Rows.Count, 5).End(xlUp).Row


'NOC Auto Entry After Map goes to Council
If Not Intersect(Target, Range("E:F")) Is Nothing Then
    If WorksheetFunction.CountA(Cells(Target.Row, "E").Resize(, 2)) = 2 Then
        MsgBox ("NOC Log Updating")
        With NOCws.Range("F4:F" & lrow)
            Set NOCTP = .Find(Cells(Target.Row, "G"), LookIn:=xlValues)
            Do
                If Not NOCTP Is Nothing Then
                    firstaddress = NOCTP.Address
                    If Cells(Target.Row, "H").Value = NOCws.Cells(NOCTP.Row, "G").Value Then
                        If Cells(Target.Row, "D").Value = NOCws.Cells(NOCTP.Row, "E").Value Then
                            MsgBox "NOC entry already made.", vbInformation, "ENTRY MADE"
                            GoTo DoneFinding
                        End If
                    Set NOCTP = .FindNext(NOCTP)
                    End If
                Else
                    lrow = lrow + 1
                    If MsgBox("Is this Private Site Project?", vbQuestion + vbYesNo, "Public or Private") = vbYes Then
                        NOCws.Cells(lrow, "A") = "PR"
                    Else
                        NOCws.Cells(lrow, "A") = "PUB"
                        NOCws.Range("W" & lrow & ":Y" & lrow).Select
                        With Selection.Interior
                            .Pattern = xlSolid
                            .PatternColorIndex = xlAutomatic
                            .ThemeColor = xlThemeColorLight1
                            .TintAndShade = 0.499984740745262
                            .PatternTintAndShade = 0
                        End With
                    End If
                    NOCws.Cells(lrow, "B") = Cells(Target.Row, "A").Value
                    NOCws.Cells(lrow, "C") = Cells(Target.Row, "B").Value
                    NOCws.Cells(lrow, "D") = Cells(Target.Row, "E").Value
                    NOCws.Cells(lrow, "E") = Cells(Target.Row, "D").Value
                    NOCws.Cells(lrow, "F") = Cells(Target.Row, "G").Value
                    If Cells(Target.Row, "H").Value = "" Then
                        NOCws.Range("G" & lrow).Select
                        With Selection.Interior
                        .Pattern = xlSolid
                            .PatternColorIndex = xlAutomatic
                            .ThemeColor = xlThemeColorLight1
                            .TintAndShade = 0.499984740745262
                            .PatternTintAndShade = 0
                        End With
                    Else
                        NOCws.Cells(lrow, "G") = Cells(Target.Row, "H").Value
                    End If
                    NOCws.Cells(lrow, "I") = Cells(Target.Row, "I").Value
                    MsgBox "Agreement has been added to NOC Log.", vbInformation, "ENTRY MADE"
                End If
            Loop While Not NOCTP Is Nothing
DoneFinding:
        End With
    End If
End If


Application.EnableEvents = True


End Sub

The section of in red that is giving me issues
Rich (BB code):
NOCws.Range("W" & lrow & ":Y" & lrow).Select

Now i have tried to do it several ways, but i get debug issues. What i'm trying to do is on sheet NOCws, the empty row indicated by lrow would select cells from "W" lrow to "Y" lrow. For whatever reason, the way i am doing this, is not being accepted. Can anyone please let me know what i'm doing wrong or is there a better way to use Range when you have a set columns but variable row.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The .Select requires NOCws to be the active sheet. Try merging that line with the With line below it like this:
Code:
                        With [COLOR=#ff0000]NOCws.Range("W" & lrow & ":Y" & lrow)[/COLOR].Interior
                            .Pattern = xlSolid
                            .PatternColorIndex = xlAutomatic
                            .ThemeColor = xlThemeColorLight1
                            .TintAndShade = 0.499984740745262
                            .PatternTintAndShade = 0
                        End With
Similarly change the other .Select.
 
Upvote 0
Thank you John_W. Making that changed worked. I tried it with several example and it works perfect.
 
Upvote 0

Forum statistics

Threads
1,215,222
Messages
6,123,709
Members
449,118
Latest member
MichealRed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top