Why the row selected is displayed in negative value : Range ().value = Target.row

Excel addicted

New Member
Joined
Sep 17, 2019
Messages
14
Hello guys,
I have just joined this amazing place and I'm still novice in VBA . From the code below, I would like to display rows number of selected cells in cell "A4". The issue is I have the first number displayed in negative value without parenthesis. However I want to display a positive value between parenthesis.
Is something missing in the code
Thanks for your great help !
Code:
[COLOR=#111111][FONT=Roboto]
Private Sub Worksheet_SelectionChange(ByVal Target As Range)[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]If Not Intersect(Target, Range(" D22:I46 ")) Is Nothing Then[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]        Dim Cell As Range[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]        If Target.Count > 1 And Target.Count < 70 Then[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]        For Each Cell In Selection[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]        Range("A4").Value = Range("A4").Value & "(" & Cell.Row - 21 & ")"[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]        Next Cell[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]        Else[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]        Range("A4").Value = "(" & Target.Row - 21 & ")"[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]        End If[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]Else[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]Range("A4").ClearContents[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]End If[/FONT][/COLOR][COLOR=#111111][FONT=Roboto][/FONT][/COLOR][COLOR=#111111][FONT=Roboto]End Sub[/FONT][/COLOR]
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The code loops through each cell selected. With the first cell in the loop, it puts something like (1). Excel evaluates that entry to just its' numeric negative (like a currency value).

Just format A4 as text and that should fix it.

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Worksheet_SelectionChange([COLOR=darkblue]ByVal[/COLOR] Target [COLOR=darkblue]As[/COLOR] Range)
    [COLOR=darkblue]Dim[/COLOR] Cell  [COLOR=darkblue]As[/COLOR] Range
    Range("A4").ClearContents
    Range("A4").NumberFormat = "@" [COLOR=green]'Text format[/COLOR]
    [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] Intersect(Target, Range("D22:I46")) [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] Cell [COLOR=darkblue]In[/COLOR] Intersect(Target, Range("D22:I46"))
            Range("A4").Value = Range("A4").Value & "(" & Cell.Row - 21 & ")"
        [COLOR=darkblue]Next[/COLOR] Cell
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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