Solved

Excel VBA incrementing a variable by 1 without loop or re-iteration

Posted on 2014-03-24
5
2,398 Views
Last Modified: 2014-03-25
Is this even possible? Is there a way to write this code so that it automatically increments "i" by one every line without the "i = i +1" inserted before every line? I know it's of no real consequence, it's just that I love code that's as compact and elegant as possible. Originally the code had fewer lines with numbers from 3 to 19 instead of i. But in this instance there is the possibility that a new column will get inserted before column 3 at some time, which is why I wanted to go the "i" route.

Thanks,
John
   Set lkprng = Range([A1], [A1].End(xlToRight).End(xlDown))
   i = 3
   seat_count = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   fclasstot = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   cwclasstot = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   wtpclasstot = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   wtclasstot = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   fclassrng1 = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   fclassrng = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   fclassLetters = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   cwclassrng1 = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   cwclassrng2 = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   cwclassLetters = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   wtpclassrng1 = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   wtpclassrng2 = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   wtpclassLetters = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   wtclassrng1 = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   wtclassrng2 = Application.VLookup(configIndex, lkprng, i, False)
   i = i + 1
   wtclassLetters = Application.VLookup(configIndex, lkprng, i, False)

Open in new window

0
Comment
Question by:gabrielPennyback
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Juan Ocasio
Comment Utility
No real way, but you could do this:

  Set lkprng = Range([A1], [A1].End(xlToRight).End(xlDown))
   i = 3
   seat_count = Application.VLookup(configIndex, lkprng, i, False)

   fclasstot = Application.VLookup(configIndex, lkprng, i + 1, False)

   cwclasstot = Application.VLookup(configIndex, lkprng, i + 1, False)
...

Open in new window

0
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
Comment Utility
You can set a function to increment your i as it goes, e.g.

Sub test()

   Set lkprng = Range([A1], [A1].End(xlToRight).End(xlDown))
   i = 2
   seat_count = Application.VLookup(configIndex, lkprng, nexti(i), False)
   fclasstot = Application.VLookup(configIndex, lkprng, nexti(i), False)
   cwclasstot = Application.VLookup(configIndex, lkprng, nexti(i), False)
   wtpclasstot = Application.VLookup(configIndex, lkprng, nexti(i), False)
   wtclasstot = Application.VLookup(configIndex, lkprng, nexti(i), False)
   fclassrng1 = Application.VLookup(configIndex, lkprng, nexti(i), False)
   fclassrng = Application.VLookup(configIndex, lkprng, nexti(i), False)
   fclassLetters = Application.VLookup(configIndex, lkprng, nexti(i), False)
   cwclassrng1 = Application.VLookup(configIndex, lkprng, nexti(i), False)
   cwclassrng2 = Application.VLookup(configIndex, lkprng, nexti(i), False)
   cwclassLetters = Application.VLookup(configIndex, lkprng, nexti(i), False)
   wtpclassrng1 = Application.VLookup(configIndex, lkprng, nexti(i), False)
   wtpclassrng2 = Application.VLookup(configIndex, lkprng, nexti(i), False)
   wtpclassLetters = Application.VLookup(configIndex, lkprng, nexti(i), False)
   wtclassrng1 = Application.VLookup(configIndex, lkprng, nexti(i), False)
   wtclassrng2 = Application.VLookup(configIndex, lkprng, nexti(i), False)
   wtclassLetters = Application.VLookup(configIndex, lkprng, nexti(i), False)

End Sub

Function nexti(i As Long) As Long

nexti = i + 1

End Function

Open in new window


Thomas
0
 
LVL 14

Expert Comment

by:Juan Ocasio
Comment Utility
You could also create a function that will return an incremented value
0
 
LVL 39

Expert Comment

by:nutsch
Comment Utility
Edit to my code, with i set as a public variable

Public i As Long


Sub test()

   Set lkprng = Range([A1], [A1].End(xlToRight).End(xlDown))
   i = 2
   seat_count = Application.VLookup(configIndex, lkprng, nexti(), False)
   fclasstot = Application.VLookup(configIndex, lkprng, nexti(), False)
   cwclasstot = Application.VLookup(configIndex, lkprng, nexti(), False)
   wtpclasstot = Application.VLookup(configIndex, lkprng, nexti(), False)
   wtclasstot = Application.VLookup(configIndex, lkprng, nexti(), False)
   fclassrng1 = Application.VLookup(configIndex, lkprng, nexti(), False)
   fclassrng = Application.VLookup(configIndex, lkprng, nexti(), False)
   fclassLetters = Application.VLookup(configIndex, lkprng, nexti(), False)
   cwclassrng1 = Application.VLookup(configIndex, lkprng, nexti(), False)
   cwclassrng2 = Application.VLookup(configIndex, lkprng, nexti(), False)
   cwclassLetters = Application.VLookup(configIndex, lkprng, nexti(), False)
   wtpclassrng1 = Application.VLookup(configIndex, lkprng, nexti(), False)
   wtpclassrng2 = Application.VLookup(configIndex, lkprng, nexti(), False)
   wtpclassLetters = Application.VLookup(configIndex, lkprng, nexti(), False)
   wtclassrng1 = Application.VLookup(configIndex, lkprng, nexti(), False)
   wtclassrng2 = Application.VLookup(configIndex, lkprng, nexti(), False)
   wtclassLetters = Application.VLookup(configIndex, lkprng, nexti(), False)

End Sub

Function nexti() As Long

i= i + 1
nexti =i

End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
Comment Utility
Thanks, Thomas, Perfect! Less is More ....    

~ John
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now