Resume duplicates (variables) in to one cell - excel

In the image I resume a little bit what I need, the names will be different but repetitive, so I need to resume in a second cell the names.
Screenshot

I am not sure if I got your question right, but if you want unique values from a column,
try copying the data to a new column and then data --> data tools --> remove duplicates

Found a solution with VBA, assuming columns are A and B
Sub findDuplicates()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).row
Sheets("Viajes").Range("A3:A" & lastrow).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Sheets("Viajes").Range("B3"), _
Unique:=True
End Sub

Related

Copying a column from one workbook to another, finding the first empty column in the target worksheet

I have a workbook that tracks long-term trends in experimental data. Independant workbooks for various experiments generate 2-3 columns of data that need to be copied to this tracking workbook. I would probably go for something like this:
Workbooks(source_book).Worksheets(source_sheet).Range(Source_Range_Variable).Copy
Workbooks(target_book).Worksheets(target_sheet).Range(Target_Range_Variable).PasteSpecial xlPasteValues
My issue is that I have no idea how to find "Target_Range_Variable" which would be the first empty column in the target sheet. I have some ideas on how to set "Source_Range_Variable" because it can be one or two columns by using nested if's to find if columns are populated and going from there. Inelegant for sure.
Sorry I don't have any real code, but I truly don't know how to start. There are several hundred columns already, and there will be several hundred more. If it wasn't so big, I would brute force my way by nesting if statements until it finds an empty column.
Note: I'm very inexperienced with this, forgive me if I miss anything obvious.
You can use Cells.Find method to find the last column. Here is an example (Untested)
Sub Sample()
'
'~~> Rest of code
'
Workbooks(source_book).Worksheets(source_sheet).Range(Source_Range_Variable).Copy
Dim LastCol As Long
Dim ColName As String
'~~> Get the last column number
LastCol = LastColumn(Workbooks(target_book).Worksheets(target_sheet))
'~~> Column number to column letter
ColName = Split(Cells(, LastCol).Address, "$")(1)
'~~> Your final range. Ex "A1"
Target_Range_Variable = ColName & 1
Workbooks(target_book).Worksheets(target_sheet).Range(Target_Range_Variable).PasteSpecial xlPasteValues
End Sub
Private Function LastColumn(wks As Worksheet) As Long
If Application.WorksheetFunction.CountA(wks.Cells) <> 0 Then
LastColumn = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Else
LastColumn = 1
End If
End Function

How to replace all values in a column by a specific value in Excel VBA?

Suppose this is my initial excel sheet
I want to replace all the non-empty cells in column C with the string "Title" excluding the column header.
The output should be like this:
Thank you!
Try below sub.
Sub FillTitle()
Dim lrow As Long
Dim rng As Range
lrow = Cells(Rows.Count, "C").End(xlUp).Row 'Detect last data entry row in Column C.
For Each rng In Range("C2:C" & lrow)
If rng <> "" Then
rng = "Title"
End If
Next rng
End Sub
Range has Replace method:
Sub ReplaceAll()
With ActiveSheet
Application.Intersect(.UsedRange, .UsedRange.Offset(1), .Columns("C")).Replace What:="*", Replacement:="Tester"
' reset Find/Replace pattern to default for further use
.Cells.Find What:="", LookIn:=xlFormulas, SearchOrder:=xlRows, LookAt:=xlPart, MatchCase:=False
End With
End Sub
For small table, just run a for loop:
for row=2 to something_large
if cells(row,col)<>"" then cells(row,col)="title"
next row
for large table, your best bet is to record a macro: create a filter on the column, filter to nonblank, select all rows, then paste to them. Once you have the macro, modify for general use.
You can choose not use loop as well by using a simple code line like below which will perform the same action.
Range("C2:C" & Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeConstants).Value = "Title"

How to remove duplicate rows in a spreadsheet

In column 'M' i have hundreds of rows with multiple duplicates. I only want one record to show per duplicate when i run my macro. Below is my code and it deletes all records apart from one.
Sub DeleteRows()
With ActiveSheet
Set Rng = Range("M5:M").End(xlDown)
Rng.RemoveDuplicates Columns:=1, Header:=xlYes
End With
End Sub
It starts from M5 as this is where the data is initially. Why would it only be showing one record?
Your original attempt, Range("M5").End(xlDown), is just one cell.
Your new attempt, Range("M5:M").End(xlDown), is closer but not a valid Range reference.
Try the following:
Set Rng = Range("M5:M" & Cells(Rows.Count, "M").End(xlUp).Row)
EDIT:
If you're dealing with an entire range, you need to specify the Columns argument of Range.RemoveDuplicates, something like this:
Sub RemoveDupes()
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A5:V" & lastRow).RemoveDuplicates Columns:=Array(13), Header:=xlYes ' column M = 13
End Sub

Select only cells that contain data between 2 columns

I am looking for a method that will select only cells which contain data between TWO columns. I can't wrap my head around the logic I need to accomplish this.
I am thinking that this is my best bet, but am open to other suggestions.
Sheet1.Columns("A3:B1000").SpecialCells(xlCellTypeConstants, 23).Select
With this code I can select the range that contains a value, however it doesn't work simultaneously between the two columns. If column A has data but column B does not, it will still select column A.
Below is what I am looking to do.
The following code will do what you expect by filtering any blank cells and then selecting all visible cells, for my example, I used columns A & B, amend this as required.
NOTE: I agree with comments from CallumDA, you would usually want to avoid selecting anything, but yet the example code below will show you how to add that given range to a variable, should you want to do something with it, rather than just select it.
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
Dim rng As Range
ws.Range("$A$1:$B$" & Lastrow).AutoFilter Field:=1, Criteria1:="<>"
ws.Range("$A$1:$B$" & Lastrow).AutoFilter Field:=2, Criteria1:="<>"
Set rng = ws.Range("A2:B" & Lastrow).SpecialCells(xlCellTypeVisible)
ws.Range("$A$1:$B$" & Lastrow).AutoFilter
rng.Select
End Sub

Adanced Filter VBA Issue

I have a large table of data and my goal is to advanced filter that table by Column B from sheet 2.
I defined my variables as ws0 (where the data I am filtering is) is my Sheet1 and ws02 is my Sheet2 (where the filter criteria is). It's just not working, I can get it to work if i specify the exact ranges but I want this to find the last row on sheet two as that range of data will change. Here is my code: I would also love to be able to find the last from from ws0 as well..but one step at a time :)
Dim LastRow As Long
With ws02
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
ws0.Range("A1:I3000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
ws02.Range("B1").LastRow, Unique:=False
End Sub
If anyone can help I'd greatly appreciate it!!
Here is Sheet 1
Here is Sheet 2
Try changing
CriteriaRange:= _
ws02.Range("B1").LastRow
To
CriteriaRange:= _
ws02.Range("B2:B" & LastRow)
You are attempting to use LastRow as a property or a method of the Range object, but no such property or method exists.
This will use the criteria from cell B2 to the last row in column B.

Resources