If with copy paste in last row - excel

In my excel sheet i want to copy the data from last row/same row of column E K M AF AI, if a condition is met in column AF =Concrete and paste it another sheet in different location
Dim FinalRow As Long
LR = lastrow
LR = Worksheets("MASTER LOG-1").Range("AF" & Rows.Count).End(xlUp).Row
If LR = "concrete" Then
Worksheets
Dim FinalRow As Long
LR = lastrow
LR = Worksheets("MASTER LOG-1").Range("AF" & Rows.Count).End(xlUp).Row
If LR = "concrete" Then
Worksheets
if a condition is met in column AF =Concrete then copy data from same row of match in column E ,K,M,AF,AI

This should help you well underway.
Sub Copy_Concrete()
Dim lastr As Integer
lastr = Worksheets("MASTER LOG-1").Range("AF" & Rows.Count).End(xlUp).Row 'determine last row of column AF
If Worksheets("MASTER LOG-1").Range("AF" & lastr) = "Concrete" Then 'If lastrow of AF = concrete then
Worksheets("Some other sheet").Range("Your range").Value = Worksheets("MASTER LOG-1").Range("Whichevercolumn", lastr).Value 'Set whichever range on another sheet to the value on the same row whichever column of your match.
End If
End Sub

Related

VBA copy contents of a row and multiply it based on a cell value

I have the following code
Sub copy()
Dim rngSource As Range
Dim copyCount As Long
With Sheets("Sheet1").Range("H2")
copyCount = .Value
Set rngSource = .EntireRow.Range("C1:F1")
End With
With Sheets("Sheet2").Range("A2")
.Resize(copyCount, rngSource.Columns.Count).Value = rngSource.Value
End With
End Sub
What it does: It copies the contents of the first row from C1 to F1 on sheet2 and multiplies the number of rows based on the value that H2 has. So if the cell H2 has the number 4, it takes all the cells from Sheet1 starting from C1 to F1 and makes 4 rows in Sheet2 with that.
Now I want it to do the same thing for each row that Sheet1 has as at the moment it only does the operation for 1 row.
I think the For Each loop would be required but I have tried several times and it failed me.
Any help is well received. Thank you!
Try the next code, please. I hope I correctly understood your need. If not, please better describe it...
Sub copySpecial()
Dim sh1 As Worksheet, sh2 As Worksheet, lastRow1 As Long, lastRow2 As Long, arrC As Variant
Dim copyCount As Long, i As Long
Set sh1 = Sheets("Sheet1"): Set sh2 = Sheets("Sheet2")
lastRow1 = sh1.Range("C" & Rows.count).End(xlUp).row
For i = 2 To lastRow1
copyCount = sh1.Range("H" & i).Value
arrC = sh1.Range("C" & i & ":F" & i)
lastRow2 = sh2.Range("A" & Rows.count).End(xlUp).row + 1
If lastRow2 < 14 Then lastRow2 = 14
If copyCount > 0 Then
sh2.Range("A" & lastRow2).Resize(copyCount, 4).Value = arrC
End If
Next i
End Sub

Copy a column of data from the second row until the last row with data in it and paste as the last row in a column on another sheet VBA

I am working on a VBA code to copy data from the second cell "A2" down to the last cell with data in it (this range will change). I then want to paste that data starting at the last row of column A on another sheet.
My VBA code as of now seems to do nothing. Can someone help me figure out why?
Option Explicit
Public Sub AddNotes()
Dim lastRow1 As String
Dim lastRow2 As String
lastRow1 = Sheets("PropertiesToMatch").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("PropertiesToMatch").Range("A2" & lastRow1).Copy
lastRow2 = Sheets("PropertyNotesToUpload").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("PropertyNotesToUpload").Range("A" & lastRow2).PasteSpecial Paste:=xlPasteValues
End Sub
Change
.Range("A2" & lastRow1)
to
.Range("A2:A" & lastRow1)
The first is only one cell, the second is the range you want.
Also, change
Dim lastRow1 As String
Dim lastRow2 As String
to
Dim lastRow1 As Long
Dim lastRow2 As Long
because Range.Row is a Long, not a String.
Option Explicit
Sub CopyPaste()
Dim i, j As Integer
Dim LastRow As Integer
With Sheets("PropertiesToMatch")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
j = 2
For i = LastRow To 2 Step -1
Sheets("PropertiesToMatch").Cells(i, 1).Copy
Sheets("PropertyNotesToUpload").Cells(j, 1).PasteSpecial Paste:=xlPasteValues
j = j + 1
Next i
End Sub

Displaying merged cell data in a For loop

I'm trying to display the contents of a merged cell in a For loop in Excel using VBA.
I have the a worksheet with very simple data in it
Here is my code:
'finding last record in my initial list
sheet_last_row = Sheets("mylist").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To sheet_last_row
last_row = Sheets("results").Cells(Rows.Count, 1).End(xlUp).Row
If Sheets("mylist").Cells(i, 1).Value = 2 Then
'test if cell is merged
If Sheets("mylist").Cells(i, 2).MergeCells Then
RowCount = Sheets("mylist").Cells(i, 2).Value
End If
Sheets("mylist").Cells(i, 1).EntireRow.Copy Sheets("results").Cells(last_row + 1, 1)
End If
Next i
I'm getting the following result with this code;
I'm new at this. Can anyone show me how to make this work.
You could try:
Option Explicit
Sub test()
Dim LastRowA As Long, LastRowB, LastRowC As Long, LastRowE As Long, MaxRow As Long
Dim cell As Range, rng As Range
With ThisWorkbook.Worksheets("Sheet1")
'Find the lastrow for all the available columns
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
LastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row
LastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row
'Get the longer last row in order to avoid losing data if the last cell of a column is merge or empty
MaxRow = WorksheetFunction.Max(LastRowA, LastRowB, LastRowC)
'Set the area to loop
Set rng = .Range("A2:C" & MaxRow)
'Start looping
For Each cell In rng
'If the cell is merger
If cell.MergeCells Then
'Find the last row of column E
LastRowE = .Cells(.Rows.Count, "E").End(xlUp).Row
'Paste cell value in column E
.Range("E" & LastRowE + 1).Value = cell.Value
'Paste cell address in column F
.Range("F" & LastRowE + 1).Value = cell.Address
End If
Next
End With
End Sub
Results:

Copy specific cells from sheet to sheet based on condition

'Sub CopyRowToSheet23()
Worksheets("Sheet2").Range("A2:E1000").Clear
Dim LastRowSheet1, LastRowSheet2 As Long
Dim i As Long
Application.ScreenUpdating = False
LastRowSheet2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A2:E" & LastRowSheet2).ClearContents
LastRowSheet1 = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
With Worksheets("Sheet1")
For i = 2 To LastRowSheet1 Step 1
If Cells(i, "E").Value = "YES" Then
LastRowSheet2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Rows(i).Copy Worksheets("Sheet2").Range("A" & LastRowSheet2 + 1)
End If
Next i
End With
Application.ScreenUpdating = True
Sheet3.Select
End Sub'
I´ve managed to create the code above to get all rows that have "yes" in column E. However, I´m having issues when trying to run the macro in other sheets different than Sheet1. I would like to run it in sheet3 but I haven´t found why it does not help.
Try:
Option Explicit
Sub test()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim wsRE As Long, i As Long, LastrowC As Long, LastrowE As Long, LastrowF As Long
'Set ws1
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
'Set ws2
Set ws2 = ThisWorkbook.Worksheets("Sheet2")
wsRE = ws2.Cells(ws2.Rows.Count, "E").End(xlUp).Row
'Starting from Row 2 - let us assume that their is a header
For i = 2 To wsRE
'Check if the value in column E is yes
If ws2.Range("E" & i).Value = "Yes" Then
'Find the Last row in Sheet1 Column C
LastrowC = ws1.Cells(ws1.Rows.Count, "C").End(xlUp).Row
'Copy row i, Column A from Sheet 1 and paste it in Sheet 2 after the lastrow in column C
ws2.Range("A" & i).Copy ws1.Cells(LastrowC + 1, 3)
'Find the Last row in Sheet1 Column E
LastrowE = ws1.Cells(ws1.Rows.Count, "E").End(xlUp).Row
'Copy row i, Column B from Sheet 1 and paste it in Sheet 2 after the lastrow in column E
ws2.Range("B" & i).Copy ws1.Cells(LastrowE + 1, 5)
'Find the Last row in Sheet1 Column F
LastrowF = ws1.Cells(ws1.Rows.Count, "F").End(xlUp).Row
'Copy row i ,Column C from Sheet 1 and paste it in Sheet 2 after the lastrow in column F
ws2.Range("C" & i).Copy ws1.Cells(LastrowF + 1, 6)
End If
Next i
End Sub

Highlight differences in same column for a group in pivot table

I have created a pivot table with Column A and Column B.
Column A has one to many relation with Column B.
I want to highlight Column A, if there is a difference in Column B values in its scope. For example,
A B
ABC 10
10
XYZ 20
25
In this case, XYZ has 2 different values in column B. I want to highlight this. How can this be done in excel?
Cheers!!
Here is how you can do it with VBA
Private Sub CommandButton8_Click()
Dim WS As Excel.Worksheet
Dim lRow As Long
Dim lastRow As Long
Dim ValueB As String
Dim lRowA As Long
Set WS = Application.Sheets("Sheet1")
lRow = 1
'Get the last row that has data in columnB
lastRow = WS.Cells(WS.Rows.Count, "B").End(xlUp).Row
'Loop through all the rows
Do While lRow <= lastRow
'If we have data in columnA, record it
If WS.Range("A" & lRow).Value <> "" Then
ValueB = WS.Range("B" & lRow).Value
'Keep track of the row that we found new columnA data on
lRowA = lRow
Else
'We don't have data in columnA, Compare what we found in
'columnB of the last row where we had data in columnA with this row
If ValueB <> WS.Range("B" & lRow).Value Then
WS.Range("A" & lRowA).Interior.Pattern = xlSolid
WS.Range("A" & lRowA).Interior.PatternColorIndex = xlAutomatic
WS.Range("A" & lRowA).Interior.Color = 255
End If
End If
lRow = lRow + 1
WS.Range("A" & lRow).Activate
Loop
End Sub

Resources