Hello!
I am a novice when it comes to VBA so please excuse any amateur mistakes...
I have the below code to remove special characters from a sheet called "NewUpload" based on another sheet, "Header_List". "Header_List" contains all the headers in "NewUpload" with additional information against each one. For example column C in "Header_List" is called "Remove Characters?" which if set to "Yes" will remove certain characters from the matching column in "NewUpload"; as specified in the function "RemoveSpecialChars" (which I found online).
However when this code is run it replaces the special characters as expected but it is also replacing the value in column A of "Header_List" to the first replaced value in the "NewUpload" sheet. I password protected the sheet to see where it crashed and it crashed at:
I spent many hours trying to work out why it is doing this (that is why I am activating cells above - this wasn't originally there) and I just can't see it!
Thank you in advance!
I am a novice when it comes to VBA so please excuse any amateur mistakes...
I have the below code to remove special characters from a sheet called "NewUpload" based on another sheet, "Header_List". "Header_List" contains all the headers in "NewUpload" with additional information against each one. For example column C in "Header_List" is called "Remove Characters?" which if set to "Yes" will remove certain characters from the matching column in "NewUpload"; as specified in the function "RemoveSpecialChars" (which I found online).
Code:
Public Sub RemoveCharactersVer2()
Dim strCell, strCellNew, strHeader, strCol As String
Dim cell, cell2, aCell, rng As Range
Dim col As Long
'Find last row of header list
lastrow = Sheets("Header_List").Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'Find last row of new upload data
lastrow2 = Sheets("NewUpload").Range("B:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'Loop through headers
For Each cell In Sheets("Header_List").Range("A2:A" & lastrow)
Sheets("Header_List").Activate
Sheets("Header_List").Range("A2").Activate
'If Remove Characters? option set then..
If cell.Offset(0, 2).Value = "Yes" Then
'Set strHeader to Related Header code
strHeader = cell.Offset(0, 1)
Sheets("NewUpload").Activate
Sheets("NewUpload").Range("A2").Activate
'Find column that matches Related Header
Set aCell = Sheets("NewUpload").Range("A1:BH1").Find(What:=strHeader, LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'Set col as column number
col = aCell.Column
'Set strCol as column letter
strCol = Col_Letter(col)
'Set rng as column range *2:*lastrow
Set rng = Sheets("NewUpload").Range(strCol & "2:" & strCol & lastrow2)
'For each cell2 in above range..
For Each cell2 In rng
Sheets("NewUpload").Range(strCol & "2").Activate
'Set strCell as the current cell address
strCell = cell.Address
'Set strCellNew as character removed version of string
strCellNew = RemoveSpecialChars(Sheets("NewUpload").Range(strCell))
'Change cell value to character removed version of string
cell.Value = strCellNew
Next cell2
Else
GoTo Skip
End If
Skip:
Next cell
End Sub
However when this code is run it replaces the special characters as expected but it is also replacing the value in column A of "Header_List" to the first replaced value in the "NewUpload" sheet. I password protected the sheet to see where it crashed and it crashed at:
Code:
cell.Value = strCellNew
I spent many hours trying to work out why it is doing this (that is why I am activating cells above - this wasn't originally there) and I just can't see it!
Thank you in advance!