VBA Search for non-empty cell. insert random number on next cell

AaronBallard

New Member
Joined
Apr 2, 2013
Messages
4
Hello All,

I have had a good forum/Google search to try and work this problem out, but had no luck.

It has been too long since I have used VB and I've forgotten how to do basic things.

I'm looking for VBA code to do the following:
Search Column B (after B1) for non-empty cells. If the cells have text, then insert a random number between the range of 10000-90000 in the A column next to the cell. e.g.


Col A Col B
123441 Mon
<blank> <blank>
312312 Mon
<blank> <blank>
<blank> <blank>
<blank> <blank>
213123 Tues
<blank> <blank>

Many Thanks.</blank></blank></blank></blank></blank></blank></blank></blank></blank></blank>
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Sub randnum()


Dim Lrow As Long
Dim x As Long
Lrow = Range("B" & Rows.Count).End(xlUp).Row
For x = 1 To LastRow
If IsEmpty(Range("B" & x).Value) = False Then
Range("C" & x).Value = Int((90000 - 10000 + 1) * Rnd + 10000)
End If
Next x
End Sub
 
Upvote 0
Welcome. I inserted random numbers in Column C. you can replace Range("C" & x).Value with Range("A" & x).Value. This way it will insert random values in A column.
 
Upvote 0
I rechacked it and it did not run here too... I found mistake though...but still not sure if it will run at 2003


Dim Lrow As Long
Dim x As Long
Lrow = Range("B" & Rows.Count).End(xlUp).Row
For x = 1 To Lrow
If IsEmpty(Range("B" & x).Value) = False Then
Range("A" & x).Value = Int((90000 - 10000 + 1) * Rnd + 10000)
End If
Next x
End Sub
 
Last edited:
Upvote 0
That's Great. Thanks

But I can't work out why it is changing cell A2, it is generating a random number in cell A2
A2 = _event_id
b2 = _day_of_week
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,749
Members
449,094
Latest member
dsharae57

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