Consulting

Results 1 to 3 of 3

Thread: Basic Vba help

  1. #1
    VBAX Newbie
    Joined
    May 2011
    Posts
    3
    Location

    Basic Vba help

    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?

  2. #2
    VBAX Regular Chabu's Avatar
    Joined
    Dec 2010
    Location
    Brussels
    Posts
    85
    Location
    This will get you going
    [VBA]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
    [/VBA]

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

  3. #3
    VBAX Newbie
    Joined
    May 2011
    Posts
    3
    Location
    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •