Results 1 to 16 of 16

Thread: [RESOLVED] help with VBA in Excel

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Location
    Minneapolis, Mn
    Posts
    16

    Resolved [RESOLVED] help with VBA in Excel

    I am trying to do repetitive actions over a selected range of cells in an Excel spreadsheet. What I am trying to do is to check if the cell is numeric or text. If it is numeric to change it to text. Yes the cells contain numbers and yes we want the numbers to be represented as text for importing the spreadsheet through another process.

    I am NEW, very NEW to VB and VBA, so pardon the obviously naive request.

    thanks
    Mike
    Last edited by mikevba; May 30th, 2006 at 12:51 PM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: help with VBA in Excel

    Welcome to Forums, mike!

    In Excel go to VB Editor and do something like the following sample:
    VB Code:
    1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    2. Static prevTarget As Range
    3.  
    4.     If Not prevTarget Is Nothing Then
    5.         If Not IsNumeric(prevTarget.Text) Then
    6.             MsgBox "Cell(" & prevTarget.Row & "," & prevTarget.Column & "): Value is not numeric."
    7.         End If
    8.     End If
    9.    
    10.     Set prevTarget = Target
    11.  
    12. End Sub

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: help with VBA in Excel

    Excel VBA question moved to Office Development

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: help with VBA in Excel

    Why don't you just make the entire range Text? This would be quicker that looping through all the cells individually.
    VB Code:
    1. YourRange.NumberFormat = "@"
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Location
    Minneapolis, Mn
    Posts
    16

    Re: help with VBA in Excel

    hey thats a great idea!!!

    This is why I am here to steal good ideas...

    thanks

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: help with VBA in Excel

    BTW, welcome to the forums - your home for great ideas.

    Also, don't forget to mark this as resolved.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Location
    Minneapolis, Mn
    Posts
    16

    Re: [RESOLVED] help with VBA in Excel

    I used this, but it only adds leading zeros. It doesn't make it a text field, like I need.

    Range("Range1").NumberFormat = "000"

    Any ideas?

    Mike

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: help with VBA in Excel

    VB Code:
    1. NumberFormat = "@"
    is what you should use to mark a cell as TExt.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Location
    Minneapolis, Mn
    Posts
    16

    Unhappy Re: help with VBA in Excel

    Thanks for the help, but it is not quite there yet.....

    What I am trying to automate is a fuction that I used to ....

    =text(c1,"000") which would add in leading zeros...... and make it a text column....

    When I use Range("Range1").NumberFormat = "@" it makes it text but I lose the leading zeros...

    thanks for helping a newbie.

    Mike

  10. #10
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: help with VBA in Excel

    Something like this maybe?
    VB Code:
    1. Sub testformat(InputRange As Range)
    2. Dim sValAsString As String
    3.    
    4.     With InputRange
    5.         sValAsString = CStr(.Value)
    6.         .NumberFormat = "@"
    7.         .Value = Right("000" & sValAsString, 3)
    8.     End With
    9.    
    10. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Location
    Minneapolis, Mn
    Posts
    16

    Re: help with VBA in Excel

    I am struggling here.... the error I get says "with variable not set"

    Sub Change2Text()
    Dim Range1 As Range
    Dim sValAsString As String

    ActiveSheet.Names.Add Name:="Range1", RefersTo:="=" & Selection.Address()
    testformat (Range1)

    End Sub

    Sub testformat(InputRange As Range)
    Dim sValAsString As String

    With InputRange
    sValAsString = CStr(.Value)
    .NumberFormat = "@"
    .Value = Right("000" & sValAsString, 3)
    End With

    End Sub

  12. #12

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Location
    Minneapolis, Mn
    Posts
    16

    Angry Re: help with VBA in Excel

    My latest.... but I get subscript out of range..... I had to use Value2 instead of Value and apparently it is an array

    Sub setRange()
    Dim Range1 As Range
    Dim sValAsString As String

    ActiveSheet.Names.Add Name:="Range1", RefersTo:="=" & Selection.Address()
    Set Rng = ActiveSheet.Range("Range1")

    testformat (Rng)

    End Sub

    Sub testformat(ByVal InputRange As Range)

    Dim sValAsString As String
    hi = UBound(InputRange.Value2)
    lo = LBound(InputRange.Value2)
    For x = lo To hi 'going thru each cell in the range
    With InputRange
    sValAsString = CStr(.Value2(x)) 'error happens here
    .NumberFormat = "@"
    .Value = Right("000" & sValAsString, 3)
    End With
    Next x

    End Sub


  13. #13
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: help with VBA in Excel

    Mike
    Lets take a step back. I want to make sure we have the requirements correct.
    Is the following list correct and complete?
    Req 1/ All numeric fields within the selected range should be formated to a fixed 3 character width. (what happens to numbers greater than 999, what happens to decimals?)
    Req 2/ All numeric fields should then be formatted as text.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  14. #14

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Location
    Minneapolis, Mn
    Posts
    16

    Re: help with VBA in Excel

    that is correct, the range will only have integers less than 999

    And I want them to be formated as text with leading zeros....

    I think it is basically working except for the statement
    sValAsString = CStr(.Value) ' problem statement
    which gives me a type mismatch error

    I tried using.... CStr(.Value(x)) too and that gave me a different error

    thanks for the help
    Mike

  15. #15
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: help with VBA in Excel

    Mike
    Here is a revised version of the Formatting procedure and a demo procedure to show hot to call it. Can you change the reference for the rngOutput variable such that it points to your data range and test the demo proc to see if this is what you need.
    VB Code:
    1. Sub MikeFormat(ByRef DataRange As Range)
    2. Dim rngCell As Range
    3. Dim sValAsString As String
    4.    
    5.     For Each rngCell In DataRange
    6.         With rngCell
    7.             If IsNumeric(.Value) Then
    8.                 sValAsString = CStr(.Value)
    9.                 .NumberFormat = "@"
    10.                 .Value = Right("000" & sValAsString, 3)
    11.             End If
    12.         End With
    13.    Next rngCell
    14. End Sub
    15.  
    16. Sub demo()
    17. Dim rngOutput As Range
    18.    
    19.     'You will need to change this line to
    20.     'refer to the range you wish to format
    21.     Set rngOutput = ThisWorkbook.Worksheets(1).Range("A1:H50")
    22.    
    23.     MikeFormat rngOutput
    24. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  16. #16

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Location
    Minneapolis, Mn
    Posts
    16

    Resolved Re: help with VBA in Excel


    Thanks that works now... it looks like all you did is test for Numeric......

    I appreciate the help...... I REALLY do!!!

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