Transform column and clean the value - excel

I am new to VBA and I have this peace of code but I get compile error sub or function not defined
Sub FormatData()
'Format data in selected column as text, remove hidden characters, and add leading zeroes to 8-digit values
Dim rng As Range
Set rng = Selection
'Format column as text
rng.NumberFormat = "#"
'Remove hidden characters and add leading zeroes
For Each cell In rng
cell.Value = Trim(Clean(cell.Value))
If Len(cell.Value) = 8 Then
cell.Value = "0" & cell.Value
End If
Next cell
End Sub
it should run a macro that convert the column to text after it add leading 0 if the value has just 8 characters.
there could be values such as 123456789 but if user enters 012345678 then Excel deletes the 0 at the beginning if the column or cell is not foramtted as text. so I want to add this 0 back and format the column to text so it stays there but why the error?

You can try this, as Clean is not VBA intrinsic function, but WorksheetFunction.Clean() method.
Sub FormatData()
'Format data in selected column as text, remove hidden characters, and add leading zeroes to 8-digit values
Dim rng As Range
Dim cell As Object
Set rng = Selection
'Format column as text
rng.NumberFormat = "#"
'Remove hidden characters and add leading zeroes
For Each cell In rng
cell.Value = Trim(WorksheetFunction.Clean(cell.Value))
If Len(cell.Value) = 8 Then
cell.Value = "0" & cell.Value
End If
Next cell
Set rng = Nothing
Set cell = Nothing
End Sub
See https://learn.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.clean

Related

Comparing selected cell value to a cell range values inside parentheses

I fairly new in excel so don't know much about it or VBA.
I have this dataset shown below, In which first column has some values in it.
I wanted a conditional formatting or VBA formatting(don't know if this exists also!)
to color the data from the 3rd to 5th column based on the selected cell in the first column.
For eg. if I select dbo.project in the 1st column, all the cells having the dbo.project string
in it should be colored/highlighted.
Given Below is the image of my dataset.
The below code will activate whenever a single cell in column 1 is selected, and will color all cells containing the selection's text in columns C:E to vbGreen color.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Target.Column = 1 Or Not Target.Cells.Count = 1 Then Exit Sub
With ThisWorksheet
Dim ColorColumns As Range, Cell As Range, strMatch As String
Set ColorColumns = Intersect(.Range("C:E"), .UsedRange)
strMatch = "*" & Target.Text & "*"
For Each Cell in ColorColumns
If Cell.Text Like strMatch Then Cell.Interior.Color = vbGreen
Next
End With
End Sub

Bold First Occurence in a Series Only Excel VBA

I have a long list of equipment like this;
I would like to be able to run a VBA script that allows excel to change the format of the first of a series so that they are more visible. Is this a possibility?
This is housed in an excel table, not sure if that has an impact.
You could iterate through the range update format if below cell<>previous cell.
Sub UpdateCatHead(ByRef rng As Range, Optional col_index As Integer = 1)
Dim rng_search As Range
'lets ,make sure to have range of one column
Set rng_search = rng.Columns(col_index)
Dim cell As Range, prev_cell As Range
Dim prev_cat As String
For Each cell In rng_search.Cells
'check if empty is empty and exit for?
If cell.Row = 1 Then
'update cell to bold here
Debug.Print (cell.Row)
Else
Set prev_cell = cell.Worksheet.Cells(cell.Row - 1, cell.Column) 'cell above
If CStr(cell.Value) <> CStr(prev_cell.Value) Then
'update cell to bold here
Debug.Print (cell.Row)
End If
End If
Next cell
End Sub
call Sub like this:
UpdateCatHead ThisWorkbook.Sheets("data").Range("A1:A100")

Deleting condition formatting for texts & blanks which may occur at any cell in range(b1:b54)

The conditional formattiong is applied to the range(B1:B54) which contains numbers, text & blank. Once this is done, I am required to re-colour cells in a column back to default one which are coloured either green or red from conditional formatting.
Can anybody give me small script to either delete the CF for texts & blanks in range(B1:B54).
You could try:
Option Explicit
Sub Delete_CF()
Dim rng As Range, cell As Range
With ThisWorkbook.Worksheets("Sheet1") 'Change if needed
'Set the range to loop
Set rng = .Range("B1:B54")
'Loop the range
For Each cell In rng
With cell
'Check if cell is empty or not numeric
If .Value = "" Or Not IsNumeric(.Value) Then
.FormatConditions.Delete
End If
End With
Next cell
End With
End Sub

Filling Blank Rows from Cells Above Conditionally

I originally used some script where the blank rows in the first 3 columns of data in my worksheet were filled from the previous row. The script is:
Dim cell As Range, SearchRange As Range
On Error Resume Next
Set SearchRange = Columns("A:C").SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not SearchRange Is Nothing Then
For Each cell In SearchRange
If cell.row > 1 Then cell = cell.Offset(-1, 0).Value
Next cell
End If
Although that is fine for blank rows in between those columns I have a problem where the blanks are alongside text in column D I do not wish to fill. I tried something like:
If Not Like "*FUEL*" Or Like "ACCOUNTS*"
yet I have trouble with the syntax in using this in a conditional statement. My pasted snip will make sense...I hope. I want to fill just the row beside the word Jacqui in Column D but not Fuel or Accounts. NB. The word Jacqui is not constant.
Excel Sample
Use the cell.row to reference column D.
Dim cell As Range, SearchRange As Range
On Error Resume Next
Set SearchRange = Columns("A:C").SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not SearchRange Is Nothing Then
For Each cell In SearchRange
If cell.Row > 1 And Not (Cells(cell.Row, "D") Like "ACCOUNTS*" Or Cells(cell.Row, "D") Like "FUEL*") Then _
cell = cell.Offset(-1, 0).Value
Next cell
End If

Filling an Empty Cell with Previous Known Value in a column

I have various empty cells in a table that I want to fill with the last known value in that column.
Sub autofiller()
Dim DataRange As Range
Set DataRange = Range("A1:D4")
Dim i As Integer
FillA = ""
For Each cell In DataRange
If cell(1, 1).Value <> "" Then
FillA = cell(1, 1).Value
Else
cell(1, 1).Value = FillA
End If
i = i + 1
Range("C1").Value = i
Next
End Sub
There will be no empty values in the first row. My logic is that it will look through every cell in that range, and if it is not an empty cell, pick up the value until it hits an empty cell where then I will place that value. The i counter is for me to keep track. It seems to me that the code is checking cell by cell horizontally but I want it to check cell by cell vertically. How can I make it check vertically?
I am new to VBA so any additional comments/guides will help.
Try this:
Sub autofiller()
Dim cell as range
Dim DataRange As Range
Set DataRange = Range("A1:D4")
For Each cell In DataRange
If cell.Value = "" Then
cell.Value = cell.Offset(-1).Value
End If
Next
End Sub
Because the for each will go left to right, top to bottom it will fill it with the cell above as it encounters empty cells.
You are confusing your variable cell which is a range variable with Cells() which is a range object. cell(1,1) is equal to cell as it is getting the first cell in a range of one.
Another way to write cell.Offset(-1) would be cell(-1,1) this gets the cell directly above the cell.

Resources