The below code looks for duplicates in Column A, and if a duplicate exists, it sets the adjacent cell in column H to 0.
I've been trying to amend it to look for duplicates in column A as well as E, then if both are duplicates it will set the adjacent cell in column H to 0.
Would appreciate any help.
Dim Cell As Range
Dim DSO As Object
Dim Rng As Range
Set Rng = Range("A1", Cells(Rows.Count, "A").End(xlUp))
Set DSO = CreateObject("Scripting.Dictionary")
DSO.CompareMode = 1
For Each Cell In Rng
If DSO.Exists(Cell.Text) Then
Cell.Offset(0, 7) = 0
Else
DSO.Add Cell.Text, Cell
End If
Next Cell
Set DSO = Nothing
End Sub
You could do something like this:
Sub Dups()
Dim ws As Worksheet, DSO As Object, Cell As Range, k
Set DSO = CreateObject("Scripting.Dictionary")
DSO.CompareMode = 1 'case-insensitive
Set ws = ActiveSheet 'or some other sheet...
For Each Cell In ws.Range("A1", ws.Cells(Rows.Count, "A").End(xlUp)).Cells
k = Cell.Value & Chr(0) & Cell.EntireRow.Columns("E").Value 'create the key value
If DSO.exists(k) Then
Cell.EntireRow.Columns("H").Value = 0
Else
DSO.Add k, Cell '.Value ?
End If
Next Cell
End Sub
I just added a second dictionary and checked both:
Sub CheckDupes()
Dim Cell As Range
Dim DSO1 As Object
Dim DSO2 As Object
Dim Rng As Range
Set Rng = Range("A1", Cells(Rows.Count, "A").End(xlUp))
Set DSO1 = CreateObject("Scripting.Dictionary")
DSO1.CompareMode = 1
Set DSO2 = CreateObject("Scripting.Dictionary")
DSO1.CompareMode = 1
For Each Cell In Rng
If DSO1.Exists(Cell.Text) And DSO2.Exists(Cell.Offset(0, 4).Text) Then
Cell.Offset(0, 7) = 0
Else
If Not DSO1.Exists(Cell.Text) Then _
DSO1.Add Cell.Text, Cell
If Not DSO2.Exists(Cell.Offset(0, 4).Text) Then _
DSO2.Add Cell.Offset(0, 4).Text, Cell.Offset(0, 4)
End If
Next Cell
Set DSO1 = Nothing
Set DSO2 = Nothing
End Sub
Example:
Related
I have been trying to create a function which checks that if Col"B" <> Empty then copy the third cell which is under the same row.
I have this Data:
Where from i want to copy the Col"D" highlighted cells and paste them into same row where Col"B" <> empty.
Here is the final result. Your help will be appreciated in this regards.
Option Explicit
Sub CopyPasting()
Dim ws As Worksheet
Dim r As Long
Dim LastRow As Long
Dim n As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
LastRow = .Range("D" & .Rows.Count).End(xlUp).Row
For r = LastRow To 2 Step -2
If .Cells(r, "B") <> "" Then
.Rows(r + "D").Copy
.Rows(r + "D").PasteSpecial
n = n + 1
End If
Next
End With
End Sub
Please, try the next code:
Sub testRetOffset3()
Dim sh As Worksheet, lastR As Long, rngV As Range, rngFin As Range, c As Range
Set sh = ActiveSheet 'use here the sheet you need
lastR = sh.Range("B" & sh.rows.count).End(xlUp).row 'last sheet row
On Error Resume Next 'if not empty cells in column, it will not return the range and raise an error
Set rngV = sh.Range("B2:B" & lastR).SpecialCells(xlCellTypeConstants)
On Error GoTo 0
If rngV Is Nothing Then Exit Sub 'stop the code if run on a wrong sheet, without empty cells in column B:B
For Each c In rngV.cells 'iterate between the discontinuous range cells
If rngFin Is Nothing Then 'if the final range is not set (first time)
Set rngFin = c.Offset(3, 2) 'set the range = the Offset necessary cell
Else
Set rngFin = Union(rngFin, c.Offset(3, 2)) 'make a Union between existing range and the Offset necessary cell
End If
Next
If Not rngFin Is Nothing Then 'copy both ranges in consecutive columns
rngV.Copy sh.Range("F2")
rngFin.Copy sh.Range("G2")
End If
End Sub
It will return in columns F:G, starting from the second row. It is easy to modify the range where to return...
You can even clear the existing processed columns and return in B:C, or in another sheet.
Edited:
In order to solve the last request, please use the next code:
Sub testRetOffsetMoreRows()
Dim sh As Worksheet, lastR As Long, rngV As Range, rngFin As Range, A As Range
Set sh = ActiveSheet
lastR = sh.Range("B" & sh.rows.count).End(xlUp).row
On Error Resume Next
Set rngV = sh.Range("A2:D" & lastR).SpecialCells(xlCellTypeConstants)
On Error GoTo 0
If rngV Is Nothing Then Exit Sub
For Each A In rngV.Areas 'iterate between the range areas
If rngFin Is Nothing Then
Set rngFin = A.cells(1,2).Offset(3, 3) 'use the second cell of the area
Else
Set rngFin = Union(rngFin, A.cells(1,2).Offset(3, 3))
End If
Next
If Not rngFin Is Nothing Then
rngV.Copy sh.Range("H2")
rngFin.Copy sh.Range("L2")
End If
End Sub
But take care to have continuous ranges when have a value in column B:B. Otherwise, the code may fail... The areas property will return differently.
I wasn't sure where you wanted the output, this will put it into a sheet called "Sheet2". (You'll have to make that before running the code it won't create it for you.)
Dim i As Long
Dim j As Long
Dim lr As Long
Dim srcWS As Worksheet
Dim destWS As Worksheet
Set srcWS = ThisWorkbook.Sheets("Sheet1")
Set destWS = ThisWorkbook.Sheets("Sheet2")
With srcWS
lr = .Cells(.Rows.Count, 4).End(xlUp).Row
j = 2
For i = 2 To lr
If .Cells(i, 2).Value <> "" Then
destWS.Cells(j, 1).Value = .Cells(i, 2).Value
destWS.Cells(j, 2).Value = .Cells(i, 4).Offset(2, 0).Value
j = j + 1
End If
Next i
End With
If you need the colors copied over as well then use this:
.Cells(i, 4).Offset(2, 0).Copy
destWS.Cells(j, 2).PasteSpecial xlPasteAll
instead of:
destWS.Cells(j, 2).Value = .Cells(i, 4).Offset(2, 0).Value
I am trying to populate column C values on my sheet based on a value in column A. So if column A value is 1, then i want the value in corresponding cell in column C to be 10, else do nothing.
Sub test()
For Each cell In Sheets("Report").Range("A9:A" & LastRow)
If cell.Value = 1 Then
cell.Offset(0, 2).Value = 10
End If
Next cell
End Sub
Would appreciate any help and show me a better way if there is.
thanks
Try below sub.
Sub test()
Dim sh As Worksheet
Dim cel As Range
Dim LastRow As Long
Set sh = ThisWorkbook.Worksheets("Report")
LastRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row
For Each cel In sh.Range("A9:A" & LastRow)
If cel.Value = 1 Then
cel.Offset(0, 2).Value = 10
End If
Next cel
Set sh = Nothing
End Sub
I would like to have a macro that deletes an entire row of the cell value equals "" for multiple ranges. Ranges are "B16:B115, B131:B230, B250:B349".
Logic:
If cell equals "" then delete the entire row.
I want the row actually deleted and not just the contents of the cells.
Thank you.
This would be worth trying:
On Error Resume Next
Range("B16:B115,B131:B230,B250:B349").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error Goto 0
Depending on whether the "" cells have formulas or are just empty.
Range.SpecialCells: What does xlCellTypeBlanks actually represent?
EDIT: if you have formulas then you have to go the long way round:
Sub DeleteEmpty()
Dim c As Range, rngDel As Range
For Each c In Range("B16:B115,B131:B230,B250:B349").Cells
If Len(c.Value) = 0 Then
If rngDel Is Nothing Then
Set rngDel = c
Else
Set rngDel = Application.Union(rngDel, c)
End If
End If
Next c
If Not rngDel Is Nothing Then rngDel.EntireRow.Delete
End Sub
Following sub will allow you to select range to delete blank rows.
Sub RemoveBlanks()
Dim rng As Range, rws As Long, i As Long
Dim LastRow As Range
Dim myRange As Range
Set myRange = Application.InputBox(prompt:="Select Header Cell To Remove Blanks.", Type:=8)
'LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set LastRow = Cells(Rows.Count, myRange.Column).End(xlUp)
Set rng = ActiveSheet.Range(myRange.Address & ":" & LastRow.Address)
rws = rng.Rows.Count
For i = rws To 1 Step (-1)
If WorksheetFunction.CountA(rng.Rows(i)) = 0 Then rng.Rows(i).EntireRow.Delete
Next
Set myRange = Nothing
Set LastRow = Nothing
Set rng = Nothing
End Sub
I have 5 columns. If column 3 has no value, I want all other adjacent cells (column 1,2,4,5) to clear.
I got this from another site:
Sub ClearCust()
'Clears data in column if there is no Amt number next to it.
'Used in conjunction to fill blanks.
Dim j As Range
For Each j In Workbooks("OH Details_v1").Worksheets("Sheet1").Range("C2:D" & Worksheets("Sheet1").Range("a65536").End(xlUp).Row)
If j.Value = 0 Then
j.Offset(0, 1).ClearContents
End If
Next j
End Sub
But it only clears column C, D, E...
Something like this might be what you're looking for:
Sub ClearCust()
Dim wb As Workbook
Dim ws As Worksheet
Dim rLast As Range
'Set wb = Workbooks("OH Details_v1")
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
Set rLast = ws.Range("A:E").Find("*", ws.Range("A1"), xlValues, , , xlPrevious)
If rLast Is Nothing Then Exit Sub 'No data
With ws.Range("C1:C" & rLast.Row)
.AutoFilter 1, "="
Intersect(.Parent.Range("A:E"), .Offset(1).EntireRow).ClearContents
.AutoFilter
End With
End Sub
EDIT:
To address your request of iterating over sets of columns to perform this same task, you can do something like this:
Sub ClearCust()
Dim wb As Workbook
Dim ws As Worksheet
Dim rLast As Range
Dim aClearAreas() As String
Dim i As Long
'Set wb = Workbooks("OH Details_v1")
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
ReDim aClearAreas(1 To 3, 1 To 2)
'Define columns that will be cleared Define column within that range to evaluate for blanks
aClearAreas(1, 1) = "A:E": aClearAreas(1, 2) = "C"
aClearAreas(2, 1) = "F:J": aClearAreas(2, 2) = "H"
aClearAreas(3, 1) = "K:O": aClearAreas(3, 2) = "M"
'loop through your array that contains your clear area data
For i = LBound(aClearAreas, 1) To UBound(aClearAreas, 1)
'Get last populated row within the defined range
Set rLast = ws.Range(aClearAreas(i, 1)).Find("*", ws.Range(aClearAreas(i, 1)).Cells(1), xlValues, , , xlPrevious)
If Not rLast Is Nothing Then
'Filter on the column to be evaluated
With ws.Range(aClearAreas(i, 2) & "1:" & aClearAreas(i, 2) & rLast.Row)
.AutoFilter 1, "=" 'Filter for blanks
Intersect(.Parent.Range(aClearAreas(i, 1)), .Offset(1).EntireRow).ClearContents 'Clear cells only in the defined range
.AutoFilter 'Remove the filter
End With
End If
Next i
End Sub
Your explanation and title are two different subjects but based on your explanation-i understand you want to loop through column C and if a cell is empty, then you make other cells value to blank-i wrote below code. You may use. Tested
Sub test()
Dim ws As Worksheet
Set ws = ActiveSheet
With ws
lr = .Cells(Rows.Count, "C").End(xlUp).Row
For i = 2 To lr
If .Cells(i, "C") = "" Then
.Cells(i, "A") = ""
.Cells(i, "B") = ""
.Cells(i, "D") = ""
.Cells(i, "E") = ""
End If
Next i
End With
End Sub
Sub FillEmptyCell()
Dim rng As Range
Dim i As Long
Dim cell As Range
Dim sht As Worksheet
Set sht = ActiveWorkbook.Sheets("Sheet1")
sht.Activate
Set rng = Range(Range("C12"), Range("AD" & sht.UsedRange.Rows.Count))
For Each cell In rng
If cell.Value = "" Then
cell.Value = "0"
End If
Next
End Sub
I am trying to fill my blank spaces in sheet with zero dynamically.However,
I don't want this to fill zeroes in row that have no data. can someone help please?
See how this works,
Sub ZeroStuff()
Dim LstRw As Long, rng As Range, sh As Worksheet, c As Range
Set sh = Sheets("Sheet1")
With sh
LstRw = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
Set rng = .Range("C12:C" & LstRw).SpecialCells(xlCellTypeBlanks)
For Each c In rng.Cells
.Range(c.Offset(, 1), c.Offset(, 27)) = 0
Next c
End With
End Sub