can't add 1 to current cell value

Viewed 59

I can't get past this. I am try'ing to add auto numbering to A column, but for some reason I cannot add 1 to to the value. I am not sure why and maybe there is easier way to work around it ?

all module code:

 Dim rng As Range
 Set rng = Cells.Find(what:="*", after:=Cells(2), 
 searchdirection:=xlPrevious, searchorder:=xlByColumns)

 b = rng.Offset(0, 0).Address
 a = rng.Offset(1, 0).Address
 s = ActiveCell.Offset(-1, -1).Address

 With Range(s, ActiveCell.Offset(-1, 11).Address).Borders(xlEdgeBottom)
     .LineStyle = xlContinous
     .Weight = xlThin
End With

Worksheets("Fakturos").Range(a, s).Merge

This is where I get stuck:

If (Range(b).Value <> " ") Then
 'Range(a).Value + 1
Else
  Range(b).Value = 1
End If
2 Answers

It should be

  Range(a).Value = Range(a).Value + 1

But it will act weird if it isn't a number in the field.

If (Range(b).Value <> " ") compares the value with a string, which is exactly one empty space long. Probably, it should compare with non-empty string, like this:

If (Trim(Range(b))>0) Then
    Range(a).Value = Range(a).Value + 1
Else
    Range(b).Value = 1
End If

Or actually you may consider, passing the value of Range(b) to a numeric variable, and compare whether it is different than 0:

Dim bValue As Double
bValue = Range(b)

If bValue <> 0 Then
   Range(a).Value = Range(a).Value + 1
Else
   Range(b).Value =  1
End If
Related