A1VBCode Forums

all combination of varibale number of numbers


http://www.a1vbcode.com/vbforums/Topic32134.aspx

By sheila - 3/6/2014

Hi all

my code give me all combination of any number of numbers except all 4 digit combination of 6 numbers for exmaple:



1-3-5-6 this is my code in blow



Sub test2()



Dim bar, a1, a3, a4, mot1, rowcount1 As Integer Dim s1 As String



Range("d1:d100").ClearContents



mot1 = 0



rowcount1 = 6



For bar = 1 To rowcount1 - 1

For a1 = 1 To rowcount1 - bar

For a3 = 1 To a1

mot1 = mot1 + 1 Cells(mot1, 4) = Cells(a3, 3)



For a4 = 1 To bar



s1 = ((a1 + a4)) Cells(mot1, 4) = Cells(mot1, 4) & "-" & Cells(s1, 3)



Next a4

Next a3

Next a1

Next bar



End Sub

please help to edit my code :-(
By bawldiggle - 10/2/2015

@ sheila

I know there have been a lot of lookers (to date) and maybe too late for Sheila ... but this post is for late comers



You have declared possible cell addresses a Variants

ie, a1, a3, a4, s1 ... as Variants are interpreted by VBA as cell objects

- VBA assumes strings that look like a cell address are in fact Objects (of absolute reference) not variables



Best practice is to always to head up modules with "Option Explicit" so that you are forced to declare Variables by a specific data type.

- and never create variable names that can be misinterpreted by VBA as objects

eg, Sheet1, A1, B53... might be misinterpreted by VBA as literal objects on the active worksheet



If variables are not explicitly declared as specific data types you are setting yourself up to trip up.



Variable names are better constructed so that others can follow your intentions and for your own clarity in 2 or 3 years time.

examples:

Dim strName As String

Dim objOneCell as Object

Dim objShtRead As Sheet

Dim objShtWrite As Sheet

Dim intNumber As Integer

Dim a As Integer

Dim cc as Long



Smile