VBA excel Concatenation Headers With column A Values - excel

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

Related

code compare two lists duplicated data and copy to another list

i would help me about my code it compares from sheet1 two columns a,b and the duplicated transfer to sheet2 the column c
Sub COPY1()
Dim i
Dim LastRow As Long
LastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("sheet1").Cells(i, "A").Value = Sheets("sheet1").Cells(i, "B").Value Then
Count = Application.WorksheetFunction.CountIf(Range("B1:B" & i), Sheets("sheet1").Cells(i, "A"))
If Count > 1 Then
Sheets("sheet1").Cells(i, "A").COPY Destination:=Sheets("sheet2").Range("B" &
Rows.Count).End(xlUp).Offset(1)
End If
End If
Next i
End Sub
Give this a try:
Sub COPY1()
Dim i As Long
Dim LastRow As Long
With Sheets("sheet1")
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
v = .Cells(i, "A").Value
For j = 2 To LastRow
If v = .Cells(j, "B").Value Then
.Cells(i, "A").Copy Destination:=Sheets("sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1)
End If
Next j
Next i
End With
End Sub

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

if and concetenate if columns have X

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

Copy data with in sheets

enter image description hereThere are 2 sheets, Sheet1 and Sheet2.
Sheet1 contain 10 columns and 5 rows with data including blank.
The requirement is to copy the data from Sheet 1 and to put in another sheet Sheet 2, wherein only populate the cell which is not blank.
I get the run time error 1004 - Application or object defined error.
The code snippet is:-
Set wsht1 = ThisWorkbook.Worksheets("Sheet1")
Set wsht2 = Sheets("Sheet2")
finalrow = wsht1.Cells(wsht1.Rows.Count, 1).End(xlUp).Row
For i = 1 To finalrow
If wsht1.Cells(i, 1).Value <> " " Then
Range(Cells(i, 2), Cells(i, 2)).Copy
Worksheets("Sheet2").Select
wsht2.Range(Cells(1, i)).PasteSpecial Paste:=xlPasteFormats
End If
Next i
Can u help me in sorting this out?
You cannot define a range like that:
wsht2.Range(Cells(1, i))
you might use:
wsht2.Cells(1, i).PasteSpecial Paste:=xlPasteFormats
BTW: with this code you won't find empty cells:
If wsht1.Cells(i, 1).Value <> " " Then
you should use:
If wsht1.Cells(i, 1).Value <> "" Then
(the difference is a missing space between the quotes)
if you want to copy the values only and to make it with a loop I'd do the following:
Sub copying()
Set wsht1 = ThisWorkbook.Worksheets("Sheet1")
Set wsht2 = Sheets("Sheet2")
finalrow = wsht1.Cells(wsht1.Rows.Count, 1).End(xlUp).Row
For i = 1 To finalrow
If wsht1.Cells(i, 1).Value <> "" Then
For j = 1 To 5
wsht2.Cells(i, j).Value = wsht1.Cells(i, j).Value
Next j
End If
Next i
End Sub
If you only have 5 cells with data in Sheet 1 and only want those 5 rows copying to Sheet 2 use the following, similar to Shai's answer above with an extra counter for the rows in Sheet 2.
Sub copying()
Set wsht1 = ThisWorkbook.Worksheets("Sheet1")
Set wsht2 = Sheets("Sheet2")
finalrow = wsht1.Cells(wsht1.Rows.Count, 1).End(xlUp).Row
k = 1
For i = 1 To finalrow
If wsht1.Cells(i, 1).Value <> "" Then
For j = 1 To 5
wsht2.Cells(k, j).Value = wsht1.Cells(i, j).Value
Next j
k = k + 1
End If
Next i
End Sub
EDIT
As per your comment if you want to dynamically change j replace For j = 1 To 5 with
For j = 1 To wsht1.Cells(i, Columns.Count).End(xlToLeft).Column
The code below will copy only values in Column A (non-empty cells) from Sheet 1 to Sheet2:
Dim j As Long
Set wsht1 = ThisWorkbook.Worksheets("Sheet1")
Set wsht2 = Sheets("Sheet2")
finalrow = wsht1.Cells(wsht1.Rows.Count, 1).End(xlUp).Row
j = 1
For i = 1 To finalrow
With wsht1
' if you compare to empty string, you need to remove the space inside the quotes
If .Cells(i, 1).Value <> "" And .Cells(i, 1).Value <> " " Then
.Cells(i, 1).Copy ' since you are copying a single cell, there's no need to use a Range
wsht2.Range("A" & j).PasteSpecial Paste:=xlPasteValues, Paste:=xlPasteFormats
j = j + 1
End If
End With
Next i

Not able to operate on dynamic non empty cells

i have to sort data from sheet1 to sheet2 with reference to non-empty cell in column A. and
i have written code for it as below:
Sub polo()
Dim lastrow As Long
lastrow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
j = 2
For i = 1 To lastrow
If Sheets("Sheet1").Cells(i, 1).Value = "" Then
Sheets("Sheet2").Cells(j, 2).Value = Sheets("Sheet1").Cells(i, 1).Offset(, 1).Value
Sheets("Sheet2").Cells(j, 4).Value = Sheets("Sheet1").Cells(i - 1, 2).Value
j = j + 1
End If
Next i
End Sub
But the problem is, i am getting result as in column D of sheet2.
I want result as shown in column E.
Please help.
Try this version:
Sub polo()
Dim lastrow As Long
Dim sTemp as String
lastrow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
j = 2
For i = 1 To lastrow
If Sheets("Sheet1").Cells(i, 1).Value = "" Then
Sheets("Sheet2").Cells(j, 2).Value = Sheets("Sheet1").Cells(i, 1).Offset(, 1).Value
Sheets("Sheet2").Cells(j, 4).Value = stemp
j = j + 1
Else
stemp = Sheets("Sheet1").Cells(i, 2).Value
End If
Next i
End Sub

Resources