Basically, I want VBA to loop through a range (E2 - last row), and when it finds any month and day "dd/mm" to add on 2014, c.i.p "dd/mm/2014." Secondly, if it finds "n/a" it should highlight that cell in red. My code doesn't do anything, however. What is wrong with my code?
Sub yearstandard()
Dim wb As Workbook
Dim ws As Worksheet
Dim range As range
Dim i As Long
LastRow = FindLastRow(1)
For i = 5 To LastRow 'test row 1 to whatever the last populated row is
Set cell = Cells(i, 2) 'Define cell as the cell to be tested e.g. (cells 1,1) is A1
Select Case cell.Value 'select case value in cell
Case Is = "dd/mm"
cell.Value = "dd/mm/2014" 'wrap each date entered in year 2014
Case Is = "n/a"
cell.Interior.Color = RGB(255, 255, 102) 'highlight cells with value "n/a" in red
End Select
Next i 'go to the next loop counter
End Sub
In your comment you indicated you would be manually entering either 3/14 or n/a into cells.
Assuming your regional settings are US (mdy), and you are going to be entering the data this year, there is no need for VBA. Merely enter 3/14 and Excel will automatically add the current year. With regard to turning the n/a red, just use conditional formatting on the entire column. You might want to custom format the column as d/m/yyyy if you don't "see" the 2014.
If you absolutely must do this in VBA, then something like the below will set those options for range in column E from E2 to the last row.
I have also assumed that there is no other conditional formatting in that column. If there is, then, instead of deleting the old conditional formatting, we will need to preserve it when running the macro.
Sub FormatColE()
With Range("E2", Cells(Rows.Count, "E").End(xlUp))
.NumberFormat = "m/d/yyyy"
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""n/a"""
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1)
.Interior.Color = vbRed
.StopIfTrue = False
End With
End With
End Sub
Related
I have a problem with referencing to other cells in my sheet. I have a VBA that creates tables out of a pivot table and saves them on different sheets. Example of the table:
The number of rows for any name can change, the total row position can therefore change too. I needed to bold the whole subtotal and total rows
(in this case it would be B9:G9, B12:G12, B14:G14, A15:G15) so I tried this:
If Right(cell.Value, 5) Like "Total" Then
With cell
.HorizontalAlignment = xlLeft
.Font.Bold = True
End With
End If
Next cell
which naturally only bolded the one cell which has the 'Total' text in it. The problem is referencing to the other cells in the rows I want to bold - the merged cells are messing that up. When I tried this:
cell.EntireRow.Font.Bold = True
the whole Project got bold (A5:G14). I also tried the offset function, but offset(0,1) sent me to cell C5.
Do you please have any idea how to make the whole total and subtotal rows bold?
Thanks,
B.
You could do something like this:
Sub Tester()
Dim ws As Worksheet, c As Range
Set ws = ActiveSheet
For Each c In ActiveSheet.UsedRange.Cells
If c.Value Like "*Total" Then
'from cell with "*Total" to last column
With ws.Range(c, ws.Cells(c.Row, "G"))
.HorizontalAlignment = xlLeft
.Font.Bold = True
End With
End If
Next c
End Sub
I have this code below that works for updating the format of cells in Column A if the cell starts with "00".
Dim cel As Range
For Each cel In Range("A6:A300")
If Left(cel.Value, 2) = "00" Then
cel.Font.Color = vbWhite
cel.Interior.Color = vbBlack
End If
Next
What I really want to do is when "00" is encountered in a cell in Column A I want white text and black cell interior not only in the Column A cell but to then copy that same formatting across the row from A:W. For example, if "00" is encountered in A7, then I want this format from A7:W7, and so on. Where "00" appears in Column A is different each time the data source is updated so it needs to be flexible.
I'm hoping there is a simple solution where I can add a second range after the THEN statement. Something like below (which does not work):
Dim cel As Range
For Each cel In Range("A6:A300")
If Left(cel.Value, 2) = "00" Then Range("A:W")
cel.Font.Color = vbWhite
cel.Interior.Color = vbBlack
End If
Next
Any help would be greatly appreciated. Thanks!
I have a table which looks like this (it changes dynamically).
I am trying to make a macro which would unlock only the "light yellow" cells = row 1 contains "Forecast" as a value and column I contains "Actual" as a value. I would later protect the sheet so the user would be able to change value only in these cells.
In conditional formatting I use this formula:
=(INDIRECT(ADDRESS(1;COLUMN();4))="Forecast")*(INDIRECT(ADDRESS(ROW();COLUMN($I$1);4))="Actual")
But I've failed to make something like this work in VBA.
Just in case anyone wondered how to do it.
Sub Lock_Cells()
Dim startCol As Long
startCol = WorksheetFunction.Match("Forecast", Range("1:1"), 0) 'Define in which column Forecast starts
Dim i As Long
For i = 2 To Range("I" & Rows.Count).End(xlUp).Row
If Cells(i, "I").Value = "Actual" Then 'Loop through values in column I
Range(Cells(i, startCol), Cells(i, "U")).Locked = False 'Unlock cells
End If
Next i
End Sub
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
Similar to how if you drag a cell down in a spreadsheet it will continue to reference cells in the same row of that formula. I need to find a way that I can search the spreadsheet for letter Y in the I column and if it finds Y in the I column it will then Select the cells in that same row for column B Through AR. then hide just those cells not the entire row. This is what I have so far:
Sub Macro1()
'Sub HideRows()
Dim cell As Range
For Each cell In Range("I1:I5000")
If UCase(cell.Value) = "Y" Then
Select (??? this is where I need to find help selecting the proper range.)
Selection.NumberFormat = ";;;"
End If
Next
Calculate
End Sub
Thanks,
You should be able to use your cell object? No need to Select anything.
For Each cell In Range("I1:I5000")
If UCase(cell.Value) = "Y" Then
cell.NumberFormat = ";;;"
End If
Next
Regarding hiding cells, I don't think you can hide individual cells without hiding the entire row.
cell.EntireRow.Hidden = True
This will set the cell format of column B - H to ;;; on the rows that contain Y in column I.
Sub test()
Dim sht As Worksheet, cell As Range
Dim rangeString As String
Set sht = ActiveSheet
For Each cell In sht.Range("I1:I5000")
If UCase(cell.Value) = "Y" Then
'Columns B --> H
sht.Cells(cell.Row, 2).Resize(1, 7).NumberFormat = ";;;"
End If
Next cell
End Sub
Autofilter would work, but obviously, you're not hiding the cells, since you can only hide full columns or rows. You're just obfuscating the display, but the contents can still be seen in the formula bar.
Sub Macro1()
'Sub HideRows()
Dim cell As Range
with Range("I1:I5000")
.autofilter
.autofilter field:=1,criteria1:="Y"
.offset(1).resize(.rows.count-1).specialcells(xlCellTypeVisible).offset(0,-7).resize(, 43).NumberFormat = ";;;"
.autofilter
end with
End Sub