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
Related
'Private Sub CommandButton1_Click()
Dim MySheetu As String, ws As Worksheet
MySheetu = operationplan.Value
Set ws = Worksheets(MySheetu)
Dim a As Variant
Dim b As Long
Dim c As Variant
Dim d As Variant
Dim e As Variant
Dim str1 As String
Dim str2 As String
Dim lastrow As Long
Dim lastcol As Long
Dim lastrow2 As Long
Dim lastrow3 As Long
Dim i As Variant
Dim j As Variant
Dim k As Variant
Dim dsheet As Worksheet
lastrow = Worksheets(1).Cells(Rows.Count, 21).End(xlUp).Row
For a = 2 To lastrow
ws.Cells((a), 1).Value = Worksheets(1).Cells(a, 21).Value
ws.Cells((a), 2).Value = Worksheets(1).Cells(a, 22).Value
ws.Cells((a), 3).Value = Worksheets(1).Cells(a, 23).Value
ws.Cells((a), 4).Value = Worksheets(1).Cells(a, 20).Value
ws.Cells((a), 5).Value = Worksheets(1).Cells(a, 26).Value
ws.Cells((a), 6).Value = Worksheets(1).Cells(a, 1).Value
ws.Cells((a), 7).Value = Worksheets(1).Cells(a, 1).Value
Next a
ws.Cells(1, 1).Value = "operasyon"
ws.Cells(1, 2).Value = "öncelik"
ws.Cells(1, 3).Value = "ardillik"
ws.Cells(1, 4).Value = "operasyon süresi"
ws.Cells(1, 5).Value = "sabit istasyon"
ws.Cells(1, 6).Value = "distinct_istasyon"
ws.Cells(1, 7).Value = "istasyon"
Columns(6).RemoveDuplicates Columns:=Array(1)
For b = lastrow To 1 Step -1
If Cells(b, 1).Text = "#N/A" Then
Rows(b).Delete
End If
Next b
lastrow2 = ws.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow2
For j = 2 To lastrow2
If ws.Cells(i, 1).Value = ws.Cells(j, 2).Value Then
ws.Cells((j), 2) = i - 1
End If
Next j
Next i'
I got mismatch error in "If ws.Cells(i, 1).Value = ws.Cells(j, 2).Value Then" line how can i solve this error? I add additional codes below them. İ try cstr function but it cant work? Could you please help with these additional codes. The error is in the same line and it use user form text box value to refer the sheet
'Private Sub CommandButton1_Click()
Dim MySheetu As String, ws As Worksheet
MySheetu = operationplan.Value
Set ws = Worksheets(MySheetu)
Dim a As Variant
Dim b As Long
Dim c As Variant
Dim d As Variant
Dim e As Variant
Dim str1 As String
Dim str2 As String
Dim lastrow As Long
Dim lastcol As Long
Dim lastrow2 As Long
Dim lastrow3 As Long
Dim i As Variant
Dim j As Variant
Dim k As Variant
Dim dsheet As Worksheet
lastrow = Worksheets(1).Cells(Rows.Count, 21).End(xlUp).Row
For a = 2 To lastrow
ws.Cells((a), 1).Value = Worksheets(1).Cells(a, 21).Value
ws.Cells((a), 2).Value = Worksheets(1).Cells(a, 22).Value
ws.Cells((a), 3).Value = Worksheets(1).Cells(a, 23).Value
ws.Cells((a), 4).Value = Worksheets(1).Cells(a, 20).Value
ws.Cells((a), 5).Value = Worksheets(1).Cells(a, 26).Value
ws.Cells((a), 6).Value = Worksheets(1).Cells(a, 1).Value
ws.Cells((a), 7).Value = Worksheets(1).Cells(a, 1).Value
Next a
ws.Cells(1, 1).Value = "operasyon"
ws.Cells(1, 2).Value = "öncelik"
ws.Cells(1, 3).Value = "ardillik"
ws.Cells(1, 4).Value = "operasyon süresi"
ws.Cells(1, 5).Value = "sabit istasyon"
ws.Cells(1, 6).Value = "distinct_istasyon"
ws.Cells(1, 7).Value = "istasyon"
Columns(6).RemoveDuplicates Columns:=Array(1)
For b = lastrow To 1 Step -1
If Cells(b, 1).Text = "#N/A" Then
Rows(b).Delete
End If
Next b
lastrow2 = ws.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow2
For j = 2 To lastrow2
If ws.Cells(i, 1).Value = ws.Cells(j, 2).Value Then
ws.Cells((j), 2) = i - 1
End If
Next j
Next i'
I got type mismatch error in the last fifth row that is "If ws.Cells(i, 1).Value = ws.Cells(j, 2).Value Then" How can I solve this.It is try to refer worksheet by using user form txt box value. In debugging it can be seen that I can get text box value but worksheet give type mismatch error.
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
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
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