Delete whitespaces vba Excel - vba

I want to delete all whitespaces in a range and got the following code
Dim rng2 As Range
Dim cleanString2 As String
Set rng2 = ContactData.Range("AR2:AR" & lRow)
rng2.Value = Application.Trim(rng2)
For j = 2 To lRow
cleanString2 = ContactData.Range("AR" & j).Value
cleanString2 = Replace(cleanString2, Chr(10), "")
ContactData.Range("AR" & j).Value = cleanString2
Next j
I got and equivalent code with same variables and everything just without the 2 in the variable names the code works, but the other code goes for colum A instead of AR, can anybody help me with finding the bug?
Thanks!
Dim rng As Range
Dim cleanString As String
Set rng = ContactData.Range("A2:A" & lRow)
rng.Value = Application.Trim(rng)
For i = 2 To lRow
cleanString = ContactData.Range("A" & i).Value
cleanString = Replace(cleanString, Chr(10), "")
ContactData.Range("A" & i).Value = cleanString
Next i
this is the working code, below a picture of the variable values while debugging:

Rather than do it row by row you could replace all whitespace in one hit.
The problem you're finding is that you're referencing different ranges.
The original code states:
Set rng = ContactData.Range("A2:AR" & lRow)
Your code has:
Set rng = ContactData.Range("A2:A" & lRow) - missing the ref to column AR.
You could use the below code to remove the whitespace (Chr(32). As #Rory said - Chr(10) is a linefeed.
Sub Test()
Dim rng As Range
Dim lRow As Long
lRow = 15
Set rng = ContactData.Range("A2:AR" & lRow)
rng.Replace What:=Chr(32), Replacement:="", LookAt:=xlPart
End Sub

Judging from your code, the problem is that you are hoping that:
cleanString = Replace(cleanString, Chr(10), "")
would remove the whitespaces. Chr(10) is not a whitespace as per the ASCII table. Thus, the easiest way is probably to go like this:
cleanString = Replace(cleanString, " ", "")
If this does not work, as a workaround try this:
Public Sub RemoveSpaceInString()
Dim myCell As Range
For Each myCell In Selection
myCell = Trim(myCell)
myCell = Replace(myCell, vbTab, "")
myCell = Replace(myCell, " ", "")
myCell = Replace(myCell, Chr(160), "")
Next myCell
End Sub
It uses Selection, because it was intended to be used outside working code, as a "format-helping" tool. However, if it works, it would be quite easy to write it inside your code.

Related

A better way in VBA to remove a whole row if one cell contains one certain word?

I wrote the following code, which looks for 3 words in the column G and then in case, that one of those occurs it delete the whole row.
However, it is not so efficient(quick). I guess because of 3 If and ElseIf.
Does someone know a better way to do it?
Last = Workbooks("reportI.xlsm").Sheets("SII_I").Cells(Rows.Count, "G").End(xlUp).Row
For i = 2 To Last Step 1
If (Workbooks("reportI.xlsm").Sheets("SII_I").Cells(i, "G").Value) = "01NU SfG" Then
Workbooks("reportI.xlsm").Sheets("SII_I").Cells(i, "A").EntireRow.Delete
'
'with the word "01NU" in column G
ElseIf (Workbooks("reportI.xlsm").Sheets("SII_I").Cells(i, "G").Value) = "01NU" Then
Workbooks("reportI.xlsm").Sheets("SII_I").Cells(i, "A").EntireRow.Delete
'with the word "11G SfG" in column G
ElseIf (Workbooks("reportI.xlsm").Sheets("SII_I").Cells(i, "G").Value) = "11G SfG" Then
Cells(i, "A").EntireRow.Delete
End If
Debug.Print i
Next i
You can use just one if clause by using the OR operator.
If "A1"= "01NU OR "A1" = "SfG" OR "A1" = "11G SfG" Then
'delete row
Alternatively, you can get your macro to filter that column for the values 01NU, SfG, 11G SfG, and then delete all the filtered rows. This is definitely more faster.
Just replace range A1 by your required range.
Another solution:
Sub Demo()
Dim delItems As String
Dim rng As Range, searchRng As Range, cel As Range
Dim lastRow As Long
delItems = "01NU SfG,01NU,11G SfG" 'search items
With Workbooks("reportI.xlsm").Sheets("SII_I")
lastRow = .Cells(Rows.Count, "G").End(xlUp).Row
Set searchRng = .Range("G1:G" & lastRow)
For Each cel In searchRng
If InStr(1, delItems, cel.Value, vbTextCompare) Then
If rng Is Nothing Then
Set rng = .Rows(cel.Row)
Else
Set rng = Union(rng, .Rows(cel.Row))
End If
End If
Next cel
End With
rng.Delete
End Sub
The code would need a little alteration to fit your needs, but this answer is very robust and scalable.
For example:
Sub Sample()
Dim DeleteThese(3) As String, strg As String
Dim rng As Range
Dim Delim As String
Dim Last As Long
Dim ws As Worksheet
Set ws = Workbooks("reportI.xlsm").Sheets("SII_I")
Last = ws.Cells(Rows.Count, "G").End(xlUp).Row
Delim = "#"
DeleteThese(0) = "01NU SfG"
DeleteThese(1) = "01NU"
DeleteThese(2) = "11G SfG"
strg = Join(DeleteThese, Delim)
strg = Delim & strg
For i = 2 To Last Step 1
If InStr(1, strg, Delim & ws.Range("G" & i).Value & Delim, vbTextCompare) Then _
ws.Range("G" & i).EntireRow.Delete
Next i
End Sub

Excel VBA - Formula writing error

I am trying to bring a formula inside my vba code and I am getting an error inside it. Please have a look into the code and kindly share your thoughts.
This is my excel function that was written in VBA Code :
GetUniqueCount(Range,Value)
And here is the VBA Code trying to make use of it :
Sheets("sheet2").Activate
With ThisWorkbook.Sheets("sheet2").UsedRange
lastrow = .Rows(.Rows.Count).Row
End With
For i = 14 To lastrow
check = Range("h" & i).Value
If check <> "" Then
Range("I" & i).Value = WorksheetFunction.GetUniqueCount(sheet1!.Range("A1:B100"), check)
Else
Range("I" & i).Value = ""
Next
The range for the function comes from a different sheet. How do I write it in VBA?
This is the function for it :
Function GetUniqueCount(Rng1 As Range, Lookup As String) As Long
Dim x, dict
Dim i As Long, cnt As Long
Set dict = CreateObject("Scripting.Dictionary")
x = Rng1.Value
For i = 1 To UBound(x, 1)
If x(i, 1) = Lookup Then
dict.Item(x(i, 1) & x(i, 2)) = ""
End If
Next i
GetUniqueCount = dict.Count
End Function
You have other possible errors in your code, unqualified Range, etc.
Since your Function GetUniqueCount is not Excel's built in WorksheetFunction, but your own UDF, you don't need to call it with WorksheetFunction.GetUniqueCount but just GetUniqueCount.
Try the code below:
Option Explicit
Sub Test()
Dim LastRow As Long, i As Long
Dim check As String
With ThisWorkbook.Worksheets("sheet2")
LastRow = .Cells(.Rows.Count, "I").End(xlUp).Row
Dim Rng As Range
Set Rng = Worksheets("sheet1").Range("A1:B100")
For i = 14 To LastRow
check = .Range("H" & i).Value
If check <> "" Then
.Range("I" & i).Value = GetUniqueCount(Rng, check)
Else
.Range("I" & i).Value = ""
End If
Next i
End With
End Sub
There is no worksheet function by the name of GetUniqueCount. If this is a function you have in your code then the way to call it would be like this:-
Range("I" & i).Value = GetUniqueCount("Sheet1".Range("A1:B100"), check)
This code presumes that your function is either on the same code sheet as the calling procedure or declared public. It must take two arguments, the first of which must be a range, the second of the same data type as check. If you didn't declare check (which isn't a good idea) then its data type will be Variant.

Take out characters and put in a new column in Excel

Hi I'm a bit new to vba so I will try to explain my problem as far as possible.
I have a dataset in Excel in Column A, I have a lot of file names like this:
1. AB000**1234**45.tif
2. AB000**1235**45.tif
3. AB000**1236**45.tif
4. AB000**1237**45.tif
etc..
From this I want to take out all the strong characters and put in column C so it will look like this:
1. 1234
2. 1235
3. 1236
4. 1237
etc..
At the moment I have a code that looks like this:
Sub TakeOut
Dim str1 As String
Dim LR As Long
Dim cell As Range, RNG As Range
LR = Range("A" & Rows.Count).End(xlUp).Row
Set RNG = Range("A1:A" & LR)
For Each cell In RNG
L = Len(RNG)
If L > 0 Then
RNG = ...
End If
Next cell
Range("C:C").Columns.AutoFit
End Sub
I have tried to count left(5) and right(6) but don't know how to take out the 4 character that I want.
Hope you can help me with this.
If you want to take out the strong characters from the string. Try it below. It will take all the Bold Characters in a cell and place it in C column.
Hope you are looking for this?
Sub get_bold_content()
Dim lastrow, i, j, totlength As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastrow
totlength = Len(Range("A" & i).Value)
For j = 1 To totlength
If Range("A" & i).Characters(j, 1).Font.Bold = True Then
outtext = outtext & Range("A" & i).Characters(j, 1).Text
End If
Next j
Range("C" & i).Value = outtext
outtext = ""
Next i
End Sub
Take a look at the Mid() Function link.
usage in your case:
Mid(cell.Value, 6, 4) 'First parameter is the string, 6 is the start character, 4 is length
The easiest way without looping would be something like this:
Sub TakeOut()
Dim rng As Range
Set rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
rng.Offset(, 1) = Evaluate("IF(" & rng.Address & "="""","""",MID(" & rng.Address & ",6,4))")
End Sub

VBA Excel - Select rows with Left value = to variable

Having trouble with this code. No errors but also doesn't seem to do anything.
In my sheet, column "M" has some values that start with the letter "T" I want to select the entire row for these. Thanks in advance.
Sub trace1()
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Left(Range("M" & i).Value, 1) = trace Then Rows("i:i").Select
Next i
End Sub
One possible way to answer the question as written:
Sub trace1()
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
Dim SelectedRows As Range
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Not SelectedRows Is Nothing Then
If Left(Range("M" & i).Value, 1) = trace Then Set SelectedRows = Union(SelectedRows, Rows(i))
Else
If Left(Range("M" & i).Value, 1) = trace Then Set SelectedRows = Rows(i)
End If
Next i
SelectedRows.Select 'Replace with .Copy if that's what you really wanted.
End Sub
If you are trying to select the row assigned to variable "i", you would use:
Sub trace1()
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Left(Range("M" & i).Value, 1) = trace Then Rows(i).Select
Next i
End Sub
"Rows("i:i")" won't work. Try collecting all the addresses of all ranges in one string and then selecting the string. Note the comma which separates each range.
Sub trace1()
Dim sRange As String
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Left(Range("M" & i).Value, 1) = trace Then sRange = sRange & "," & i & ":" & i
Next i
Range(Mid(sRange, 2)).Select
End Sub
using AutoFilter to give you the range as is, or the actual address
avoids slow loops
Sub trace2()
Dim strTrace As String
Dim strAddress
Dim rng1 As Range
strTrace = "T"
Set rng1 = Range([m1], Cells(Rows.Count, "M").End(xlUp))
With rng1
.AutoFilter 1, strTrace & "*"
Set rng1 = rng1.Cells(1).Offset(1, 0).Resize(rng1.Rows.Count - 1, 1)
strAddress = rng1.SpecialCells(xlVisible).EntireRow.Address
End With
MsgBox "rows that start with " & strTrace & vbNewLine & strAddress
ActiveSheet.AutoFilterMode = False
End Sub
The expression
Rows("i:i").Select
throws an error -- Rows() won't recognize the text value "i:i" as an argument.
Rows(i).Select
will work. But it won't DO anything you can see, other than the last row should be selected when the code is finished running. You may want to do whatever needs to be done to the "T" rows at the next step in your code before your get to the Next i step.
EDIT:
OK, you want multiple rows selected when the code is finished. That can be done:
Dim RowsDescript As String
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If Left(Range("M" & i).Value, 1) = trace Then RowsDescript = RowsDescript & i & ":" & i & ","
Next i
If Len(RowsDescript) > 0 Then
RowsDescript = Left(RowsDescript, Len(RowsDescript) - 1) ' removes the last comma
Range(RowsDescript).Select
End If
What you want to end up with is an expression that looks like this:
Range("9:9,12:12,16:16").Select
How you get there is, when a row is identified as having the "T" that you want in it, add the row number and a colon and the row number again and a comma to the string RowDescript. So at the end of the loop, you end up with the string having
9:9,12:12,16:16,
in it. But we need to strip off that last comma, so there's the check for a non-zero length string, remove the last character, and then select those rows.
1."T" is not equal to "t", to remove "case sensitivity" it is required to use LCase or UCase (low case or upper case)
2.Rows("i:i") replaced by Row(i)
Sub trace1()
Dim trace As String
trace = "T"
Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If UCase(Left(Range("M" & i).Value, 1)) = UCase(trace) Then Rows(i).Select
Next i
End Sub
And also one comment, at the final will be selected only last row in range, for example row 1,5 and 10 will start from "T", so at the end will be selected only 10th row
updated against question in the comments
this will allow you to select rows which starting from "t" or "T", but this method allow to select not more than 45 rows.
Sub Macro1()
Dim trace$, LR&, i&, Rng$
trace = "T": Rng = ""
LR = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If UCase(Left(Range("M" & i).Value, 1)) = UCase(trace) Then Rng = Rng & i & ":" & i & ","
Next i
Rng = Left(Rng, Len(Rng) - 1)
Range(Rng).Select
End Sub
Use copy/paste one by one row if you need copy rows from one sheet to another, or sort range and select range from first row to the last row where cell value start from "T"

Sum of a column

I want to calculate Sum of different columns in a worksheet and fill it in another worksheet.
LastrowA = Weight.Cells(Weight.Rows.Count, "A").End(xlUp).Row
Set Rng = Weight.Range("A2" & LastrowA)
Weight.Activate
Summ= WorksheetFunction.Sum(Rng) ' Doesn't work
Summary.Cells(1, 1).Value=Summ
Summary.Cells(1, 1).Value = Application.Sum(Rng) ' Doesn't Work
The two Sheets are Weight and Summary. I have tried above two ways and both give me an answer of Zero. I want to continue doing it for all my columns . Please advice. Thank you.
This sub will sum data in Sheet 1 Columns A to C and put results in Sheet2
You can use this sub and just change column letters and output cells.
Hope this helps
Sub SumRange()
Dim wb as Workbook
Set wb = Thisworkbook
Dim ws as worksheet
Set Weight = wb.Sheets("Weight")
LastRow1 = Weight.Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Weight.Range("A2:A" & "" & LastRow1 & "")
Col1Sum = WorksheetFunction.Sum(Rng)
LastRow1 = Weight.Range("B" & Rows.Count).End(xlUp).Row
Set Rng = Weight.Range("B2:B" & "" & LastRow1 & "")
Col2Sum = WorksheetFunction.Sum(Rng)
LastRow1 = Weight.Range("C" & Rows.Count).End(xlUp).Row
Set Rng = Weight.Range("C2:C" & "" & LastRow1 & "")
Col3Sum = WorksheetFunction.Sum(Rng)
ThisWorkbook.Sheets("Sheet2").Cells(2, 2).Value = Col1Sum
ThisWorkbook.Sheets("Sheet2").Cells(3, 2).Value = Col2Sum
ThisWorkbook.Sheets("Sheet2").Cells(4, 2).Value = Col3Sum
End Sub
first of all, i have no excel here, so i cant try by myself what I'm thinking.
but i think you use the Range-Method the wrong way.. it should look like this:
Set Rng = Weight.Range("A2" , Cells( LastrowA , "A") )
so there is a "," between the arguments instead of a "&" an there is a second Cell instead of a row-number.
Hope that helps