Add last Column to Macro

capson

Board Regular
Joined
Jul 9, 2010
Messages
107
I am trying to change the Sub CleanAll() to Sub CleanAll2() but can not get it.

Thanks



Code:
Sub CleanAll()


    Dim myArray As Variant
    Dim lastRow As Long
 
    With Sheets("360")
        lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    End With


    myArray = Sheets("360").Range("B2:CJ" & lastRow)


    For x = LBound(myArray) To UBound(myArray)
        For Y = LBound(myArray, 2) To UBound(myArray, 2)
            myArray(x, Y) = NumberOnly(myArray(x, Y))
        Next Y
    Next x


    Sheets("360").Range("B2:CJ" & lastRow) = myArray
End Sub

Code:
Sub CleanAll3()


    Dim myArray As Variant
    Dim lastRow As Long
    Dim lastCol As Long
    Dim WS As Worksheet
    Dim rng As Range


 
  With Sheets("360")
            lastCol = .Cells(8, .Columns.Count).End(xlToLeft).Column
            lastRow = .Cells(.Rows.Count, lastCol).End(xlUp).Row
        End With


For Each rng In Sheets("360").Range(Cells(2, 8), Cells(lastRow, lastCol))


    myArray = Sheets("360").Range(Cells(2, 8), Cells(lastRow, lastCol))


    For x = LBound(myArray) To UBound(myArray)
        For Y = LBound(myArray, 2) To UBound(myArray, 2)
            myArray(x, Y) = NumberOnly(myArray(x, Y))
        Next Y
    Next x


    Sheets("360").Range(Cells(2, 8), Cells(lastRow, lastCol)) = myArray
    Next
End Sub



Code:
Function NumberOnly(ByVal strSource As String) As String
    Dim i As Integer
    Dim strResult As String


    For i = 1 To Len(strSource)
        Select Case Asc(Mid(strSource, i, 1))
            Case 32, 48 To 57, 65, 78:
                strResult = strResult & Mid(strSource, i, 1)
        End Select
    Next
    NumberOnly = strResult


End Function
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
        Sub CleanAll3()
            Dim myArray As Variant
            Dim lastRow As Long
            Dim lastCol As Long
            Dim WS As Worksheet
            Dim rng As Range
        
          With Sheets(1)
                    lastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
                    lastRow = .Cells(.Rows.Count, lastCol).End(xlUp).Row
                End With
        For Each rng In Sheets(1).Range(Cells(2, 2), Cells(lastRow, lastCol))
            myArray = Sheets(1).Range(Cells(2, 2), Cells(lastRow, lastCol))
            For x = LBound(myArray) To UBound(myArray)
                For Y = LBound(myArray, 2) To UBound(myArray, 2)
                    myArray(x, Y) = NumberOnly(myArray(x, Y))
                Next Y
            Next x
            Sheets(1).Range(Cells(2, 2), Cells(lastRow, lastCol)) = myArray
            Next
        End Sub
 
Upvote 0
Thanks for the reply.

When I run the macro with your code excel stops responding i.e the macro never finishes running like there is a infinite loop going on
 
Upvote 0

Forum statistics

Threads
1,215,606
Messages
6,125,805
Members
449,262
Latest member
hideto94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top