I need to a create similar macro for duplicates (see below). The user will be able to choose a column symbol and then all the duplicates from the chosen column will be highlighted with color. I do not how to do that.
Below is the same idea but with empty cells.
Could you help? THX!
Sub EmptyCells()
Dim kol As String
Dim ost As Long
ost = Cells(Rows.Count, "A").End(xlUp).Row
kol = InputBox("Enter column symbol: B, C...etc.", "Column symbol", "B")
If kol = vbNullString Then Exit Sub
If IsNumeric(kol) Then
MsgBox "You entered number, please enter column symbol", _
vbInformation, "ERROR"
Exit Sub
End If
If ost < 5 Then Exit Sub
Range("A5:E" & ost).Interior.Color = xlNone
Range(Cells(5, kol), Cells(ost, kol)).SpecialCells(xlCellTypeBlanks).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
Please make the following changes as per comments in your program. After changes it works for me. Assuming I have cleared existing formats on the worksheet and it is going to be first format condition.
Sub DuplicateCells() ' changed sub name
Dim kol As String
Dim ost As Long
ost = Cells(Rows.Count, "A").End(xlUp).Row
kol = InputBox("Enter column symbol: B, C...etc.", "Column symbol", "B")
If kol = vbNullString Then Exit Sub
If IsNumeric(kol) Then
MsgBox "You entered number, please enter column symbol", _
vbInformation, "ERROR"
Exit Sub
End If
If ost < 5 Then Exit Sub
Range("A5:E" & ost).Interior.Color = xlNone
Range(Cells(5, kol), Cells(ost, kol)).Select ' Remove SpecialCells(xlCellTypeBlanks)
Selection.FormatConditions.AddUniqueValues 'Add this line
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 'Add this line
Selection.FormatConditions(1).DupeUnique = xlDuplicate 'Add this line
With Selection.FormatConditions(1).Interior ' add FormatConditions(1)
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Results on my sample data are shown.
Related
I have 3 statuses in a dropdown list in Col C. namely "N/A", "Pending" and "Completed" and a date column in Col B. If the user selects N/A from the dropdown in Col C. it locks the date field to the left and provides some formatting to the field. However, if the user selects another status besides "N/A" it clears the formatting and N/A text from date field. I have the code below which does everything I need it to, except that if a date is entered into the field and then I select "completed" or "pending" it clears the field - which it shouldn't. Please help.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Count = 1 Then
If Not Intersect(Target, Range("C3:C13,C18:C28")) Is Nothing Then
Application.EnableEvents = False
If Target.Value = "N/A" Then
With Target(1, 0)
.Value = "(N/A)"
.Enabled = False
With .Interior
.Pattern = xlLightDown
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Else
With Target(1, 0)
.Value = ""
.Enabled = True
With .Interior
.Pattern = xlPatternNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
End If
Application.EnableEvents = True
End If
End If
End Sub
Given your description of what you want, the following code should get you there. I think it may have been how you aimed at the adjacent cell.
I'm not sure how you lock the date field as I can't see where you Protect the sheet in your code - I'll leave that part to you. Let me know how it goes.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo GetOut
Application.EnableEvents = False
If Not Intersect(Range("C3:C13,C18:C28"), Target) Is Nothing Then
If Target.Text = "N/A" Then
With Target.Offset(0, -1)
.Value = "(N/A)"
.Locked = True
With .Interior
.Pattern = xlLightDown
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Else
With Target.Offset(0, -1)
.Value = ""
.Locked = False
With .Interior
.Pattern = xlPatternNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
End If
End If
Continue:
Application.EnableEvents = True
Exit Sub
GetOut:
MsgBox Err.Description
Resume Continue
End Sub
Conditional Formatting Condition:If selected cell("cel7") is not blank then put Black fill on it.
How can i modify my current code in such away that conditional formatting condition is used in cel7.
I tried to use xlnoblankscondition but i could not find any VBA examples of it on web.
P.S:As i have written all cel7 cell as C1,every condition will be true ie NOT BLANK.
x = ws.Range("A4").Value
y = ws.Range("A5").Value
ocol = 4
Set cel = Range("E6")
Set cel7 = cel.Offset(2, 0)
For m = 1 To x
For o = 1 To y
cel7.Value = "C1"
cel7.Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Set cel7 = cel7.Offset(4, 0)
Next
Set cel = cel.Offset(0, ocol)
Set cel7 = cel7.Offset(0, ocol)
Next
I'm sorry as I'm still not clear on what you mean.
Anyway, I'm guessing that you want to coding the Conditional Formatting, just like when you do it manually.
I find the code below after I macro recording my manual step in Conditional Formatting.
I think the code in your condition maybe like this :
Sub test()
Cells.FormatConditions.Delete
cel7.Select
cf = cel7.Address(0, 0)
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=NOT(ISBLANK(" & cf & "))"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Application.WindowState = xlMaximized
End Sub
I try the code above by having cel7 variable refer to cell D10.
After I run the code, if I type something in cell D10, D10 fill black with white font.
If I clear the content of D10, D10 back to normal (no fill).
Also I try by having cel7 variable to a range D2 to D10.
If I type on any cell within D2:D10, the cell fill black with white font.
If I clear it, the cell back to normal.
But once again, maybe that's not what you want to achieve.
If I'm not mistaken read your code, it seems that your cel7 formatting is a non-contagious row. So please try your o loop like this one :
Cells.FormatConditions.Delete 'put this line before m loop
For m = 1 To x
For o = 1 To y
Cel7.Select
cf = Cel7.Address(0, 0)
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=NOT(ISBLANK(" & cf & "))"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Application.WindowState = xlMaximized
Set Cel7 = Cel7.Offset(4, 0)
Next o
In the code below I took out your Selection of Cel7. You can address the range directly. I also added variable declarations. Omitting them causes more work than it saves. For the rest of it, the cell color is applied if the cell is found not to be Empty.
Sub Macro1()
Dim Ws As Worksheet
Dim Cel As Range, Cel7 As Range
Dim Tmp As Variant
Dim oCol As Long
Dim x As Long, y As Long
Dim m As Long, o As Long
Set Ws = ActiveSheet
x = Ws.Range("A4").Value
y = Ws.Range("A5").Value
oCol = 4
Set Cel = Ws.Range("E6")
Set Cel7 = Cel.Offset(2, 0)
For m = 1 To x
For o = 1 To y
With Cel7
Tmp = "C1" ' avoid read/write to sheet multiple times
.Value = Tmp
If IsEmpty(Tmp) Then
.Interior.Pattern = xlNone
Else
.Interior.Color = vbBlack
End If
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Set Cel7 = Cel7.Offset(4, 0)
Next o
Set Cel = Cel.Offset(0, oCol)
Set Cel7 = Cel7.Offset(0, oCol)
Next m
End Sub
So I wrote a makro, which inserts a new Column in every worksheet except the first. It works just fine. My only problem is, that I would like it to jump back to the sheet I started at after finishing up the last worksheet. All solutions I found online said, that the line: Sheets("Name of Sheet").Select should do the deed. However it doesn't do it for me. What am I doing wrong? I would also appreciate suggestions to improve the code.
Option Explicit
Sub NeueSpalte()
Dim ende As Boolean
Dim Name As Variant
Dim Anzahl_WS As Integer
Dim Zaehler As Integer
Do While ende = False
Name = InputBox("Name der neuen Spalte")
If StrPtr(Name) = 0 Then
MsgBox ("Abgebrochen!")
Exit Sub
ElseIf Name = "" Then
MsgBox ("Bitte etwas eingeben")
Else
Exit Do
End If
Loop
Anzahl_WS = ActiveWorkbook.Worksheets.Count - 1
Sheets("Rechte auf Verträge der A3").Select
Application.ScreenUpdating = False
For Zaehler = 1 To Anzahl_WS
Cells(1, 2).EntireColumn.Copy
Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Activate
ActiveCell.EntireColumn.Insert
Application.CutCopyMode = False
Cells(1, Columns.Count).End(xlToLeft).Activate
ActiveCell.EntireColumn.Select
Selection.ClearContents
Cells(8, 2).MergeCells = False
Cells(1, Columns.Count).End(xlToLeft).Offset(7, 1).Activate
Range(Cells(8, 2), ActiveCell).MergeCells = True
Cells(8, 2).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Value = Name
If ActiveSheet.Name = "Rechte auf Verträge der WW" Then
Exit Sub
Else
ActiveSheet.Next.Select
End If
Next Zaehler
Application.ScreenUpdating = True
Sheets("Rechte auf Verträge der A3").Select
End Sub
expected result: copy column b into first empty column, delete its contents and insert the user picked name in row 1 of the new column. Do that for every sheet and jump back to sheet number 2
actual result: it does everything just fine, but doesn't jump to sheet 2
I figured it out. The problem was at the end of my for loop, inside the if branching I wrote 'Exit Sub', if it is at the last sheet. I just put the 'Sheets("Rechte auf Verträge der A3").Select' before the 'Exit Sub'
A small example:
Option Explicit
Sub Select_Sheet()
'Declare the variable
Dim ws1 As Worksheet
'Set the variable
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
'Create a with statement to avoid repetition
With ws1
'Refer to range A1
.Range ("A1")
'Refer to the value of range A1
.Range("A1").Value
End With
End Sub
Please note that when you create a With Statement ranges used should have a dot "." before the rannge .Range("A1").Value
working on work project and i am stuck.
I allready have a function thats add a new row over active cell.
Now i want to add grey color to new row, and when new row cells has letters or numbers in it, it will appear as no color (hvite). SEE IMAGE OF PROJECT HERE
Also i dont want the color to go longer than column S as ilustrated in image.
Im not the author of this code. And theres is much i dont even understand. Code goes as follows. AND THERE MAY BE SOME TYPE ERRORS IN THIS CODE, HAD TO WRITE IT FROM A COMPUTER TO ANOTHER. THE CODE BELOW WORKS. just need to add the color to the row
`Sub insert_row()
Dim LineNumber As Integer
Dim insertionpoint
Dim Rownumber, Positionrow As Integer
Dim MarkedArea As String
Application.ScreenUpdating = False 'Stops screenupdating
Insertionpoint = ActiveCell.Address
LineNumber = ActiveCell.Row
For Rownumber = 5 To 1000
If Range("B" & Rownumber).Value = "PLACE" Then
Positionrow = Rownumber + 1
End If
Next Rownumber
If LineNumber < Positionrow - 5 And LineNumber > 6 Then
Range(Insertionpoint).Select
Selection.EntireRow.Insert 'Inserts new row over active cell
LineNumber = ActiveCell.Row
Range("A" & LineNumber).Select
ActiveCell.FormulaR1C1 = "=IF(RC[1]="""","""",TEXT(RC[1],""DDMM"")&""0""&RC[2])"
'More cell properties .....
'More .....
'More .....
MarkedArea = "B" & LineNumber & ":X" & LineNumber
Range("B" & LineNumber).Select
'SetStandardFormat
Range("AB6:AS6).Select ' not shown in picture
Selection.Copy
Range(Insertionpoint).Select
Selection.PasteSpecial Paste:=x1PasteFormats, Operation:=x1None, _
SkipBlanks:=False, Transpose:=False
Else
MsgBox ("Row can not be added here")
End If
Application.ScreenUpdating = False
End Sub`
Also there is a button with this in it
Private Sub CommandButton2_Click()
'add row
Insert_row
End Sub
Hope for some help! Thanks.
You just want a grey-color to the added row?
Insertionpoint = ActiveCell.Address
Range(Insertionpoint).Select
Selection.EntireRow.Insert
With Range(Insertionpoint).EntireRow.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
Giving what I used to test... didn't fix any .select, and pulled out what I needed to test, from your code.
Edit
Adding some code for the loop to add color... will assume that the date is in Column B:
Dim i As Long, LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row 'assumes column A is contiguous
For i = 2 To LR 'Assumes row 1 is headers
If Cells(i, "B").Value = "" Then
With Rows(i).EntireRow.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
Else
Rows(i).EntireRow.Interior.Color = xlNone
End If
Next i
I'm new to VBA coding and please help me create a VBA script with the following conditions.
Should highlight cells containing decimals.
Should highlight cells with number of characters less than 3 or more than 6.
Should execute from Column G (G1) till the last row last used cell.
My data is alphanumeric or numeric.
I have tried using characters.count and Value.count but it didn't work out. Hope it will work with len, but I'm not sure how to start with.
Attached is the sample excel file with highlighted cells
I have tried the below code. Since my data is alphanumeric, characters count doesn't help.
Sub HighlightCells()
Range(" G1").Select
Do
If ActiveCell.Characters.Count < 3 Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row last used cell
Loop Until ActiveCell = ""
Range(" G1").Select
Do
If ActiveCell.Characters.Count > 6 Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row last used cell
Loop Until ActiveCell = ""
End Sub
Before:
This code is almost a direct translation of your description in English into VBA:
Sub Dural()
Dim N As Long, i As Long, s As String, L As Long
N = Cells(Rows.Count, "G").End(xlUp).Row
For i = 1 To N
s = Cells(i, "G").Text
L = Len(s)
If InStr(1, s, ".") > 0 Or (L < 3 Or L > 6) Then
With Cells(i, "G").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next i
End Sub
and after:
Sub Test()
Application.ScreenUpdating = False
LastRow = Rows(ActiveSheet.UsedRange.Row + _
ActiveSheet.UsedRange.Rows.Count - 1).Row
LastCol = Columns(ActiveSheet.UsedRange.Column + _
ActiveSheet.UsedRange.Columns.Count - 1).Column
For Each cll In Range(Cells(1, 7), Cells(LastRow, LastCol))
s = cll.Value
l = Len(s)
If ((l > 0) And (l < 3)) Or (l > 6) Or (s Like "*#.#*") _
Then cll.Interior.Color = vbRed
Next cll
Application.ScreenUpdating = True
End Sub