Share via

Macro Help

Anonymous
2014-03-10T21:51:30+00:00

I have data that needs to be converted to Number - when I extract the data is always formatted to Text. My data is in column A, B, and C that needs to be converted. Ultimately, I would like to add the code to convert from Text to Number in column A, B, and C on Sheet1 to this basic macro below? I want to prevent having to convert to Number each time I extract data (which is a lot). Please help?

    Range("D4").Select

    Sheets("Sheet1").Select

    Columns("A:A").Select

    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

    Columns("E:E").Select

    Selection.Cut Destination:=Columns("A:A")

    Columns("E:E").Select

    Selection.Delete Shift:=xlToLeft

    Range("A2:C356").Select

    ActiveWindow.ScrollRow = 314

    ActiveWindow.ScrollRow = 311

    ActiveWindow.ScrollRow = 306

    ActiveWindow.ScrollRow = 299

    ActiveWindow.ScrollRow = 292

    ActiveWindow.ScrollRow = 284

    ActiveWindow.ScrollRow = 277

    ActiveWindow.ScrollRow = 271

    ActiveWindow.ScrollRow = 266

    ActiveWindow.ScrollRow = 263

    ActiveWindow.ScrollRow = 260

    ActiveWindow.ScrollRow = 259

    ActiveWindow.ScrollRow = 258

    ActiveWindow.ScrollRow = 257

    ActiveWindow.ScrollRow = 255

    ActiveWindow.ScrollRow = 251

    ActiveWindow.ScrollRow = 244

    ActiveWindow.ScrollRow = 233

    ActiveWindow.ScrollRow = 219

    ActiveWindow.ScrollRow = 204

    ActiveWindow.ScrollRow = 187

    ActiveWindow.ScrollRow = 168

    ActiveWindow.ScrollRow = 151

    ActiveWindow.ScrollRow = 138

    ActiveWindow.ScrollRow = 129

    ActiveWindow.ScrollRow = 124

    ActiveWindow.ScrollRow = 120

    ActiveWindow.ScrollRow = 117

    ActiveWindow.ScrollRow = 115

    ActiveWindow.ScrollRow = 114

    ActiveWindow.ScrollRow = 113

    ActiveWindow.ScrollRow = 112

    ActiveWindow.ScrollRow = 111

    ActiveWindow.ScrollRow = 110

    ActiveWindow.ScrollRow = 108

    ActiveWindow.ScrollRow = 105

    ActiveWindow.ScrollRow = 100

    ActiveWindow.ScrollRow = 96

    ActiveWindow.ScrollRow = 93

    ActiveWindow.ScrollRow = 90

    ActiveWindow.ScrollRow = 89

    ActiveWindow.ScrollRow = 88

    ActiveWindow.ScrollRow = 87

    ActiveWindow.ScrollRow = 86

    ActiveWindow.ScrollRow = 84

    ActiveWindow.ScrollRow = 81

    ActiveWindow.ScrollRow = 76

    ActiveWindow.ScrollRow = 71

    ActiveWindow.ScrollRow = 65

    ActiveWindow.ScrollRow = 58

    ActiveWindow.ScrollRow = 51

    ActiveWindow.ScrollRow = 44

    ActiveWindow.ScrollRow = 37

    ActiveWindow.ScrollRow = 30

    ActiveWindow.ScrollRow = 25

    ActiveWindow.ScrollRow = 20

    ActiveWindow.ScrollRow = 15

    ActiveWindow.ScrollRow = 12

    ActiveWindow.ScrollRow = 10

    ActiveWindow.ScrollRow = 8

    ActiveWindow.ScrollRow = 7

    ActiveWindow.ScrollRow = 6

    ActiveWindow.ScrollRow = 5

    ActiveWindow.ScrollRow = 4

    ActiveWindow.ScrollRow = 3

    ActiveWindow.ScrollRow = 2

    ActiveWindow.ScrollRow = 1

    Range("A1").Select

    Sheets("DATA").Select

    Range("D4").Select

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

OssieMac 48,001 Reputation points Volunteer Moderator
2014-03-11T06:38:22+00:00

Hello nekiaholland,

Firstly you can delete all of the ScrollRow lines that gets inserted when recording code.  I am not clear on exactly where you need the code to convert the data from text to numeric because at the end of your code you select worksheet Data. Therefore I have commented out your code so I could simply test the conversion code and will leave it to you to decide if it goes before selecting worksheet data.

Just so you understand what is occurring, the code firstly sets the Numeric format of columns A, B and C to General. (could use numeric but General works). This does not convert the text to numeric at this stage but prepares the columns to accept numeric values.

Then we find a blank cell on the worksheet and enter 1 (one) in it. (It is the first blank cell below the data in column A.

Then copy the cell with the 1 in it and then PasteSpecial -> Multiply over the data in columns A, B and C.  Performing a mathematical operation on the data converts it to numeric. However, multiplying by 1 does not actually change the numeric value.

The User Defined Function (UDF) LastRowOrCol is code that very reliably finds the last row on the worksheet that contains data. Other methods can be used but if some columns are longer than others then it is possible that the last row is not found.

Sub ConvertToNumeric()

    Dim lngLastRow As Long

    'Sheets("Sheet1").Select

    'Range("D4").Select

    'Columns("A:A").Select

    'Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

    'Columns("E:E").Select

    'Selection.Cut Destination:=Columns("A:A")

    'Columns("E:E").Select

    'Selection.Delete Shift:=xlToLeft

    'Range("A2:C356").Select

    'Range("A1").Select

    'Sheets("DATA").Select

    'Range("D4").Select

    'Format the required columns to General (or numeric)

    Columns("A:C").NumberFormat = "General"

    'Find an empty cell below the data

    lngLastRow = LastRowOrCol(ActiveSheet, True)

    'Insert 1 in the empty cell

    Range("A" & lngLastRow + 1) = 1

    'Copy the cell with 1 in it

    Range("A" & lngLastRow + 1).Copy

    'PasteSpecial with Multiply to the range of text numbers

    Range("A2:C" & lngLastRow).PasteSpecial _

        Paste:=xlPasteAll, _

        Operation:=xlMultiply, _

        SkipBlanks:=False, _

        Transpose:=False

    'Cleanup. clear the cell with 1

    Range("A" & lngLastRow + 1).ClearContents

 End Sub

Function LastRowOrCol(ws As Worksheet, bolRowCol As Boolean, Optional rng As Range) As Long

    'Finds the last used row or column in a worksheet

    'First parameter is Worksheet

    'Second parameter is True for Last Row or False for last Column

    Dim lngRowCol As Long

    Dim rngToFind As Range

    If rng Is Nothing Then

        Set rng = ws.Cells

    End If

    If bolRowCol Then

        lngRowCol = xlByRows

    Else

        lngRowCol = xlByColumns

    End If

    With ws

        Set rngToFind = rng.Find(What:="*", _

                LookIn:=xlFormulas, _

                LookAt:=xlPart, _

                SearchOrder:=lngRowCol, _

                SearchDirection:=xlPrevious, _

                MatchCase:=False)

    End With

    If Not rngToFind Is Nothing Then

        If bolRowCol Then

            LastRowOrCol = rngToFind.Row

        Else

            LastRowOrCol = rngToFind.Column

        End If

    End If

End Function

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. OssieMac 48,001 Reputation points Volunteer Moderator
    2014-03-11T22:02:19+00:00

    Thanks for simplifying the code for me. However, I got an error on the IngLastRow = LastRowOrCol(ActiveSheet, True) line - the error was Sub or Function not Defined. I tried to fix it but it did not work.

    My apologies for that Nikki. Somehow I did not copy all of the code into the post. I have now edited the post and included the remaining code.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-03-11T15:58:20+00:00

    OssieMac seems to have forgotten to post the code for that Function!

    You could use this code from Ron de Bruin (just copy and paste below the Sub you have) and change that offending line of code to become:

    lngLastRow = RDB_Last(1, ActiveSheet.Cells)

    Ron's code (that I use quite often)

    Function RDB_Last(choice As Long, rng As Range)

    'Ron de Bruin, 5 May 2008

    'source page: http://www.rondebruin.nl/last.htm

    ' 1 = last row

    ' 2 = last column

    ' 3 = last cell

      Dim lrw As Long

      Dim lcol As Long

      Select Case choice

        Case 1:

          On Error Resume Next

          RDB_Last = rng.Find(What:="*", _

           After:=rng.Cells(1), _

           Lookat:=xlPart, _

           LookIn:=xlFormulas, _

           SearchOrder:=xlByRows, _

           SearchDirection:=xlPrevious, _

           MatchCase:=False).Row

          On Error GoTo 0

        Case 2:

          On Error Resume Next

          RDB_Last = rng.Find(What:="*", _

           After:=rng.Cells(1), _

           Lookat:=xlPart, _

           LookIn:=xlFormulas, _

           SearchOrder:=xlByColumns, _

           SearchDirection:=xlPrevious, _

           MatchCase:=False).Column

          On Error GoTo 0

        Case 3:

          On Error Resume Next

          lrw = rng.Find(What:="*", _

           After:=rng.Cells(1), _

           Lookat:=xlPart, _

           LookIn:=xlFormulas, _

           SearchOrder:=xlByRows, _

           SearchDirection:=xlPrevious, _

           MatchCase:=False).Row

          On Error GoTo 0

          On Error Resume Next

          lcol = rng.Find(What:="*", _

           After:=rng.Cells(1), _

           Lookat:=xlPart, _

           LookIn:=xlFormulas, _

           SearchOrder:=xlByColumns, _

           SearchDirection:=xlPrevious, _

           MatchCase:=False).Column

          On Error GoTo 0

          On Error Resume Next

          RDB_Last = rng.Parent.Cells(lrw, lcol).Address(False, False)

          If Err.Number > 0 Then

            RDB_Last = rng.Cells(1).Address(False, False)

            Err.Clear

          End If

          On Error GoTo 0

      End Select

    End Function

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-03-11T15:52:13+00:00

    Hi OssieMac

    Thanks for simplifying the code for me. However, I got an error on the IngLastRow = LastRowOrCol(ActiveSheet, True) line - the error was Sub or Function not Defined. I tried to fix it but it did not work.

    Thanks

    Nikki

    Was this answer helpful?

    0 comments No comments