Consulting

Results 1 to 9 of 9

Thread: Avoid selection and select in format number

  1. #1
    VBAX Regular
    Joined
    Oct 2017
    Posts
    20
    Location

    Avoid selection and select in format number

    Hello.
    I have a code to convert number stored as text to numbers.

    I've read on the web much about to avoid using:
    .Select
    .Selection
    ActiveSheet, etc.

    However, I am unsure of how to avoid using those in my code:

    Sub textTonumber()
    Dim ws As Worksheet
    
    
    Set ws = Worksheets("qry_creategantt")
            
        With ws.Range("I2", Range("I2").End(xlDown)).Select
         
            Selection.TextToColumns Destination:=Range("I2"), DataType:=xlDelimited, _
            TextQualifier:=xlTextQualifierDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=False, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
            ws.Range("A1").Select
    
    
        End With
    
    
    End Sub
    Your ideas are welcome
    Cheer

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    This is all you need:

    Sub M_snb()
       sheets("qry_creategantt").colums(9).TextToColumns ,,,, -1,0, 0, 0 
    End Sub

  3. #3
    VBAX Regular
    Joined
    Oct 2017
    Posts
    20
    Location
    snb. Thanks for your reply.

    I tested your code as follow:

    Sub M_snb()   sheets("qry_creategantt").colums(9).TextToColumns , , , , -1, 0, 0, 0
    End Sub
    Sub M_snb()   Sheets("qry_creategantt").colums(9).TextToColumns , , , , -1, 0, 0, 0
    End Sub
    When I type sheets vba change to Sheets

    In both lines, VBA displays
    Excel VBA, error 438 "object doesn't support this property or method.

    Why in this code IntelliSense is not working

    Moreover, this is my first time I see ", , , ,-1,0,0,0"

    I really appreciate advice on how to fix this problem.

    Cheers

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    try this:
    Sub M_snb()    Dim ws As Worksheet
        Set ws = Worksheets("qry_creategantt")
    
    
        ws.Columns(9).TextToColumns , , , , -1, 0, 0, 0
    End Sub

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    To amend your original:

    Sub textTonumber()Dim ws As Worksheet
    Set ws = Worksheets("qry_creategantt")
            
        ws.Range("I2", ws.Range("I2").End(xlDown)).TextToColumns Destination:=ws.Range("I2"), DataType:=xlDelimited, _
            TextQualifier:=xlTextQualifierDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=False, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
    
    
    End Sub
    or skip the variable:

    Sub textTonumber()
        With Worksheets("qry_creategantt")
            .Range("I2", .Range("I2").End(xlDown)).TextToColumns Destination:=.Range("I2"), DataType:=xlDelimited, _
                TextQualifier:=xlTextQualifierDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
                Semicolon:=False, Comma:=False, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
        End With
    End Sub
    Be as you wish to seem

  7. #7
    VBAX Regular
    Joined
    Oct 2017
    Posts
    20
    Location
    JKwan.
    With only two lines, your code works as I need.
    Thank you

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Sub M_snb()
       sheets("qry_creategantt").columns(9).TextToColumns ,,,, -1,0, 0, 0 
    End Sub

  9. #9
    VBAX Regular
    Joined
    Oct 2017
    Posts
    20
    Location
    snb.
    I apologize, I did not read your code with due care.
    Editing column to columns the code works fine.

    Thank you

Tags for this Thread

Posting Permissions

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