How can I concatenate unique cell values in every row to adapt in the code below. Removing duplicate values in a cell. Result after macro is the second image.
Sub Concatenar()
Dim myRange As Range
Dim c As Long
Dim r As Long
Dim txt As String
Set myRange = Application.InputBox("Selecione a primeira e a Ășltima cĂ©lula:", Type:=8)
For r = 1 To myRange.Rows.Count
For c = 1 To myRange.Columns.Count
If myRange(r, c).Text <> "" Then
txt = txt & myRange(r, c).Text & vbLf
End If
Next
If Right(txt, 1) = vbLf Then
txt = Left(txt, Len(txt) - 1)
End If
myRange(r, 1) = txt
txt = ""
Next
Range(myRange(1, 2), myRange(1, myRange.Columns.Count)).EntireColumn.Delete
End Sub
This does what you want, I believe. It pastes/tranposes the values to a temporary workbook, uses RemoveDuplicates to trim them down, and Join to munge them all together. It then pastes the munged values back into column A of the original workbook and deletes the other columns.
Because of the destructive nature of this code, you must test it on a copy of your data:
Sub CrazyPaste()
Dim wsSource As Excel.Worksheet
Dim rngToConcat As Excel.Range
Dim wbTemp As Excel.Workbook
Dim wsPasted As Excel.Worksheet
Dim rngPasted As Excel.Range
Dim i As Long
Dim LastRow As Long
Dim Results() As String
Set wsSource = ActiveSheet
Set rngToConcat = wsSource.UsedRange
Set wbTemp = Workbooks.Add
Set wsPasted = wbTemp.Worksheets(1)
wsSource.UsedRange.Copy
wsPasted.Range("A1").PasteSpecial Transpose:=True
Set rngPasted = wsPasted.UsedRange
ReDim Results(1 To rngPasted.Columns.Count)
For i = 1 To rngPasted.Columns.Count
If WorksheetFunction.CountA(rngPasted.Columns(i)) = 0 Then
Results(i) = ""
Else
rngPasted.Columns(i).RemoveDuplicates Columns:=1, Header:=xlNo
LastRow = Cells(wsPasted.Rows.Count, i).End(xlUp).Row
Results(i) = Replace(Join(Application.Transpose(rngPasted.Columns(i).Resize(LastRow, 1)), vbCrLf), _
vbCrLf & vbCrLf, vbCrLf)
End If
Next i
With wsSource
.Range("A1").Resize(i - 1, 1) = Application.Transpose(Results)
.Range(.Cells(1, 2), .Cells(1, .Columns.Count)).EntireColumn.Delete
wbTemp.Close False
End With
End Sub
In my limited testing, the only situation where this might yield unwanted results is when a cell in the first column is blank, but there's other data in that row. The resulting cell would then start with a blank.
Related
I apologize for the vague title as I'm not really sure where the error is. I think I'm having some compability issues with copying the elements of an array and then manipulating that data.
This is the code I have so far:
Sub listNotCompletedTasks()
Dim cell As Range
Dim sourceRange As Range
Dim targetRange As Range
Dim notCompleted() As Variant
Dim i As Integer
Dim lastr As Integer
'define sourceRange
lastr = Range("A" & Rows.count).End(xlUp).Row
Set sourceRange = Range("A2:A" & lastr)
'notCompleted is an array with all the offset cells of the cells in sourceRange
'that don't contain a "Completed" string
i = 0
For Each cell In sourceRange.Cells
If cell.Offset(0, 2).Value <> "Completed" Then 'if the cell in column C does not contain "completed"...
ReDim Preserve notCompleted(0 To i)
notCompleted(i) = cell.Value 'add cell in column A to array
i = i + 1
End If
Next cell
'define targetRange
lastRow = Cells(Rows.count, "Z").End(xlUp).Row
Set targetRange = Range("Z1:Z" & lastRow)
'copy all elements from the array to the targetRange
For i = 0 To UBound(notCompleted)
targetRange.Offset(i, 0).Value = notCompleted(i)
Next i
End Sub
Expected output:
This works well. The problem begins with the second step:
Sub listNoDuplicatesAndNoOfInstances()
Dim sourceRangeZ As Range
Dim targetRangeB As Range
Set sourceRangeZ = Sheets("SourceData").Range("Z2")
Set targetRangeB = Sheets("TargetSheet").Range("B17")
'add all of the unique instances of a string in Z from the notCompleted() array
Do Until IsEmpty(sourceRangeZ)
If Application.WorksheetFunction.CountIf(Sheets("TargetSheet").Range("B:B"), sourceRangeZ.Value) = 0 Then
targetRangeB.Value = sourceRangeZ.Value
Set targetRangeB = targetRangeB.Offset(1, 0)
Else
End If
Set sourceRangeZ = sourceRangeZ.Offset(1, 0)
Loop
'count every instance of those strings and add the value to the respective cell to the right
Set targetRangeB = Sheets("TargetSheet").Range("C17")
Do Until IsEmpty(targetRangeB.Offset(0, -1))
targetRangeB.Formula = "=COUNTIF(SourceData!Z:Z,Z" & targetRangeB.Row & ")"
Set targetRangeB = targetRangeB.Offset(2, 0)
Loop
End Sub
The first loop (the one that adds every unique instance of the strings to column B) works. The second loop (the one that returns the number of instances of each string) does not work, only returning zeroes. The thing is, if I manually do the steps of the first subroutine (use a Pivot Table to filter out the rows I need, then copy the relevant row and paste it to column Z), and then call the second subroutine, then it actually works!
So I'm assuming the first subroutine is the culprit. A "cheap" workaround that worked for me was to copy the range in Z to another column (using sourceRange.Copy/targetRange.PasteSpecial xlPasteAll) and then call the second subroutine. What am I doing wrong, and is there a better way to solve this?
A 2D array you can copy to sheet without looping.
Sub listNotCompletedTasks()
Dim wsSource As Worksheet, arNotCompl()
Dim lastrow As Long, i As Long, n As Long
Set wsSource = ThisWorkbook.Sheets("SourceData")
With wsSource
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
ReDim arNotCompl(1 To lastrow, 1 To 1)
For i = 2 To lastrow
If .Cells(i, "C") <> "Completed" Then
n = n + 1
arNotCompl(n, 1) = .Cells(i, "A")
End If
Next
If n = 0 Then Exit Sub
'copy array to targetRange
lastrow = .Cells(.Rows.Count, "Z").End(xlUp).Row
.Cells(lastrow + 1, "Z").Resize(n) = arNotCompl
End With
End Sub
Add the formula in column C when you add the unique value to column B.
Sub listNoDuplicatesAndNoOfInstances()
Dim wsSource As Worksheet, wsTarget As Worksheet
Dim lastrow As Long, i As Long, n As Long
Dim arNotCompl(), v
Set wsSource = ThisWorkbook.Sheets("SourceData")
With wsSource
lastrow = .Cells(.Rows.Count, "Z").End(xlUp).Row
arNotCompl = .Range("Z2:Z" & lastrow).Value2
End With
Set wsTarget = ThisWorkbook.Sheets("TargetSheet")
n = 17
With wsTarget
For i = 1 To UBound(arNotCompl)
v = arNotCompl(i, 1)
If Application.WorksheetFunction.CountIf(.Range("B:B"), v) = 0 Then
.Cells(n, "B") = v
.Cells(n, "C").Formula = "=COUNTIF(SourceData!Z:Z,B" & n & ")"
n = n + 1
End If
Next
End With
End Sub
[I need some help how to count date in another workbook with VBA.
Sub countMacro()
Dim oWBWithColumn As Workbook: Set oWBWithColumn = Application.Workbooks.Open("D:\U2000\Taishan01\Dump\Taishan01_0428.xlsx")
Dim oWS As Worksheet: Set oWS = oWBWithColumn.Worksheets("CurrentAlarms20210428102131871_")
Dim intLastRow As Long: intLastRow = oWS.Cells(Rows.Count, "J").End(xlUp).Row
ThisWorkbook.Worksheets("Sheet1").Range("D2").Value = Application.WorksheetFunction.CountA(oWS.Range("J7:J" & intLastRow), "<2021")
oWBWithColumn.Close False
Set oWS = Nothing
Set oWBWithColumn = Nothing
End Sub
This is my code I want filter date <2021 and between 2 date 01-01-2021 to 04-01-2021. Please kindly help. Because I want to try is can't run.
When you will have the range converted to Date, please use the next (adapted) code:
Sub countMacro()
Dim oWBWithColumn As Workbook: Set oWBWithColumn = Application.Workbooks.Open("D:\U2000\Taishan01\Dump\Taishan01_0428.xlsx")
Dim oWS As Worksheet: Set oWS = oWBWithColumn.Worksheets("CurrentAlarms20210428102131871_")
Dim intLastRow As Long: intLastRow = oWS.cells(oWS.rows.count, "J").End(xlUp).row
Dim firstD As Long, endD As Long, rng As Range
Set rng = oWS.Range("J7:J" & intLastRow)
firstD = CLng(DateSerial(2021, 1, 1))
endD = CLng(DateSerial(2021, 4, 1))
ThisWorkbook.Worksheets("Sheet1").Range("D2").value = WorksheetFunction.CountIfs(rng, ">=" & firstD, rng, "<=" & endD)
oWBWithColumn.Close False
End Sub
Edited:
In order to convert your text looking like Date, in Date, please use the next code:
Sub TransformTextInDate()
Dim sh As Worksheet, lastR As Long, rngT As Range, arrT, ArrD, arr, i As Long
Set sh = ActiveSheet 'use here the sheet you need, or activate the one to be processed
lastR = sh.Range("J" & sh.rows.count).End(xlUp).row
Set rngT = sh.Range("J7:J" & lastR) 'use here your range to be converted in Date
arrT = rngT.value 'load the range in an array
ReDim ArrD(1 To UBound(arrT), 1 To 1) 'redim the array to keep Date
For i = 1 To UBound(arrT)
arr = Split(arrT(i, 1), "-") 'split the text by "-"
ArrD(i, 1) = CDate(left(arr(2), 2) & "/" & arr(1) & "/" & arr(0)) 'build the Date
Next i
rngT.Offset(0, 1).EntireColumn.Insert xlLeft 'insert a column to the right of the processed one
With sh.cells(rngT.row, rngT.Offset(0, 1).Column).Resize(UBound(ArrD), 1)
.Value2 = ArrD 'drop the processed array values at once
.EntireColumn.AutoFit 'fit the new column
.NumberFormat = "dd/mm/yyyy" 'format the range in the standard way
End With
MsgBox "Converted to Date..."
End Sub
The above code will insert a column and drop the processed result in it.
If the conversion is correct, you may delete the previous column, or if you like keeping it (for hours or for something else), you should adapt the first code to make it working on the new column (please, change J to K).
Please, send some feedback after testing the above codes.
I am trying to get unique values from dynamic F column and store it in an array. I am getting "Object Required error for my code while setting Selection variable to a dynamic range. Please help.
Sub UniqueFilter()
Dim tmp As String
Dim arr() As String
Dim Selection As Range
Dim lrow As Long
Dim str As String
Dim cell As Range
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("14Feb19")
sht.Activate
'Set Selection = sht.Range(sht.Cells(1, 6), sht.Cells(Rows.Count, 6).End (xlUp)).Select
lrow = shData.Range("F" & Rows.Count).End(xlUp).Row
Set Selection = sht.Range("F2:F" & lrow).Select
If Not Selection Is Nothing Then
For Each cell In Selection
If (cell <> "") And (InStr(tmp, cell) = 0) Then
tmp = tmp & cell & "|"
End If
Next cell
End If
If Len(tmp) > 0 Then tmp = Left(tmp, Len(tmp) - 1)
arr = Split(tmp, "|")
End Sub
You can achieve your goal without having to use Selection at all.
Just copy the range content and transpose it into an array:
Sub UniqueFilter()
Dim arr() As String
Dim tmp As Variant
Dim lrow As Long
Dim sht As Worksheet
Dim index As Integer
Dim count As Integer
Set sht = ThisWorkbook.Worksheets("14Feb19")
sht.Activate
lrow = sht.Range("F" & Rows.count).End(xlUp).Row
'Copying and trasposing selected Range
tmp = Application.Transpose(sht.Range("F2:F" & lrow).Value)
'Cleaning from temp array all empty values
count = 1
For index = 1 To UBound(tmp, 1) - LBound(tmp, 1) + 1
ReDim Preserve arr(1 To count)
If tmp(index) <> "" Then
arr(count) = tmp(index)
count = count + 1
End If
Next
End Sub
(special thanks to #Nathan_Sav, who helped simplifying the code)
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
I want to remove duplicates based on the text in Column I and sum the values in Column C, the data in the other columns doesn't matter.
I do not want a pivot table and I am aware they are the preferred option for this type of thing.
An example of what I'd like to achieve:
I found VBA code and tried to modify it. It doesn't delete all the lines.
Sub Sum_and_Dedupe()
With Worksheets("data")
'deal with the block of data radiating out from A1
With .Cells(1, 1).CurrentRegion
'step off the header and make one column wider
With .Resize(.Rows.Count - 1, .Columns.Count + 1).Offset(1, 0)
.Columns(.Columns.Count).Formula = "=sumifs(c:c, i:i, i2)"
.Columns(3) = .Columns(.Columns.Count).Value
.Columns(.Columns.Count).Delete
End With
'remove duplicates
.RemoveDuplicates Columns:=Array(9), Header:=xlYes
End With
.UsedRange
End With
End Sub
This should be an answer to your question.
However, code might require adaptation if the range in which you look becomes very long.
Option Explicit
Sub test()
Dim wb As Workbook
Dim ws As Worksheet
Dim LastRow As Long, LastCol As Long, a As Double, i As Long
Dim Rng As Range
Dim Cell As Variant, Estimate As Variant
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
LastRow = ws.Cells(ws.Rows.Count, "I").End(xlUp).Row
LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
Set Rng = ws.Range(ws.Cells(2, 9), ws.Cells(LastRow, 9))
For Each Cell In Rng
i = 0
a = 0
For Each Estimate In Rng
If Estimate.Value = Cell.Value Then
i = i + 1 'Count nr of intances
a = a + ws.Cells(Estimate.Row, 3).Value 'sum booking value
If i > 1 Then
ws.Rows(Estimate.Row).Delete
i = 1
LastRow = LastRow - 1
End If
End If
Next Estimate
ws.Cells(Cell.Row, 3).Value = a 'Enter sum in booked this week
Next Cell
End Sub
You'll either need to change your current sheet name to data, or change the first two lines of this code to fit your needs. sh = the data sheet that you showed us. osh = an output sheet that this code will generate. Note also if column C or I move you can update the positions easily by changing colBooked and colEstimate. If you have more than a thousand unique estimate entries then make the array number larger than 999.
Sub summariseEstimates()
Dim sh As String: sh = "data"
Dim osh As String: osh = "summary"
Dim colBooked As Integer: colBooked = 3
Dim colEstimate As Integer: colEstimate = 9
Dim myArray(999) As String
Dim shCheck As Worksheet
Dim output As Worksheet
Dim lastRow As Long
Dim a As Integer: a = 0
Dim b As Integer
Dim r As Long 'row anchor
Dim i As Integer 'sheets
'Build summary array:
With Worksheets(sh)
lastRow = .Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
For r = 2 To lastRow
If r = 2 Then 'first entry
myArray(a) = .Cells(r, colEstimate) & "," & .Cells(r, colBooked)
Else
For b = 0 To a
If VBA.LCase(VBA.Replace(.Cells(r, colEstimate), " ", "")) = VBA.LCase(VBA.Replace(VBA.Split(myArray(b), ",")(0), " ", "")) Then 'match
myArray(b) = VBA.Split(myArray(b), ",")(0) & "," & VBA.Split(myArray(b), ",")(1) + .Cells(r, colBooked)
Exit For
End If
Next b
If b = a + 1 Then 'completed loop = no match, create new array item:
a = a + 1
myArray(a) = .Cells(r, colEstimate) & "," & .Cells(r, colBooked)
End If
End If
Next r
End With
'Create summary sheet:
On Error Resume Next
Set shCheck = Worksheets(osh)
If Err.Number <> 0 Then
On Error GoTo 0
Set output = Worksheets.Add(After:=Worksheets(sh))
output.Name = osh
Err.Clear
Else
On Error GoTo 0
If MsgBox("*" & osh & "* sheet already exists. Proceed to delete and recreate?", vbOKCancel, "Summary") = vbCancel Then
Exit Sub
Else
Application.DisplayAlerts = False
Worksheets(osh).Delete
Set output = Worksheets.Add(After:=Worksheets(sh))
output.Name = osh
End If
End If
'Output to summary sheet:
With Worksheets(osh)
.Cells(1, 1).Value = "ESTIMATE"
.Cells(1, 2).Value = "BOOKED THIS WEEK"
For b = 0 To a
.Cells(b + 2, 1).Value = VBA.Split(myArray(b), ",")(0)
.Cells(b + 2, 2).Value = VBA.Split(myArray(b), ",")(1)
Next b
.Columns("A:B").AutoFit
End With
End Sub