Consulting

Results 1 to 3 of 3

Thread: Adding odd numbers in VBA

  1. #1

    Question Adding odd numbers in VBA

    Hi friends I am working on a code that adding odd number from 1 to the number user determines. I did the adding number but I don't know how I should do it for odd number. I hope anybody can help me.
    In attachment you can find what I've done.
    Attached Files Attached Files
    Last edited by loloobaloo; 06-23-2022 at 01:13 AM.

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,196
    Location
    Maybe just add a step to your loop:
    Sub OddNumber()    Dim num As Integer
        Dim Sum As Long
        Dim i As Long
        
        num = InputBox("Enter your number")
        Range("A1").Value = num
    
    
        For i = 1 To num Step 2
            Sum = Sum + i
        Next
    
    
        Range("A2").Value = Sum
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    Thanks it works.

Posting Permissions

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