+ Reply to Thread
Results 1 to 11 of 11

Seeking assistance with ths code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-15-2012
    Location
    Jamaica
    MS-Off Ver
    Microsoft 365
    Posts
    244

    Seeking assistance with ths code

    I am new to vba and i am trying to write a code to validate the "A" column. if the cell character length = 16 then add the letter "C" to the cell.
    if the cell not <> 9 and it is <> 16 then delete the info entered in the cell. I would like the cell that was deleted to still be selected after the validation of the cell is done (this part is optional though). The primary thing is to validate the cell. I got both codes to work but not at the same time. if it captures one condition it misses out the other.

    Kindly assist thank you.


    below is a sample of the code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Worksheets("Sheet1").Range("a8:a") <> "" Then
    If Len(Target.Value) = 16 Then
    Target.Value = "C" & Target.Value
    MsgBox ("test")
    End If
    End If
    If Worksheets("Sheet1").Range("a8:a") <> "" Then
    If Len(Target.Value) <> 9 And Len(Target.Value) <> 16 Then
    MsgBox ("test again")
    Target.Value = Left(Target.Value, 0)
    End If
    End If

    End Sub
    Last edited by kmakjop; 10-04-2012 at 02:07 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Seeking assistance with ths code

    Might be that this is what you're after:

    Sub test()
    
    Dim Cell As Range
    
    For Each Cell In Worksheets("Sheet1").Range("A8","A:A")
    If Cell.Value <> "" Then
        If Len(Cell.Value) = 16 Then
            Cell.Value = "C" & Cell.Value
            MsgBox ("test")
        End If
    End If
    Next Cell
    
    For Each Cell In Worksheets("Sheet1").Range("A8", "A:A")
    If Cell.Value <> "" Then
            If Len(Cell.Value) <> 9 And Len(Cell.Value) <> 16 Then
            MsgBox ("test again")
            Cell.Value = Left(Cell.Value, 0) ' which is nothing btw
        End If
    End If
    Next Cell
    
    End Sub
    Please try to add some formatting to your code (i almost got blind ). Wrap your code in "[code]" & "[/ code](without the space)" next time.

  3. #3
    Forum Contributor
    Join Date
    09-15-2012
    Location
    Jamaica
    MS-Off Ver
    Microsoft 365
    Posts
    244

    Re: Seeking assistance with ths code

    Thank you, i like your code; however the code you gve me deletes the cell if the "C" is added to the cell. I only want the delete to execute only if the cell <> 9 but if it is = 16 just add "C" to the cell and keep content.

    Thanks again.

    Quote Originally Posted by Bishonen View Post
    Might be that this is what you're after:

    Sub test()
    
    Dim Cell As Range
    
    For Each Cell In Worksheets("Sheet1").Range("A8","A:A")
    If Cell.Value <> "" Then
        If Len(Cell.Value) = 16 Then
            Cell.Value = "C" & Cell.Value
            MsgBox ("test")
        End If
    End If
    Next Cell
    
    For Each Cell In Worksheets("Sheet1").Range("A8", "A:A")
    If Cell.Value <> "" Then
            If Len(Cell.Value) <> 9 And Len(Cell.Value) <> 16 Then
            MsgBox ("test again")
            Cell.Value = Left(Cell.Value, 0) ' which is nothing btw
        End If
    End If
    Next Cell
    
    End Sub
    Please try to add some formatting to your code (i almost got blind ). Wrap your code in "[code]" & "[/ code](without the space)" next time.

  4. #4
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Seeking assistance with ths code

    Quote Originally Posted by kmakjop View Post
    Thank you, i like your code; however the code you gve me deletes the cell if the "C" is added to the cell. I only want the delete to execute only if the cell <> 9 but if it is = 16 just add "C" to the cell and keep content.

    Thanks again.
    That's exactly what i tried to inform you about in my comment (which comes after an apostrophe).

    Sub test()
    
    Dim Cell As Range
    
    For Each Cell In Worksheets("Sheet1").Range("A8", "A" & Rows.Count)
    Select Case Len(Cell)
    
        Case Is = 16
        Cell.Value = "C" & Cell.Value
        
        Case Is <> 9
        Cell.Value = ""
        
        End Select
        
        Next Cell
        
    End Sub

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Seeking assistance with ths code

    Give this a try

    Dim Cell As Range
    
    For Each Cell In Worksheets("Sheet1").Range("A8", "A:A")
        With Cell
            If Trim(.Value) <> "" Then
                Select Case Len(.Value)
                    Case Is = 16
                        .Value = "C" & .Value
                        MsgBox ("test 16")
                    Case Is <> 9
                        .ClearContents
                        MsgBox ("test <> 9 but not 16")
                End Select
            End If
        End With
    Next Cell
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  6. #6
    Forum Contributor
    Join Date
    09-15-2012
    Location
    Jamaica
    MS-Off Ver
    Microsoft 365
    Posts
    244

    Re: Seeking assistance with ths code

    Ok i am starting to feel kinda stupid here. I tried Mike's code and it basically did th same thing (deleted the content even though there were 16 characters in the cell). I also tried Bishonen's code and it froze the application. What am i not doing or understanding here?

  7. #7
    Forum Contributor
    Join Date
    09-15-2012
    Location
    Jamaica
    MS-Off Ver
    Microsoft 365
    Posts
    244

    Re: Seeking assistance with ths code

    is there any other suggestions bishonen or mike? Are there anyone else who can suggest something that could assist with ths code. I think the suggestios i got from both Mike and bishonen will work if they are modified a bit. I am trying but its still not executing as i would like.

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Seeking assistance with ths code

    @ kmakjop,

    Before we can continue, two things...

    Please read the forum rules about proper thread titles and applying code tags when posting code. Thanks.
    HTH
    Regards, Jeff

  9. #9
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Seeking assistance with ths code

    The problem is that your doing this inside the Worksheets Change Event. Example when you type in say Cell A8 a 16 character it changes to 17 which fires the macro again, so then the 16 character value became C17 and gets set to nothing here Case Is <> 9. That make sense.

  10. #10
    Forum Contributor
    Join Date
    09-15-2012
    Location
    Jamaica
    MS-Off Ver
    Microsoft 365
    Posts
    244

    Re: Seeking assistance with ths code

    Thank you so much for your assistance, your code worked flawlessly. I also appreciate the explanation you gave as to why i couldnt get it to execute.

    I am in jamaica so let me give that thank you in a Jamaican vibe!

    Mike yu a di bhass (bhass meaning boss!), bless up!

    Big up to bishonen for the guidance as well!

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Seeking assistance with ths code

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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