Excel: move the contents of the row to a specific column based on condition - excel

I have a large excel file with consistent columns but they're not placed too accurately;
The example in the photo is illustrative to my problem; I'm only interested in the information after the "country" column.
Therefore, within each row, I would like to
1. check each row to find a specific string of characters - in this case "Spain".
2. move the row so that the cells with the strings found are all on the same column.
In the example below, all the rows should be moved so that "Spain" is under column F - arranging the rest of the info with it.

If word Spain isn't changed, use this VBA code:
Sub MoveCoun()
Dim LastRow As Long
Dim rFind As Range
Dim r As String
Dim m As Integer
LastRow = 1000
For n = 1 To LastRow
r = n & ":" & n
Range(r).Select
With Range(r)
Set rFind = .Find(What:="Spain", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
If rFind.Column < 6 Then
m = 6 - rFind.Column
Range(Cells(n, 1), Cells(n, m)).Insert Shift:=xlToRight
ElseIf rFind.Column > 6 Then
m = rFind.Column - 6
Range(Cells(n, 1), Cells(n, m)).Delete Shift:=xlToLeft
End If
End If
End With
Next
End Sub

Related

Find and replace using VBA

I have a chart where I need to remove certain keywords from column C- Range C3:C5000(some cells are blank). The words that needs to be removed are placed in column A- Range A3:A100(some cells are blank). Both ranges gets changed for different files. I have written a code but its not working for dynamic range. Also I want to sort column c according to no. of characters in cell in Ascending order. please help
Sub Replace_Char()
Dim i As Integer
Dim Mpp As String
For i = 3 To 50
Mpp = Cells(i, 1).Value
If Cells(i, 1).Value <> 0 Then
Worksheets("Sheet1").Columns("C").Replace _
What:=Mpp, Replacement:="", _
SearchOrder:=xlByColumns, MatchCase:=True
End If
Next i
End Sub
For dynamic ranges, you can try using the .UsedRange property.
As for sorting by number of characters, create a column that has the formula like "=LEN(D1)" and then sort the sheet on that column.
Sub Replace_Char()
Dim i As Integer
Dim Mpp As String
'For i = 3 To 50
Dim Thing As Range
For Each Thing In ActiveSheet.UsedRange.Columns(1).Cells
Mpp = Cells(i, 1).Value
If Cells(i, 1).Value <> 0 Then
Worksheets("Sheet1").Columns("C").Replace _
What:=Mpp, Replacement:="", _
SearchOrder:=xlByColumns, MatchCase:=True
End If
'Next i
Next
End Sub

Merge 2 columns and find text VBA

I have one table and one file. I can find the text which is in specific place in file inside the table.
However, the texts are not unique all the time, so I decided to combine 2 cells in file and try to find in table. unless, I cannot find a way to combine 2 columns in table to match it with combined 2 cells in file.
Below you may see example table.
my aim is adding date in cell next cell of Units. So I try to find A1234 instead of 1234 due to 1234 not unique.
FindString = wb.Sheets("1").Range("E4").Value & wb.Sheets("1").Range("E5").Value
If Trim(FindString) <> "" Then
With Wb2.Sheets("Sheet1").Range("A:A") 'this section need to be amended and need combine column A&B
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Rng.Offset(0, 1).Value = wb.Sheets("1").Range("I4") ' if column A&B combining completed then next cell probably will not work
Else
MsgBox "Nothing found in the list"
End If
This is similar to the variant strategy mentioned in the comments-- Try looping through your data with a For loop and an If Statement looking for both values to match. Here's an example code that shows the concept
Sub test()
Dim s As Worksheet, findstring1 As String, findstring2 As String
Dim firstrow As Integer, lastrow As Integer, i As Integer
Set s = Sheets("test")
findstring1 = "A "'replace this with the Customer reference (what to search for)
findstring2 = "1234" 'replace this with the unit reference
firstrow = 2 ' row number for first cell with data
lastcell = s.Cells(2, 1).End(xlDown).Row 'find last cell row number (end of data)
For i = firstrow To lastcell
If s.Cells(i, 1) = findstring1 And s.Cells(i, 2) = findstring2 Then
'do something with found values
End If
Next i
End Sub

VBA Excel - Search rows for string and if found copy entire cell to a specific location

I got 3000 rows of data in Excel.
Each row contains the same type of information but not in the right order.
What i need to do is to gather the same type of information under the same column.I would like to create a macro that is going to:
Search a row for a partial string (some values have similar strings but fall under different categories)
If the string is part of a cell copy the entire cell in a
new location
Repeat for the next row
Thanks in advance
Sub MoveColumns()
Dim LastRow As Long
Dim rFind As Range
Dim r As String
Dim m As Integer
LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
MsgBox (LastRow)
For n = 1 To LastRow
r = n & ":" & n
Range(r).Select
With Range(r)
Set rFind = .Find(What:="Spain*", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
If rFind.Column < 6 Then
m = 6 - rFind.Column
Range(Cells(n, 1), Cells(n, m)).Insert Shift:=xlToRight
ElseIf rFind.Column > 6 Then
m = rFind.Column - 6
Range(Cells(n, 1), Cells(n, m)).Delete Shift:=xlToLeft
End If
End If
End With
Next
End Sub
UPDATED
If row doesn't contain any value starts with "Spain", this row is simply ignored and skipped.
I hope you can modify and customize the way suitable for your data.

How to average multiple cells in same column?

Is there a way to find the average for a block of data in the same column?
This is the code I was using before which wasn't working the way i had hoped for data with fewer numbers:
Sub Macro1()
'Macro1
Do Until ActiveCell.Value = ""
'Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.FormulaR1C1 = "=AVERAGE(R[-13]C:R[-1]C)"
Selection.End(xlDown).Select
Loop
End Sub
Example: the image is the file I'm working on. I've got thousands of rows of this data and it's all separated by three blank rows. Is there a way to find the averages of each block of data and display it in the first blank row directly underneath the data?
Try this: (for columnA considering 3 gaps after each block including last block)
Sub DoAvginColumn()
Dim i as long, q as long, avgBlock
Dim lastrow as Range, firstrow as Range, rngcolA as Range, rngBlock as Range
Set lastrow = Range("A:A").Find(What:="*", After:=[A1], SearchDirection:=xlPrevious)
Set firstrow = Range("A:A").Find(What:="*", After:=[A1], SearchDirection:=xlNext)
Set rngcolA = Range("A" & firstrow.Row & ":" & "A" & lastrow.Row)
q = 1
For i = 1 To rngcolA.Rows.Count + 1
If rngcolA(i).Value = "" And rngcolA(i + 1).Value = "" And rngcolA(i + 2).Value = "" Then
Set rngBlock = Range(rngcolA(q), rngcolA(i - 1))
avgBlock = WorksheetFunction.Average(rngBlock)
rngcolA(i).Value = avgBlock
rngcolA(i).Font.Bold = True
q = i + 3
End If
Next
End Sub
APPENDED: Steps I followed: From top to bottom of respective range:
search for 3 consecutive blank.
If found, then define an appropriate range for each block starting with 1st cell of that block and ending with last cell of that block.
Then applied avg function on that block and put result below the block.

Excel VBA: Copy from one worksheet to another

So I have a workbook with two sheets in it. I need to copy data from worksheet 2 ("Detail") to worksheet 1 ("Syncrofit"). The items from ws2 I need to paste into progressive rows on sheet 1, so rows in sheet two, column B which say "Joint1-1" need to be inserted below row 1 on sheet 1. This essentially creates a nested table.
Here's what I have so far, mostly scraped together from code and help I've found around here:
Sub SelectJoints()
Sheets("Detail").Activate
Dim Selection1 As Integer, Selection2 As Integer
Dim SelectionRange As Range
Dim num As Integer
Dim rngFind As Range
Set rngFind = Columns("B:B").Find(what:="*" & "Joint1-" & num, After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngFind Is Nothing Then
Selection1 = rngFind.Row + 1
End If
Set rngFind = Columns("B:B").Find(what:="*Joint1-" & num + 1, After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngFind Is Nothing Then
Selection2 = rngFind.Row - 1
End If
If Selection1 > 0 And Selection2 > 0 Then
Set SelectionRange = Range(Cells(Selection1, 2), Cells(Selection2, 6))
End If
End Sub
The intent here is that this should activate the detail sheet, find strings in column B which match SomeTextHere(Joint1-1) and select those rows. I then need it to paste those selections over to sheet 1 (below row 1, which has a value matching the Joint value in one of the columns), come back to sheet 2, select the rows containing SomeTextHere(Joint1-2) and paste those below the next row (after those which were just inserted). I realize that the pasting part of that is not in the code. This has been driving me nuts.
Please excuse my lack of knowledge in regards to VBA.
I'd like the finished product to look like a nested table kind of like follows:
Original Items
Copied from sheet 2
Copied from sheet 2
Copied from sheet 2
Original Item 2
Copied from sheet 2
etc.
I was a bit bored so I whipped up something that might help you. Let me know if it works for you.
Sub Macro1()
Dim i, j, x
Dim rng As Range
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = Sheets("Syncrofit")
Set sh2 = Sheets("Detail")
lr = sh2.Range("B" & Rows.Count).End(xlUp).Row
lc = sh2.Cells(2, Columns.Count).End(xlToLeft).Column
j = 2
For y = 1 To 3 ' set upper limit of first integer in Joint string
For x = 1 To 2 ' set upper limit of second integer in Joint string
For i = 2 To lr
If InStr(sh2.Cells(i, 2), "Joint" & y & "-" & x) <> 0 Then
sh2.Range(sh2.Cells(i, 1), sh2.Cells(i, lc)).Copy
sh1.Rows(j).Insert
j = j + 1
End If
Next i
Next x
Next y
End Sub

Resources