+ Reply to Thread
Results 1 to 2 of 2

Manipulating Numbers

  1. #1
    Jonas
    Guest

    Manipulating Numbers

    I am just learning how to program in Excel. I am having a bit of
    difficulty doing calculations on variables. Below is some code I have
    created. The first procedure works fine but I get an error when I run
    the second procedure. The counter might actually be less than or equal
    to 30. When I finish the code, I am going to use an input box to get
    the maxinum number for the counter. Any suggestions you could give
    would be much appreciated. I'm think that the code probably has a very
    fundamental flaw but I can't figure it out.

    Sub test()
    Public testme(1 To 30) As Integer
    For counter = 1 To 30
    Range("B1").Select
    testme(counter) = Selection.Offset(counter, 0)
    Next counter
    End Sub

    Sub test2()
    For counter2 = 1 To 30
    If testme(counter2) = 1 Then testme(counter2) = testme(counter2) - 1
    MsgBox counter2
    Next counter2
    End Sub


  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Jonas

    Public declarations do not go inside macro's

    It is also a good idea to declare all variables that you wish to use

    I suggest having a look at http://www.cpearson.com/ especially http://www.cpearson.com/excel/variables.htm

    In regards to your code try this

    Sub test()
    Dim counter As Integer
    For counter = 1 To 30
    Range("B1").Select
    testme(counter) = Selection.Offset(counter, 0)
    Next counter
    End Sub

    Sub test2()
    Dim counter2 As Integer
    For counter2 = 1 To 30
    If testme(counter2) = 1 Then testme(counter2) = testme(counter2) - 1
    MsgBox counter2
    Next counter2
    End Sub

    You can use the same variables names within different macros
    as an example

    Sub test()
    Dim counter As Integer
    For counter = 1 To 30
    Range("B1").Select
    testme(counter) = Selection.Offset(counter, 0)
    Next counter
    End Sub

    Sub test2()
    Dim counter As Integer
    For counter = 1 To 30
    If testme(counter) = 1 Then testme(counter) = testme(counter) - 1
    MsgBox counter
    Next counter
    End Sub

    If you need to declare a public variable then it would be done as

    Public testme(1 To 30) As Integer

    Sub test()
    Dim counter As Integer
    For counter = 1 To 30
    Range("B1").Select
    testme(counter) = Selection.Offset(counter, 0)
    Next counter
    End Sub

    Sub test2()
    Dim counter As Integer
    For counter = 1 To 30
    If testme(counter) = 1 Then testme(counter) = testme(counter) - 1
    MsgBox counter
    Next counter
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1