Results 1 to 5 of 5

Thread: Select column based on user input

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    7
    Rep Power
    0

    Select column based on user input

    Hi everyone.

    Is it possible to create an input box, and for me to put in a letter (for example "P") and then the macro selects the column (in this case Column P)?

    Thanks


    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=XQAIYCT4f8Q&lc=UgwTUdEgR4bdt6crKXF4AaABAg. 9xmkXGSciKJ9xonTti2sIx
    https://www.youtube.com/watch?v=XQAIYCT4f8Q&lc=UgwWw16qBFX39JCRRm54AaABAg. 9xnskBhPnmb9xoq3mGxu_b
    https://www.youtube.com/watch?v=XQAIYCT4f8Q&lc=UgzgWvzV-kvC4TJ8O414AaABAg.9xnFzCj8HRM9xon1p2ImxO
    https://www.youtube.com/watch?v=XQAIYCT4f8Q&lc=UgybZfNJd3l4FokX3cV4AaABAg. 9xm_ufqOILb9xooIlv5PLY
    https://www.youtube.com/watch?v=XQAIYCT4f8Q&lc=UgzgWvzV-kvC4TJ8O414AaABAg.9xnFzCj8HRM9y38bzbSqaG
    https://www.youtube.com/watch?v=XQAIYCT4f8Q&lc=UgyWm8nL7syjhiHtpBF4AaABAg. 9xmt8i0IsEr9y3FT9Y9FeM
    https://www.youtube.com/watch?v=jTmVtPHtiTg&lc=Ugy_RiNN_kAqUvZ8W994AaABAg. 9xhyRrsUUOM9xpn-GDkL3o
    https://www.youtube.com/watch?v=jTmVtPHtiTg&lc=Ugy_RiNN_kAqUvZ8W994AaABAg
    https://www.youtube.com/watch?v=QjEWAJ3d-jw&lc=UgxJLVpwY8fIla7G-pN4AaABAg.9BLeCWVhxdG9wgNsaS3Lp1
    https://www.youtube.com/watch?v=QjEWAJ3d-jw&lc=UgxJLVpwY8fIla7G-pN4AaABAg.9BLeCWVhxdG9wgR1EPUkhw
    https://www.youtube.com/watch?v=QjEWAJ3d-jw&lc=UgxJLVpwY8fIla7G-pN4AaABAg.9BLeCWVhxdG9wgNe_XC-jK
    https://www.youtube.com/watch?v=QjEWAJ3d-jw&lc=UgxJLVpwY8fIla7G-pN4AaABAg.9BLeCWVhxdG9wgNPOdiDuv
    https://www.youtube.com/watch?v=QjEWAJ3d-jw&lc=UgxJLVpwY8fIla7G-pN4AaABAg.9BLeCWVhxdG9wgN7AC7wAc
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 12-14-2023 at 02:55 AM.

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    Hello Wall31
    Welcome to ExcelFox
    I would use the standard VBA InputBox function for something very simple like you are asking for , rather than creating one,

    This is the basic coding to get you started. You would normally want to possibly consider some extra lines to check that valid input is given by the user
    ( You can give the column as a Letter, such as your P , or a number. In both cases just give the character with no quotes, )
    Alan

    Code:
    Sub SimpleInputBox()   '
    Rem 1 VBA input box function  https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/inputbox-function
    Dim Anser As String
     Let Anser = InputBox(Prompt:="Give Column number or column Letter", Title:="Select Column", Default:="A")
    Rem 2 select column
    ActiveSheet.Columns(Anser).Select
    End Sub
    Last edited by DocAElstein; 06-18-2020 at 11:42 AM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    7
    Rep Power
    0
    That is exactly what I am after! Thanks so much!

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    You're welcome.
    Just in passing interest I have a version here that demonstrates what I mentioned about extending the solution to check the data. The macro below makes some attempt at handling the case of the user giving bad data.
    I have not checked it thoroughly yet, and might edit / correct it later.
    I am just adding it for future reference, that’s all.

    If the simpler solution does what you want then it is probably best to use that. Best is often to keep it as simple as possible….

    Alan

    Code:
    Sub SimpleInputBox2()   '  https://excelfox.com/forum/showthread.php/2560-Select-column-based-on-user-input?p=13744&viewfull=1#post13744
    Rem 1 VBA input box function  https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/inputbox-function
    Dim Anser As String
     Let Anser = InputBox(Prompt:="Give Column number or column Letter", Title:="Select Column", Default:="A")
     Let Anser = Trim(Anser) ' take away any spaces either side of the input character or characters
    Rem 2 Some data checking
        If StrPtr(Anser) = 0 Then Exit Sub ' the user clicked cancel https://excelfox.com/forum/showthread.php/1828-How-To-React-To-The-Cancel-Button-in-a-VB-(not-Application)-InputBox?p=8474&viewfull=1#post8474
        If IsNumeric(Anser) Then ' this checks if it looks like a number
            If Anser > ActiveSheet.Columns.Count Then MsgBox Prompt:="there aint that many columns in ya worksheet!": Exit Sub
        Else   ' we did not input just a number
            If Len(Anser) > 3 And ActiveSheet.Columns.Count = 16384 Then MsgBox Prompt:="there aint that any columns with more than 3 characters in XL 2007 and higher": Exit Sub
            If Len(Anser) > 2 And ActiveSheet.Columns.Count = 256 Then MsgBox Prompt:="there aint that any columns with more than 2 characters in XL 2003 and lower": Exit Sub
            If Len(Anser) = 2 And (IsNumeric(Left(Anser, 1)) Or IsNumeric(Right(Anser, 1))) Then MsgBox Prompt:="you can't mix letters and numbers": Exit Sub
            If Len(Anser) = 3 And (IsNumeric(Left(Anser, 1)) Or IsNumeric(Right(Anser, 1)) Or IsNumeric(Mid(Anser, 2, 1))) Then MsgBox Prompt:="you can't mix letters and numbers": Exit Sub
            If ActiveSheet.Columns.Count = 256 And Len(Anser) = 2 And Not (UCase(Left(Anser, 1)) = "A" Or UCase(Left(Anser, 1)) = "B" Or UCase(Left(Anser, 1)) = "C" Or UCase(Left(Anser, 1)) = "D" Or UCase(Left(Anser, 1)) = "E" Or UCase(Left(Anser, 1)) = "F" Or UCase(Left(Anser, 1)) = "G" Or UCase(Left(Anser, 1)) = "H" Or UCase(Left(Anser, 1)) = "I") Then MsgBox Prompt:="First character must be  A B C D E F G H or I": Exit Sub
            If ActiveSheet.Columns.Count = 256 And Len(Anser) = 2 And (UCase(Right(Anser, 1)) = "W" Or UCase(Right(Anser, 1)) = "X" Or UCase(Right(Anser, 1)) = "Y" Or UCase(Right(Anser, 1)) = "Z") Then MsgBox Prompt:="you can't have second character above  ""V"" ": Exit Sub    '                     For up to XL 2003 we can go up to column letterws IV
            If ActiveSheet.Columns.Count = 16384 And Len(Anser) = 3 And (UCase(Left(Anser, 1)) = "Y" Or UCase(Left(Anser, 1)) = "Z") Then MsgBox Prompt:="First character must be not above X": Exit Sub                                                           '                                        For XL 2007 and higher we can go up to column XFD
            If ActiveSheet.Columns.Count = 16384 And Len(Anser) = 3 And Not (UCase(Mid(Anser, 2, 1)) = "A" Or UCase(Mid(Anser, 2, 1)) = "B" Or UCase(Mid(Anser, 2, 1)) = "C" Or UCase(Mid(Anser, 2, 1)) = "D" Or UCase(Mid(Anser, 2, 1)) = "E" Or UCase(Mid(Anser, 2, 1)) = "F") Then MsgBox Prompt:="second  character must be  A B C D E or F ": Exit Sub
        
        End If
        
    Rem 3 select column
     ActiveSheet.Columns(Anser).Select
    End Sub
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    'Public Function CL(ByVal lclm As Long) As String '         http://www.excelforum.com/development-testing-forum/1101544-thread-post-appendix-no-reply-needed-please-do-not-delete-thanks-4.html#post4213980
    '    Do: Let CL = Chr(65 + (((lclm - 1) Mod 26))) & CL: Let lclm = (lclm - (1)) \ 26: Loop While lclm > 0
    'End Function
    
    Last edited by DocAElstein; 06-19-2020 at 09:33 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  5. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    7
    Rep Power
    0
    Thank you so much for the effort you put into my query! I really appreciate! To be honest, I love your validation options because you never know how other people will actually use it!

    Please stay safe and well!

Similar Threads

  1. Replies: 2
    Last Post: 03-22-2014, 10:32 AM
  2. Select A Range Dynamically Based On Count Of Values
    By getlucky in forum Excel Help
    Replies: 1
    Last Post: 02-19-2014, 07:06 PM
  3. User Input requested Search box (floating of static)
    By william516 in forum Excel Help
    Replies: 0
    Last Post: 07-22-2013, 07:53 PM
  4. Insert Or Delete Columns Based On User Input
    By HDMI in forum Excel Help
    Replies: 4
    Last Post: 06-21-2013, 03:00 AM
  5. Format Cells Based on Given Input
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 10
    Last Post: 08-23-2011, 11:19 AM

Posting Permissions

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