PDA

View Full Version : Basic Vba help



dasnoob
05-28-2011, 04:05 AM
Hi!

I'd have to build a macro that collects 5 numbers from the user, and 5 numbers from the sheet. And if any of those numbers are negative (-), it is supposed to write them out on the sheet.

Could somebody please help me with this? :think:

Chabu
05-28-2011, 04:51 AM
This will get you going
Public Sub numbers()
Dim x As Integer
Dim n As Integer
Dim aNumber As Integer
Dim sheet As Worksheet
Set sheet = ActiveSheet
n = 1
For x = 1 To 5
aNumber = InputBox("Gimme a number: ")
If aNumber < 0 Then
sheet.Cells(n, 2).Value = aNumber 'this assumes the negative number go in column B starting at 1
n = n + 1
End If
Next x
For x = 1 To 5
aNumber = sheet.Cells(x, 1).Value 'this assumes the numbers on the sheet are in A1 to A5
If aNumber < 0 Then
sheet.Cells(n, 2).Value = aNumber
n = n + 1
End If
Next x
End Sub


It will fail if any of the input is not an integer number, but data validation is another subject

dasnoob
05-28-2011, 05:15 AM
Thank you!