Results 1 to 9 of 9

Thread: Excel VBA Question

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    43

    Excel VBA Question

    how can I create a excel vba to display the following numbers to 170

    3
    7
    13
    17
    23
    27
    33
    37
    43
    47
    ...
    130
    170

    I tried to do it with

    Code:
    for x = 3, x <= 170 Step 3
    {
       x= x + 1
    }
    Next x
    But this doesn't work. how do I fix this? this was an interview question.

  2. #2
    New Member
    Join Date
    Oct 2009
    Location
    Ireland
    Posts
    4

    Re: Excel VBA Question

    try


    x=3
    y=7

    ...


    x=x+10
    y=y+10

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA Question

    Moved To Office Development

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA Question

    Two things...

    I think that the series that you want to generate is

    7
    13
    17
    23
    27
    33
    37
    43
    47
    53
    57
    63
    67
    73
    77
    83
    87
    93
    97
    103
    107
    113
    117
    123
    127
    133
    137
    143
    147
    153
    157
    163
    167
    173
    If yes then try this...

    vb Code:
    1. Sub GenerateSeries()
    2.     Dim i As Long, j As Long, boolCheck As Boolean, boolExit As Boolean
    3.    
    4.     i = 3
    5.     j = 1
    6.    
    7.     '~~> Store it in Excel Cell A1
    8.     Range("A" & j).Value = i
    9.    
    10.     '~~> Generate Series
    11.     Do While boolExit = False
    12.         If boolCheck = False Then
    13.             i = i + 4
    14.             boolCheck = True
    15.         ElseIf boolCheck = True Then
    16.             i = i + 6
    17.             boolCheck = False
    18.         End If
    19.        
    20.         Range("A" & j).Value = i
    21.         j = j + 1
    22.        
    23.         '~~> Exit loop when end of number reached
    24.         If i > 170 Then boolExit = True
    25.     Loop
    26. End Sub
    Last edited by Siddharth Rout; Oct 1st, 2009 at 10:29 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    Addicted Member
    Join Date
    Jan 2009
    Posts
    183

    Re: Excel VBA Question

    I don't understand your pattern, because
    130
    170
    doesn't logically fit into
    7
    13
    17
    23
    27
    33
    37
    43
    47
    ...
    Starting from 3, you are getting every number which ends in either 3 or 7. 130 & 170 doesn't fit that. Unless I am misunderstanding how you arrived at your pattern.

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA Question

    Mark I guess that was a typo... guess

    I have put the actual series in my last post...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7
    Addicted Member
    Join Date
    Jan 2009
    Posts
    183

    Re: Excel VBA Question

    Quote Originally Posted by koolsid View Post
    Mark I guess that was a typo... guess

    I have put the actual series in my last post...
    Yeah, I'd guess it was too, but you never know.

    Another solution for jazlady:

    Dim x As Integer
    Dim y As Integer

    For x = 3 To 173 Step 10
    y = x + 4
    If (x > 173) Then Exit For
    Debug.Print x
    If (y > 173) Then Exit For
    Debug.Print y
    Next x

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA Question

    Nice one Mark
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA Question

    Code:
    For i = 0 To 16
        Cells(2 * i + 1, 1) = i * 10 + 3
        Cells(2 * i + 2, 1) = i * 10 + 7
    Next
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

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



Click Here to Expand Forum to Full Width