Results 1 to 5 of 5

Thread: [RESOLVED] Help needed with Variable Constant assigning

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Resolved [RESOLVED] Help needed with Variable Constant assigning

    Guys,

    I have an issue I hope you can offer assistance on.

    I am trying to define a variable to maintain a number between 01 and 52,
    but I want to retain the two digit format, ie... it needs to be 01, - 09.

    The purpose of this is to join with another variable to display a Year and Wk format.

    This will then be outputted into a cell, and a for next function used to populate subsequent cells with the same value + 1.

    Using a String constant I have been able to retain 01, 02 etc, but when my For Next function then populates the cells I lose the 2 digit format, and it revertes to 1, 2, 3 etc.
    I have tried both Byte and Long constants but to the same effect.

    Below is my code that I am using, any assistance would be most appreciated.

    Dim strWk As String
    Dim strYear As Long
    Dim strProduct As Long

    strWk = InputBox("Please enter the current Financial week ? (example: 01,21,52)")
    strYear = InputBox("Please enter the current Financial year ? (example: 1999, 2008, 2010)")
    strProduct = InputBox("Please enter the product number that you want to view ? (example: 2205, 83312, 5)")

    Range("c6").Value = (strWk)

    Range("d6").Value = (strYear)

    Range("c10").Value = (strProduct)

    Dim strFY As String


    strFY = strYear & strWk

    Range("c15").Value = (strFY)
    Range("c15").Select

    For x = 1 To 51
    ActiveCell.Offset(0, 1).Activate
    strWk = strWk + 1
    strFY = strYear & strWk
    ActiveCell.Value = (strFY)
    If strWk = 52 Then strYear = strYear + 1
    If strWk = 52 Then strWk = 0
    Next x

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Help needed with Variable Constant assigning

    to keep the format in the cell you need to set the format of the cell to text, before inserting the data, you can do this either manually or by code
    Range("a:a").numberformat = "@"
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Re: Help needed with Variable Constant assigning

    Quote Originally Posted by westconn1
    to keep the format in the cell you need to set the format of the cell to text, before inserting the data, you can do this either manually or by code
    Range("a:a").numberformat = "@"
    West Conn,

    It is not the format of the cells that is the issue, as you can see from my code, I change the variable strWk to + 1 each time I use the offset function in the For next loop, this is causng the issue, as each time the variable changes it loses the two digit integrity if the result is less than 10.

    In other words, the user can input 01, and the initial output will be 01, but when the variable changes + 1, it now changes to 2 and loses the two digit integrity.

    How can I get around this?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Help needed with Variable Constant assigning

    try
    strWk = format(strWk + 1,"00")
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Re: Help needed with Variable Constant assigning

    Quote Originally Posted by westconn1
    try
    strWk = format(strWk + 1,"00")

    West,

    Thanks that worked a treat.

    SJ

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