if and concetenate if columns have X - excel-formula

I have to populate a column "Country Scope" and using this formula. However, I need to populate all the country name if it's there is a X in the column (column L thru Q). Country names should be populated with semicolon in between. See the pic.
=IF(L3="X","Corporate;",IF(M3="x","Mexico;",IF(N3="x","Argentina;",IF(O3="X","Dubai;",IF(P3="X","Broken Arrow;",IF(Q="X","Brazil;"))))))
I tried following code from https://www.mrexcel.com/forum/excel-questions/553169-concatenate-row-variable-number-columns.html but receiving Type mismatch error. I replace each column's x to respected country name.
Sub ConCatFromColumnC()
Dim X As Long, LastRow As Long, LastCol As Long, Delimiter As String
Const StartRow As Long = 1
Delimiter = vbLf
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For X = StartRow To LastRow
LastCol = Cells(X, Columns.Count).End(xlToLeft).Column
If LastCol = 3 Then
Cells(X, "B").Value = Cells(X, "C").Value
Else
Cells(X, "B").Value = Join(Application.Index(Range(Cells(X, "C"), Cells(X, LastCol)).Value, 1, 0), Delimiter)
End If
Next
End Sub

Looks like the following is working now.
Sub ConCatFromColumnC()
Dim X As Long, LastRow As Long, LastCol As Long, Delimiter As String
Const StartRow As Long = 2
Delimiter = vbLf
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For X = StartRow To LastRow
'LastCol = Cells(X, Columns.Count).End(xlToLeft).Column
LastCol = 17
If LastCol = 12 Then
Cells(X, "K").Value = Cells(X, "L").Value
Else
Cells(X, "K").Value = Join(Application.Index(Range(Cells(X, "L"), Cells(X, LastCol)).Value, 1, 0), Delimiter)
End If
Next
End Sub

Related

VBA excel Concatenation Headers With column A Values

I have a table of headers and Row is a list of values. I'm trying to concatenate the whole table so the header is followed by the value in row A like this -
Correct
I have a loop that does this quite nicely however it has begun to take some time to work -
r = 2
c = 2
Do While Cells(1, r) <> ""
Do While Cells(c, 1) <> ""
Cells(c, r) = Cells(1, r) & Cells(c, 1)
c = c + 1
Loop
r = r + 1
c = 2
Loop
I've tried to use a formula instead -
Dim lngLastRow As Long
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("B2:B" & lngLastRow).Formula = "=B1 & ""_"" & A2"
But I get the following - Error
Any help would be much appreciated.
You don't need a nested loop in the code. (Unless you plan to use more columns and want to quickly expand that out without modding the code.)
Dim i As Long
Dim lr As Long
With Sheets("Sheet1")
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To lr
.Cells(i, 2).Value = .Cells(1, 2).Value & .Cells(i, 1).Value
.Cells(i, 3).Value = .Cells(1, 3).Value & .Cells(i, 1).Value
.Cells(i, 4).Value = .Cells(1, 4).Value & .Cells(i, 1).Value
Next i
End With
With dynamic columns:
Dim i As Long
Dim j As Long
Dim lr As Long
Dim lc As Long
With Sheets("Sheet1")
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
For i = 2 To lr
For j = 2 To lc
.Cells(i, j).Value = .Cells(1, j).Value & .Cells(i, 1).Value
Next j
Next i
End With
For your formula you need to set row for the access level and column for name to be absolute:
=CONCATENATE(B$1,$A2)
This will allow you to drag the formula around without messing up what it's grabbing

vba for loop for 4 different column

Dim I As Long
For I = 2 To lastrow
If Not IsEmpty(Cells(I, "f")) And IsEmpty(Cells(I, "j")) Then
Cells(I, "j").Value = "unregister"
End If
Next I
Dim I2 As Long
For I2 = 2 To lastrow
If IsEmpty(Cells(I2, "f")) Then
Cells(I2, "i").Value = Cells(I2 - 1, "i").Value
End If
Next I2
can you make this code more simple i want to copy above row for 3 different column if column f is empty
You can do something like this, using a single loop and Offset(-1, 0) to get the cell above:
Dim i As Long, ws As Worksheet
Set ws = ActiveSheet
For i = 2 To ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
With ws.Rows(i)
If Not IsEmpty(.Columns("F")) Then
If IsEmpty(.Columns("J")) Then .Columns("J").Value = "unregister"
Else
.Columns("I").Value = .Columns("I").Offset(-1, 0).Value
.Columns("L").Value = .Columns("L").Offset(-1, 0).Value
'etc
End If
End With
Next I2

Change data view in excel

I want to change the way data is shown on an excel sheet. Here are the images that describe how it looks currently
But I want to include one row per B code columns something like this:
Tried transposing etc but it did not work
Can someone help me please?
You could use this:
Option Explicit
Sub run()
Dim LastColumn As Long, LastRow As Long, LastRow2 As Long, i As Long, j As Long
Dim ServiceName As String, Route As String, B_Code As String
Dim sDate As Date
Dim Code_Value As Double
'Change sheet if needed
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
For i = 2 To LastRow
ServiceName = .Cells(i, 1).Value
sDate = .Cells(i, 2).Value
Route = .Cells(i, 3).Value
For j = 4 To LastColumn
B_Code = .Cells(1, j).Value
Code_Value = .Cells(i, j).Value
With ThisWorkbook.Worksheets("Sheet2")
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Cells(LastRow2 + 1, 1).Value = sDate
.Cells(LastRow2 + 1, 2).Value = ServiceName
.Cells(LastRow2 + 1, 3).Value = Route
.Cells(LastRow2 + 1, 4).Value = B_Code
.Cells(LastRow2 + 1, 5).Value = Code_Value
End With
Next j
Next i
End With
End Sub
The results as paste in Sheet2

Cut content to next row maintaining other content in range

I want to cut the cells present from the cells in E1:G1 and add it to D2 and copy the cells in range present in A1:C1 to the next row,
and do that to next rows and so on in which they have content from the columns E to G.
I've already tried to use the "Data - Text to Columns" in Excel but I can't use that in order to copy to rows...
What I'm trying to obtain is in this format, but I'm having a hard time finding VBA code in order to do this.
You could try:
Option Explicit
Sub test()
Dim LastRow As Long, LastColumn As Long, i As Long, y As Long
Dim Avalue As String, BValue As String, Cvalue As String
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = LastRow To 1 Step -1
Avalue = .Range("A" & i).Value
BValue = .Range("B" & i).Value
Cvalue = .Range("C" & i).Value
LastColumn = .Cells(i, .Columns.Count).End(xlToLeft).Column
If LastColumn > 4 Then
For y = LastColumn To 5 Step -1
.Rows(i + 1).EntireRow.Insert
.Cells(i + 1, 1).Value = Avalue
.Cells(i + 1, 2).Value = BValue
.Cells(i + 1, 3).Value = Cvalue
.Cells(i, y).Cut .Cells(i + 1, 4)
Next y
End If
Next i
End With
End Sub
Array Version
Option Explicit
Sub test()
Dim LastRow As Long, LastColumn As Long, i As Long, y As Long
Dim Avalue As String, BValue As String, Cvalue As String
Dim ABCvalues As Variant
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = LastRow To 1 Step -1
ABCvalues = .Range("A" & i & ":C" & i).Value
LastColumn = .Cells(i, .Columns.Count).End(xlToLeft).Column
If LastColumn > 4 Then
For y = LastColumn To 5 Step -1
.Rows(i + 1).EntireRow.Insert
.Range("A" & i + 1 & ":C" & i + 1).Value = ABCvalues
.Cells(i, y).Cut .Cells(i + 1, 4)
Next y
End If
Next i
End With
End Sub

Comparing 3 columns of two sheets ,if found matching copying value of 4th column of first sheet to second

I am trying to compare 3 columns in two sheets in a workbook and if all 3 columns are matching then copying the value of first sheet(Column G) to Column P of the second sheet.
I am trying to use the below code but it does not seem to work.(Returns blank)
Sub CopyCells()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim j As Long, i As Long, lastrow1 As Long, lastrow2 As Long
Set sh1 = Worksheets("EOD")
Set sh2 = Worksheets("Consolidated")
lastrow1 = sh1.Cells(Rows.Count, "A").End(xlUp).Row
lastrow2 = sh2.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastrow1
For j = 2 To lastrow2
If sh1.Cells(i, "A").Value = sh2.Cells(j, "B").Value And _
sh1.Cells(i, "B").Value = sh2.Cells(j, "C").Value And _
sh1.Cells(i, "C").Value = sh2.Cells(j, "D").Value Then
sh1.Cells(i, "G").Value = sh2.Cells(j, "P").Value
End If
Next j
Next i
End Sub
Thanks for your help in advance!
I've tested the following code and it does as you wanted:
Sub CopyCells()
LastRow1 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
LastRow2 = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow1
For j = 2 To LastRow2
If Sheet1.Cells(i, "A").Value = Sheet2.Cells(j, "B").Value Then
If Sheet1.Cells(i, "B").Value = Sheet2.Cells(j, "C").Value Then
If Sheet1.Cells(i, "C").Value = Sheet2.Cells(j, "D").Value Then
Sheet2.Cells(j, "P").Value = Sheet1.Cells(i, "G").Value
End If
End If
End If
Next j
Next i
End Sub

Resources