Merge rows into one based on full stop - excel

I have a table in the following format.
all that is
well known for
let be apples.
abs kdjhkj kfhksh sh
kjsfhkshgkh dh.
I want the rows to merge based on the fullstop, whenever a full stop comes, a new row should be created untill next full stop occurs.
example
all that is well known for let be apples.
abs kdjhkj kfhksh sh kjsfhkshgkh dh.
I see we can merge by n number of rows into one using inbuild tools. But I have a huge list, I cannot go around and do that for each set.
Any solution, in code or through excel or libreoffice calc will be helpful.
though I can try macro but not preferring that. Anyways if that is the only way to achieve it then why not.

I think there is no way to archive this with excel function. i try to create a code fulfill your needs:
Code:
Option Explicit
Sub test()
Dim str As String
Dim i As Long, LastRowA As Long, LastRowC As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRowA
If InStr(1, .Range("A" & i).Value, ".") > 0 Then
If str = "" Then
str = .Range("A" & i).Value
Else
str = str & " " & .Range("A" & i).Value
End If
LastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row
If LastRowC = 1 And .Range("C1").Value = "" Then
.Range("C" & LastRowC).Value = str
Else
.Range("C" & LastRowC + 1).Value = str
End If
str = ""
Else
If str = "" Then
str = .Range("A" & i).Value
Else
str = str & " " & .Range("A" & i).Value
End If
End If
Next i
End With
End Sub
Results:

If you just import it in a google drive you can do it with:
=SPLIT(CONCATENATE(A1:A5);".")
You can do it in excel by using concatenate in the same way and then also separating by "." but I do not think you can't split in that way to my knowledge. In any case, in VBA is not a difficult code. But I guess you just want to solve this and you are doing it in excel beceause you don't know how to do it in any other way.

The below code is effective compare to first answer.
Sub Program1()
Dim wb As Workbook
Dim ws As Worksheet
Dim Lastrow As Long
Dim Str1 As String
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
j = 1
For i = 1 To Lastrow
If InStr(1, ws.Cells(i, 1).Value, ".") > 0 Then
ws.Cells(j, 3).Value = Str1 & ws.Cells(i, 1).Value
j = j + 1
Str1 = ""
Else
Str1 = Str1 & ws.Cells(i, 1).Value
End If
Next
End Sub

Related

VBA Search row changed and code needs update

Below is a code that I am now using to automatically insert numbers to a cell that has todays date on Column A and the correct name on the first row of that column.
However, I can't seem to make it work if the names are in any other row than 1.
What changes do I need to make if I want it to search matches on row 2 or multiple rows?
Sub SyöttöEriVälilehti()
Application.ScreenUpdating = False
On Error GoTo M
Dim i As Long
Dim Lastrow As Long
Dim col As Long
col = 0
Dim LastColumn As Long
Dim DateLastrow As Long
Dim ans As String
Dim LString As String
Dim LArray() As String
Dim anss As String
Dim ansss As String
With Sheets("Malli2Data") ' Sheet name
DateLastrow = .Cells(Rows.Count, "A").End(xlUp).Row
Set SearchRange = .Range("A1:A" & DateLastrow).Find(Date)
If SearchRange Is Nothing Then MsgBox Date & " No matches", , "Oops!": Exit Sub
Lastrow = SearchRange.Row
LastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column
ans = InputBox("Input name and number like so: Tom,5")
LString = ans
LArray = Split(LString, ",")
anss = LArray(0)
ansss = LArray(1)
For i = 2 To LastColumn
If .Cells(1, i).Value = anss Then col = Cells(1, i).Column
Next
If col = 0 Then MsgBox anss & " No matches": Exit Sub
.Cells(Lastrow, col).Value = ansss
End With
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "Error" & vbNewLine _
& "Check input" & _
vbNewLine & "You typedt: " & ans & vbNewLine & "Correct input type: " & vbNewLine & "Name" & ",Number" & _
vbNewLine & vbNewLine & "Try again"
End Sub
The snippet:
For i = 2 To LastColumn
If .Cells(1, i).Value = CDec(anss) Then col = Cells(1, i).Column
Next
Is searching row 1 for your name
If you want to change it and make it a variable, something like
For i = 2 To LastColumn
If .Cells(xRow, i).Value = CDec(anss) Then col = Cells(1, i).Column
Next
With xRow being your defined row to search will work.
At the same time, you could sub out the last bit within the loop and use
For i = 2 To LastColumn
If .Cells(xRow, i).Value = CDec(anss) Then col = i
Next
As they are the same thing.
edit 20201-04-23A: Use of CDec(anss) will convert the string (as gathered from "ans") into a decimal number - which can then be compared against the .Value taken out of the cell.

Cleaning VBA needed for Search and Replace

I am new in VBA so i just look for the code online and i modify it for my files.
I would like to know if i can put together the following 2 macros.
It looks for contract number in column AF and then changes the Customer Name (col AN) & Customer Group Name (col AP). can it be donein another way?
I would like to simplify it as later i need, based on Contract Number to change 5 more variables in 5 different columns for other customers.
Sub CorrectCustomerNameXXXX()
Dim LastRow As Long
Dim i As Long
LastRow = Range("AF1000000").End(xlUp).Row
For i = LastRow To 1 Step -1
If Range("AF" & i) = "006-0146157-001" Then
Range("AN" & i).Value = "CUSTOMER_NAME"
End If
Next
End Sub
Sub CorrectCustomerGROUPNameXXXX()
Dim LastRow As Long
Dim i As Long
LastRow = Range("AF1000000").End(xlUp).Row
For i = LastRow To 1 Step -1
If Range("AF" & i) = "006-0146157-001" Then
Range("AP" & i).Value = "CUSTOMER_GROUP"
End If
Next
End Sub
Thanks in advance for your help
Try this code, please. You should use the same iteration:
Sub CorrectWhatever()
Dim LastRow As Long, i As Long
LastRow = Range("AF" & rows.count).End(xlUp).Row
For i = 1 To LastRow
If Range("AF" & i).Value = "006-0146157-001" Then
Range("AN" & i).Value = "CUSTOMER_NAME"
Range("AP" & i).Value = "CUSTOMER_GROUP"
End If
Next
End Sub

How cut a substring?

I have a lot of string which can contain italic font. I want to copy this string with this font. In each new string I have bold word
Example:
BIG STRING:
I tried:
Public Function GetDefinition(ByVal rngText As Range) As String
Dim theCell As Range
Set theCell = rngText.Cells(1, 1)
For I = 1 To Len(theCell.Value)
If theCell.Characters(I, 1).Font.Bold = False Then
If theCell.Characters(I + 1, 1).Text = " " Then
theChar = theCell.Characters(I, 1).Text
Else
theChar = theCell.Characters(I, 1).Text
End If
Results = Results & theChar
End If
Next I
GetDefinition = Results
End Function
I think you could use this:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long, j As Long, PositionOfDot As Long
With ThisWorkbook.Worksheets("Sheet1")
'Find last row of column A
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Loop from row 1 to lastrow
For i = 1 To LastRow
'Copy paste from column A to C keeping formatting
.Range("A" & i).Copy .Range("C" & i)
'Find the position of "."
PositionOfDot = InStr(1, .Range("A" & i), ".")
'Delete characters starting from the first one up to PositionOfDot+1
.Range("C" & i).Characters(1, PositionOfDot + 1).Delete
Next i
End With
End Sub
Results:
If your bold string always ends with a dot this will do it for you:
Option Explicit
Public Function GetDefinition(ByVal rngText As Range) As String
Dim SplitBold As Variant
SplitBold = Split(rngText, ". ")
GetDefinition = Trim(SplitBold(1))
End Function

How to check for duplicate on specific column?

Im trying to detect duplicates on column("G") of my input workbook and by using lastrow of its data at column("E") to merge upwards by using & "" & after which it will delete the entireRow and this process continue until there are no more duplicates.
I tried and also look up for many codes including delete and duplicates but I am still having trouble.
Dim myCell As Range, myRow As Integer, myRange As Range, myCol As Integer, X As Integer
'Count number column
Set wsInput = Workbooks("InputB.xls").Worksheets("HC_MODULAR_BOARD_20180112")
myCol = Range(Cells(3, 7), Cells(3, 7).End(xlDown)).Count
'Loop each column to check duplicate values & highlight them.
For X = 3 To myRow
Set myRange = Range(Cells(2, X), Cells(myRow, X))
For Each myCell In myRange
If Workbooks("InputB.xls").Worksheets("HC_MODULAR_BOARD_20180112").CountIf(myRange, myCell.Value) > 1 Then
myCell.Interior.ColorIndex = 3
End If
Next
Next
' allow values at Column"E" to merge upwards and delete all duplicate and its row (missing)
I have no clue how to delete after copying data on top of the column. Someone please help.
Many Thanks,
Adrian
You could try:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long, y As Long, Counter As Long
Dim SearchValue As String, AddValue As String
With ThisWorkbook.Worksheets("Sheet1") ' Always select your worksheet name
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
Counter = 0
AddValue = ""
SearchValue = ""
For i = LastRow To 3 Step -1
SearchValue = .Range("C" & i).Value
If SearchValue <> "" Then
If Application.WorksheetFunction.CountIf(.Range("C3:C" & LastRow), SearchValue) > 1 Then
For y = i To 3 Step -1
If .Range("C" & y).Value = SearchValue Then
If AddValue = "" Then
AddValue = .Range("E" & y).Value
Else
AddValue = AddValue & ", " & .Range("E" & y).Value
.Rows(y).EntireRow.Delete
Counter = Counter + 1
End If
End If
Next y
.Range("E" & i - Counter).Value = AddValue
AddValue = ""
SearchValue = ""
Counter = 0
End If
End If
Next i
End With
End Sub

vba excel: how to copy characters from a cell to another

I'm new in VBA world and I'm learning by your suggestions.
I've looked for several solutions, tried them but they were't ideal for my problem.
Here are the links
Find a string within a cell using VBA
How to Count the Number of a Specific Character in a Cell with Excel VBA
Which command in VBA can count the number of characters in a string variable?
What I need is the possibility to check the characters inInput and match with Match list key. After matched the string, copy the characters I in output.
As you can see, first rows are simple to match but in A8 cell (for example) there is a string with fourteen characters.
In this case I need that, when there is a string that begin with CMXXAB, the match is with WT (ALWAYS!).
The same thing happen when we have A12: it begins with ETRxxx and, after match will begin in output like JAZZ.
I think this will help you:
Option Explicit
Sub test()
Dim LastrowA As Long, i As Long, AppearA As Long, AppearB As Long
Dim strA As String, strB As String
With ThisWorkbook.Worksheets("Sheet1")
LastrowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastrowA
strA = .Range("A" & i).Value
strB = .Range("C" & i).Value
'Check if strA appears in strB
AppearA = InStr(1, strB, strA)
If AppearA > 0 Then
.Range("B" & i).Value = strA
Exit For
End If
'Check if strB appears in strA
AppearB = InStr(1, strA, strB)
If AppearB > 0 Then
.Range("B" & i).Value = strB
Exit For
End If
Next i
End With
End Sub
Thank you so much for help.
after few days i've found solution for problem in my question.
infact, this is my solution and I hope to be good for helping anyone need something like this.
Sub AssociazioneRotabiliPerPivot()
Dim LastrowA, LastrowC As Long, i, j As Long, AppearA As Long, AppearB As Long
Dim strA As String, strB As String
With ThisWorkbook.Worksheets("sheet1")
LastrowA = .Cells(.Rows.count, "A").End(xlUp).Row
LastrowC = .Cells(.Rows.count, "C").End(xlUp).Row
For j = 1 To LastrowC
For i = 1 To LastrowA
strA = .Range("A" & i).Value
strB = .Range("C" & j).Value
AppearC = InStr(1, strA, strB)
If AppearB > 0 Then
.Range("B" & i).Value = strB
End If
If (InStr(1, strA, "CM") Or InStr(1, strA, "C4551R")) > 0 Then
.Range("B" & i).Value = "WT"
ElseIf InStr(1, strA, "ETR425") > 0 Then
.Range("B" & i).Value = "JAZZ"
End If
Next i
Next j
End With
End Sub

Resources