Results 1 to 8 of 8

Thread: Data cleaning temperature data where a value might be negative

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Site Admin VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,496
    Location
    Hmmmm.... scratch the above. In my haste of crossing eyes at the spreadsheet, I failed to notice that the minimums are occasionally single digit with one decimal place values, so the above code is useless if the value ranges between 9.9 and -9.9 in value. So would this be applicable?

    Option Explicit
    
    
    Sub AddParenthesesWithSpace()
        Dim rng As Range
        Dim cell As Range
        Dim strVal As String
        ' Set the range to B3:N27
        Set rng = ThisWorkbook.Sheets("Sheet3").Range("B3:N27")
        ' Loop through each cell in the range
        For Each cell In rng
            ' Get the string value from the cell
            strVal = cell.Value
            ' Check if the string has at least 4 characters and Smaller than -10.0
            If Len(strVal) >= 4 And Left(strVal) = "<=-10.0" Then
                'Insert space and open parenthesis after the 5th Character
                strVal = Left(strVal, 5) & " (" & Mid(strVal, 6)
                ' Add closing parenthesis at the end
                strVal = strVal & ")"
                ' Update the cell value
                cell.Value = strVal
            Elseif
    
                ' Insert space and open parenthesis after the 4th character
                strVal = Left(strVal, 4) & " (" & Mid(strVal, 5)
                ' Add closing parenthesis at the end
                strVal = strVal & ")"
                ' Update the cell value
                cell.Value = strVal
            End If
        Next cell
        ' Clean up
        Set rng = Nothing
    End Sub
    Last edited by Aussiebear; 03-17-2025 at 04:43 AM.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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