Question

I have a code that searches for the first empty cell in column E if a cell is clicked:

If Target.Cells.Count = 1 Then
    If Not Intersect(Target, [B2]) Is Nothing Then _
        Range("E:E").Find(vbNullString, [E3], , , , xlNext).Select
End If

Now I want to change the code where it finds the first empty cell and puts text into a cell on the same row. So if I just go down in the excell sheet, withouth clicking, it puts text into a cell in column B (for instance "add value"). And of course if I click the cell I still go down to the first empty cell.

I do not know where to start, so any help would be much appreciated!

Was it helpful?

Solution

Short Answer the code below will put "<--Empty" in the cell next to the empty cell:

Range("E:E").Find(vbNullString, [E3], , , , xlNext).Offset(0,1) = "<--Empty"

The Rnage.Find method returns cells matching the criteria sequentially. To return an adjacent cell, use the Range.Offset method. Rather than selecting, just type the string into the cell.

Long Answer: Personally, I try not to use the Range.Find method because all of those arguments you did not specify default to whatever your PC had last. This can cause different behavior on different PCs. Instead, I would use a for loop to loop through the range of interest

 For i = 1 to Max
     if RangeOfInterest(i,1) = vbNullString then RangeOfInterest(i,1).Offset(0,1) = "<--Empty"
 Next i
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top