Removing duplicate lines of text but have exceptions - Excel - excel

I have some lines of text that look as below. As you can see there is a duplicate of "x1". What I want to do in Excel is remove duplicates (eg. x1) but keep the word "Title" as it counts as a duplicate and it will be removed which is something I don't want.
Title
x1
Title
x2
Title
x3
Title
x1
Title
x4
I searched all over the internet about methods some of them including "data validation" or even formulas like the one below but none of these worked. So do you have any ideas on how can this be done?
=IFERROR(LOOKUP(2, 1/((COUNTIF($A$2:$A$20, $A$2:$A$20)>1)*(COUNTIF($E$1:E1, $A$2:$A$20)=0)*(COUNTIF($C$2:$C$3, $A$2:$A$20)<>1)), $A$2:$A$20), "")

There are a few ways to deal with this I suppose but if the "Title" is always the every other row, you could simply use a do loop with a "+ 2" to determine the dups. So for example, let's assume all this data is in column A starting in row 1:
x = 2
Range("C1").Formula = "=Count(A:A)"
y = range("C1")
Cells(x,2).Formula = "=Countifs(A:A, A1)"
Range(Cells(x,2)).Copy
Range(Cells(x + 1, 2), cells(y, 2)).PasteSpecial xlPasteFormulas
Do While x <= y
If Cells(x, 2) > 1 Then
Rows(x).Delete
x = x + 1 'Add 1 since you already deleted 1 row and only need to move to the next row at this point.
Else
x = x + 2 'Add 2 to ensure you don't delete the title row
End If
Loop

I realized after searching a little further that there is no easy way to do this in Excel. And in some cases it is impossible. So I found a workaround with Sublime text which is much easier to do this.
Copy-paste the content from column A to Sublime text.
Then select the repeated content that you don't want to be removed and then ALT + F3. This will select all instances of that content.(HINT: Add one "space" before that text to avoid line merging when proceeding to deleting duplicates.
Inverse the selection to now select all the rest text/content.
Then go Edit > Permute Lines > Unique....
5 . DONE!!

Related

How to add 2 jpeg pictures from excel in the same cell of a table in word document?

I am trying to add few jpeg pictures (up to 6) from excel worksheet in a table (word document), but only one appears at the end. Each time I am adding a picture it goes over the previous one. Here a part of my code with the issue:
' Filling the table
For i = 1 To iNumChem
' Column 1
wdTable.Rows(i + 1).Cells(1).Range.Text = Sheet1.Cells(a + 1 + 2 * i - 2, 5).Value
' Column 2
wdTable.Rows(i + 1).Cells(2).Range.Text = Sheet1.Cells(a + 1 + 2 * i - 2, 31).Value
' Column 3
For p = 0 To 5
If Sheet1.Cells(a + 2 * i, 5 + 2 * p).Value <> 0 Then
Sheet3.Shapes(Sheet1.Cells(a + 2 * i, 5 + 2 * p).Value).Copy
wdTable.Rows(i + 1).Cells(3).Range.PasteSpecial
End If
Next p
' Column 4
Next i
I tried to work with the properties ParagraphFormat and Move, but it didn't help.
I usually find difficult to move "the cursor" to the right position to be able to add something, especially in this case with Pictures (not Shapes) to add side by side.
Any ideas/comments are welcome.
Note: Edited after comments as I mixed the terms shape and picture!
I am trying to take pictures from Excel and add them in the same cell of a table word.
When you paste the shape it won't be in the cell, it will be anchored to it but float above it. This is just the same as in Excel where the shape floats above the worksheet and hides the cells below it.
To have the shape in the table you'll need to set the wrap type to inline, otherwise the shapes will stack up on top of each other.
With wdTable.Rows(i + 1).Cells(3).Range
.PasteSpecial
.ShapeRange(1).WrapFormat.Type = wdWrapInline
End With
EDIT:
Pictures are pasted into Word as InlineShapes. They do not stack up one on top of the other. If you attempt to paste more than one into wdTable.Rows(i + 1).Cells(3).Range each one will overwrite the last. Instead you need to declare a variable outside the loop, something like wdCellRange as Word.Range, and then use it when inserting the pictures, e.g.
Set wdCellRange = wdTable.Rows(i + 1).Cells(3).Range
With wdCellRange
.Collapse Direction:= wdCollapseEnd
.PasteSpecial
End With
You could try:
...Range.Collapse Direction:= wdCollapseEnd

How do I check if a cell is blank in VBA

I am trying to go through an array and any element with a value I want to print in a list going down. However, I am having problems with identifying blank cells. Right now it recognizes every cell as not be empty. I am also including a screenshot of the data to help. (Some of the columns are hidden in the screen shot)
I have tried:
Evaluate("isblank(" & Cells(i, 8).Address & ")")
IsEmpty(list(i, 1))
IsNull(list(i, 1))
None of the options above work and this is what I currently have
Dim list As Variant
Dim i As Integer
Dim Y As String
Dim X As Integer
Y = "A"
X = 2
list = Range("H12:H158").Value
For i = 1 To 147
If Not IsEmpty(list(i, 1)) Then
Cells(X, Y) = list(i, 1)
X = X + 1
End If
Next i
Any advice is greatly appreciated
I have just tried out your macro and as a result, in the column "A", starting from row 2, I have the values:
HRT-2-11
HRT-2-13
HRT-2-14
HRT-2-15
HRT-2-17
HRT-2-18
This, I believe, is what you are looking for, isn't it? I believe that there has been a value in the cells which appear blank, but now there should be some invisible character (like a space or something). I'd advise you to check the value of such an "empty" cell and go on from there (the "immediate" window is perfect for investigations like this).
Len() never missed a char.
If Len(list(i, 1))>0 Then
....

Excel VB: how do I call VLOOKUP if condition is met?

I've got a data set that comes out of Oracle, but a number of the records are missing the GEO data (AMER, EMEA, APAC) in column E and instead show "-". I am formatting and processing this data using VB and I have everything working save for this one piece.
What I want to do is to call a VLOOKUP formula to replace those "-" values with the correct GEO based on the country name shown in column J. All records with GEOs already present should be bypassed. The code I built for this overwrites every record, unfortunately:
For z = 1 To LR
x = Application.VLookup(Cells(z, "J"), Sheets("MasterTerritoryList").Columns("A:F"), 6, False)
If Not (IsError(x)) Then
Cells(z, "E").Value = x
End If
Next z
Can someone help me out by showing me how to read the data in column E and replace only those values that equal "-"?
Many thanks!!
For z = 1 To LR
If Cells(z, "E").text = "-" Then ' <-------- Add this test
X = Application.VLookup(Cells(z, "J"), Sheets("MasterTerritoryList").Columns("A:F"), 6, False)
If Not (IsError(X)) Then Cells(z, "E").Value = X
End If
Next z

changes of colums randomly of excel file randomly using libra office

I have an excel file consist of 1000x3 words. I wrote 5 rows here as an example.
Target choice1 choice2 choice3
booklet criquet nutmeg luciole
argoman border phobie liom
mouche libellule taplet luciole
abrot guêpe terrier greeca
However I know currently all the words in choice2 are correct answer, I want to randomly mix the place of choice 1 and choice 2 and choice 3 using libra office such as below:
Target choice1 choice2 choice3
booklet nutmeg criquet luciole
argoman phobie liom border
mouche taplet libellule luciole
abrot terrier guêpe greeca
I am new to excel and I don't know how to work with it. Please advice me how to do it and in advance I appreciate all the comments.
Sorry for the late reply.
You can use something like this:
Sub MixTheValues()
Dim x, y, r
Dim Words As New Collection
For x = 2 To Cells(Rows.CountLarge, "B").End(xlUp).Row
For y = 2 To 4
Words.Add (Cells(x, y).Value)
Next y
For y = 2 To 4
r = Int(Words.Count * Rnd + 1)
Cells(x, y).Value = Words(r)
Words.Remove (r)
Next y
Next x
End Sub
x starts on row 2 and goes to the last row.
y defines the columns you want to use (in this case, 2 to 4 or B to D
Note that this shuffles the answers, but because it is completely random, some of them may not move at all.
Results:

Split Cell by Numbers Within Cell

I have some fields that need to be split up into different cells. They are in the following format:
Numbers on Mission 21 0 21
Numbers on Mission 5 1 6
The desired output would be 4 separate cells. The first would contain the words in the string "Numbers on Mission" and the subsequent cells would have each number, which is determined by a space. So for the first example the numbers to extract would be 21, 0, 21. Each would be in its own cell next to the string value. And for the second: 5, 1, 6.
I tried using a split function but wasn't sure how to target the numbers specifically, and to identify the numbers based on the spaces separating them.
Pertinent to your first case (Numbers on Mission), the simple solution could be as shown below:
Sub SplitCells()
Const RowHeader As String = "Numbers on Mission"
Dim ArrNum As Variant
ArrNum = Split(Replace(Range("A1"), RowHeader, ""), " ")
For i = 1 To UBound(ArrNum)
Cells(1, i + 2) = ArrNum(i)
Next
Cells(1, 2) = RowHeader
End Sub
The same logic is applicable to your second case. Hope this may help.
Unless I'm overlooking something, you may not need VBA at all. Have you tried the "Text to Columns" option? If you select the cell(s) with the information you would like to split up, and go to Data -> Text to Columns. There, you can choose "delimited" and choose a space as a delimiter, which will split your data into multiple cells, split by where the space is.
edit: Just realized that will also split up your string. In that case, when you are in 3rd part of the Text to Columns, choose a destaination cell that isn't the cell with your data. (I.E. if your data is in A1, choose B1 as destination, and it'll put the split info there. Then just combine the text columns with something like =B1&" "&C1&" "&D1)
I was able to properly split the values using the following:
If i.Value Like "*on Mission*" Then
x = Split(i, " ")
For y = 0 To UBound(x)
i.Offset(0, y + 1).Value = x(y)
Next y
End If

Resources