Changing range and address

topi1

Active Member
Joined
Aug 6, 2014
Messages
252
Office Version
  1. 2010
Hi, I have the following working vba.

I tried to chnage the vba to clean columns A and B by making the following change. But it did not work. How should I have done it?
Set rngData = .Range("A1", .Cells(Rows.Count, "A").End(xlUp)) to...
Set rngData = .Range("A1", .Cells(Rows.Count, "B").End(xlUp))

Also, I tried to move to columns M and N to the sheet Start_Clean and change the code as follows. But again, it did not work. Suggestions?
Set rngLookup = .Range("M1", .Cells(Rows.Count, "N").End(xlUp)) to...
Set rngLookup = .Range("Start_Clean!M1", .Cells(Rows.Count, "Start_Clean!N").End(xlUp))

Thank you very much.

VBA Code:
Sub Clean_ColumnA()

    Dim wsData As Worksheet
    Dim rngData As Range, rngLookup As Range
    Dim arrData As Variant, arrLookup As Variant
    Dim i As Long
    
    Set wsData = Worksheets("Start2")
    With wsData
        Set rngData = .Range("A1", .Cells(Rows.Count, "A").End(xlUp))
        Set rngLookup = .Range("M1", .Cells(Rows.Count, "N").End(xlUp))
    End With
    arrData = rngData.Value
    arrLookup = rngLookup.Value
arrData = Application.Substitute(arrData, ".", ". ")
arrData = Application.Substitute(arrData, "!", "! ")
arrData = Application.Substitute(arrData, "?", "? ")
    arrData = Application.Substitute(arrData, "  ", " ")
    arrData = Application.Substitute(arrData, " .", ".")
    arrData = Application.Substitute(arrData, "..", ".")
    arrData = Application.Substitute(arrData, "[", "")
    arrData = Application.Substitute(arrData, "]", "")
    arrData = Application.Clean(arrData)
    
    For i = 1 To UBound(arrLookup, 1)
        arrData = Application.Substitute(arrData, arrLookup(i, 1), arrLookup(i, 2))
    Next i
    rngData.Value = arrData
    
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Like This ?

VBA Code:
Sub Clean_ColumnA()

    Dim wsData As Worksheet
    Dim rngData As Range, rngLookup As Range
    Dim arrData As Variant, arrLookup As Variant
    Dim i As Long
    
    Set wsData1 = Worksheets("Start2")
    Set wsdata2 = Worksheets("Start_Clean")
    
    With wsData1
        Set rngData = .Range("A1:B1", .Cells(Rows.Count, "A").End(xlUp))
    End With
    
    With wsdata2
        Set rngLookup = .Range("M1", .Cells(Rows.Count, "N").End(xlUp))
    End With


    arrData = rngData.Value
    arrLookup = rngLookup.Value
arrData = Application.Substitute(arrData, ".", ". ")
arrData = Application.Substitute(arrData, "!", "! ")
arrData = Application.Substitute(arrData, "?", "? ")
    arrData = Application.Substitute(arrData, "  ", " ")
    arrData = Application.Substitute(arrData, " .", ".")
    arrData = Application.Substitute(arrData, "..", ".")
    arrData = Application.Substitute(arrData, "[", "")
    arrData = Application.Substitute(arrData, "]", "")
    arrData = Application.Clean(arrData)
    
    For i = 1 To UBound(arrLookup, 1)
        arrData = Application.Substitute(arrData, arrLookup(i, 1), arrLookup(i, 2))
    Next i
    rngData.Value = arrData
    
End Sub
 
Upvote 0
Solution
Like This ?

VBA Code:
Sub Clean_ColumnA()

    Dim wsData As Worksheet
    Dim rngData As Range, rngLookup As Range
    Dim arrData As Variant, arrLookup As Variant
    Dim i As Long
   
    Set wsData1 = Worksheets("Start2")
    Set wsdata2 = Worksheets("Start_Clean")
   
    With wsData1
        Set rngData = .Range("A1:B1", .Cells(Rows.Count, "A").End(xlUp))
    End With
   
    With wsdata2
        Set rngLookup = .Range("M1", .Cells(Rows.Count, "N").End(xlUp))
    End With


    arrData = rngData.Value
    arrLookup = rngLookup.Value
arrData = Application.Substitute(arrData, ".", ". ")
arrData = Application.Substitute(arrData, "!", "! ")
arrData = Application.Substitute(arrData, "?", "? ")
    arrData = Application.Substitute(arrData, "  ", " ")
    arrData = Application.Substitute(arrData, " .", ".")
    arrData = Application.Substitute(arrData, "..", ".")
    arrData = Application.Substitute(arrData, "[", "")
    arrData = Application.Substitute(arrData, "]", "")
    arrData = Application.Clean(arrData)
   
    For i = 1 To UBound(arrLookup, 1)
        arrData = Application.Substitute(arrData, arrLookup(i, 1), arrLookup(i, 2))
    Next i
    rngData.Value = arrData
   
End Sub
Perfect. Worked great! Thank you much.
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,985
Members
452,540
Latest member
haasro02

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