Copying only specific cells to different sheets if certain conditions are true - excel

I am working with 4 rows (test bed for my code) and each product is allocated 2 rows: Data range is A1:E5
Fizzy Drink Australia Perth no sugar High
Fizzy Drink 3 5 7 5
Still water Australia Perth flavoured High
Still water 4 7 5 4
The above is on sheet 1 and there's a sheet for each produce i.e. total 3 sheets. I'm using a For loop in column 'A' to find the product and then copy the text in each of the 4 columns to the right onto the respective product sheets in columns H1:K1. This text acts as headers for each product sheet, so the headers are not the same for each product. Each product's text must be copied to the correct product sheet.
I'm having problems copying the text attached in the first row against each product in column 'A', as the 2nd row has values. The format is same for all the products - 2 rows - first row for text and second for formulas.
The challenge (which I have failed miserably) is to make the code copy text in columns B:E for each particular product.
The text can change on often basis so if the code can identify the product in column 'A' and copy/paste the text that would be fantastic.
Option Explicit
Sub copy_Text_Formulas_to_sheets
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim Lastrow As Long
Dim Lastrow1 As Long
Dim Lastrow2 As Long
Dim i As Integer
Dim j As Integer
Set ws1 = ThisWorkbook.Worksheets("Key") 'this is the sheet I'm pulling data from
Set ws2 = ThisWorkbook.Worksheets("Fizzy Drink") 'this is the worksheet I'm pulling data into for Prd1
Set ws3 = ThisWorkbook.Worksheets("still water") 'this is the worksheet I'm pulling data into for Prd2
Lastrow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
Lastrow1 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
Lastrow2 = ws3.Cells(ws3.Rows.Count, "A").End(xlUp).Row
For i = 1 To Lastrow
For j = 1 To Lastrow
If ws1.Cells(i, "A").Value = "Fizzy Drink" And ws1.Cells(i, "B").Value = "Australia" And _
ws1.Cells(i, "C").Value = "Perth" And ws1.Cells(i, "D").Value = "no sugar" And ws1.Cells(i, "E").Value = "High" Then
ws1.Range("B" & i, "E" & i).Copy 'copy row with text from B to E including all formatting
ws2.Select
ws2.Range("H1:K1").PasteSpecial xlPasteValues
'If the above conditions are not met msg user
End If
If ws1.Cells(j, "A").Value = "Fizzy Drink" And ws1.Range(i,"B:E").HasFormlua Then
ws2.Range("B2:E2") = ws1.Range(j, "H:K") 'copy the formulas in row B:E with relative references
'If the above conditions are not met msg user
End If
If ws1.Cells(i, "A").Value = "still water" And ws1.Cells(i, "B").Value = "Australia" And _
ws1.Cells(i, "C").Value = "Perth" And ws1.Cells(i, "D").Value = "flavoured" And ws1.Cells(i, "E").Value = "High" Then
ws1.Range("B" & i, "E" & i).Copy 'copy row with text from B to E including all formatting
ws3.Select
ws3.Range("H1:K1").PasteSpecial xlPasteValues 'copy including all formatting
'If the above conditions are not met msg user
End If
If ws1.Cells(j, "A").Value = "still water" And ws1.Range(i, "B:E").HasFormlua Then
ws2.Range("B2:E2") = ws1.Range(j, "H:K") 'copy the formulas in row B:E with relative references
'If the above conditions are not met msg user
End If
Next j
Next i
On Error Resume Next
ws2.Range("B2:E2").AutoFill Destination:=Range("B2:E" & Lastrow1) 'copy formula in row to down to lastrow
ws3.Range("B2:E2").AutoFill Destination:=Range("B2:E" & Lastrow2) 'copy formula in row to down to lastrow

This should help. I didn't do anything with the header row because I don't know why you would have to change it one, let alone once for each record.
Sub copy_Text_Formulas_to_sheets1()
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim Lastrow As Long, i As Long
Dim msg as String
Set ws1 = ThisWorkbook.Worksheets("Key") 'this is the sheet I'm pulling data from
Set ws2 = ThisWorkbook.Worksheets("Fizzy Drink") 'this is the worksheet I'm pulling data into for Prd1
Set ws3 = ThisWorkbook.Worksheets("still water") 'this is the worksheet I'm pulling data into for Prd2
With ws1
Lastrow = .Cells(ws1.rowS.Count, "A").End(xlUp).Row
MsgBox "Last Row:" & Lastrow
For i = 1 To Lastrow
msg = msg & .Cells(i, "A") & vbcrlf
If IsNumeric(.Cells(i, 2)) Then
If .Cells(i, "A").value = "Fizzy Drink" Then
.Range(.Cells(i, "B"), .Cells(i, "E")).Copy getNextRow(ws2, "H")
ElseIf .Cells(i, "A").value = "Still water" Then
.Range(.Cells(i, "B"), .Cells(i, "E")).Copy getNextRow(ws3, "H")
End If
End If
Next
MsgBox "Range B2 is Numeric:" & .Cells(2, 2) & vbCrLF & "Range B3 is Numeric:" & .Cells(3, 2)
MsgBox "Range B2 has formula:" & .Cells(2, 2).HasFormula & vbCrLF & "Range B3 has formula:" & .Cells(3, 2).HasFormula
MsgBox msg
End With
End Sub
Function getNextRow(xlWorksheet As Worksheet, colmnLetter As String) As Range
Set getNextRow = xlWorksheet.Cells(rowS.Count, colmnLetter).End(xlUp).Offset(1, 0)
End Function
I added a couple of message to see what's up. Let me know what you get back. Can you provide a download link with sample data?

Related

Copy the background color from another column

In column A1:A145 need code to highlight color.
Apply format color in A1, how many rows? Base on the image1.
enter image description here
In column A , 14 rows will have yellow, next 14 rows will have blue and so on. (number will always change)
In column B need to repeat the number that appear in column D.
Looking for this result image 2
enter image description here
The below code only copy the color and number from column C at the end of row A:145, what we need is trying to highlight rows base on another cell value, working with sheet 1 until sheet 8 or more sheets.
Sub Color_My_Cells()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
Lastrowa = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To Lastrowa
Cells(Lastrow, 1).Resize(Cells(i, 2).Value) = Cells(i, 2).Value
Cells(Lastrow, 1).Resize(Cells(i, 2)).Interior.Color = Cells(i, 2).Interior.Color
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row + 1
Next
Application.ScreenUpdating = True
End Sub
#GoalExcel the code below loops through all sheets in your Excel file and repeat the same steps for each one.
Sub ColorMyCells()
Dim i, j, intRowCounterAB, intRowC As Integer
For Each ws In ThisWorkbook.Sheets
ws.Activate
intRowC = Evaluate("=COUNTA(C:C)")
intRowCounterAB = 1
For i = 1 To intRowC
For j = 0 To Range("C" & i).Value - 1
Range("A" & intRowCounterAB).Interior.Color = Range("C" & i).Interior.Color
Range("B" & intRowCounterAB).Value = Range("D" & i).Value
intRowCounterAB = intRowCounterAB + 1
Next j
Next i
Next ws
End Sub

How to loop this data in VBA?

I have a worksheet (first row headers) where the 3 last columns are as follows: Component 1(column AW), Component 2 (AX) and Number of Components (AY). Number of components value is either 1 or 2. If "Number of components" is 2 then I would like to copy the entire row and paste it twice to the last row of the same worksheet.
If "Number of components"is 1 then I would only like to copy it once.
Earlier there is also a column M that contains the article number and for each copied row I would like the value in column M to be replaced by the value from the corresponding row of column Component 1 / Component 2.
For example in this row the article number in column M is x. As "Number of components" is 2, I would like this whole row to be pasted twice to the last row of the worksheet. By the first time it is pasted, I need column M value to be replaced by 205334 (Component 1 value) and when it is pasted second time I need column M value to be replaced by 96423 (Component 2 value). Is it possible to loop this way? The worksheet has more than a 1000 rows.
I have written the following code to do the copy/paste part, however I'm having trouble to replace the value from Column M with the value from Columns Component 1 and 2.
Dim lastcol As Range
Dim lColumn As Long
Dim lRow As Long
Dim i As Long
lColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Set lastcol = Cells(2, lColumn)
For i = 2 To Rows.Count
If lastcol.Value = 2 Then
Cells(i, lColumn).EntireRow.Select
Selection.Copy Sheets("Filtersets Database (2)").Range("A" & Rows.Count).End(xlUp).Offset(1)
Cells(i, lColumn).EntireRow.Select
Selection.Copy Sheets("Filtersets Database (2)").Range("A" & Rows.Count).End(xlUp).Offset(1)
Cells(i, lColumn).EntireRow.Select
ElseIf lastcol.Value = 1 Then
Cells(i, lColumn).EntireRow.Select
Selection.Copy Sheets("Filtersets Database (2)").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
Thank you in advance!
This should do what you want.
Option Explicit
Sub CopySomeThings()
Dim ws As Worksheet
Dim rngDst As Range
Dim lastcol As Long
Dim lastrow As Long
Dim i As Long
Set ws = ActiveSheet ' Sheets("Filtersets Database (2)")
lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
lastcol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Set rngDst = ws.Range("A" & lastrow + 1)
For i = 2 To lastrow
With ws
If .Cells(i, lastcol).Value = 2 Then
.Rows(i).Copy rngDst
.Cells(rngDst.Row, "M").Value = .Cells(i, lastcol - 2)
Set rngDst = rngDst.Offset(1)
.Rows(i).Copy rngDst
.Cells(rngDst.Row, "M").Value = .Cells(i, lastcol - 1)
Set rngDst = rngDst.Offset(1)
ElseIf .Cells(i, lastcol) = 1 Then
.Rows(i).Copy rngDst
.Cells(rngDst.Row, "M").Value = .Cells(i, lastcol - 2)
Set rngDst = rngDst.Offset(1)
End If
End With
Next i
End Sub

Copy the data from one sheet and paste it on the other sheet

I need an excel vba code which copy the data from one sheet and paste it on the other sheet if the given conditions satisfied. There will be two sheets in a workbook (sheet1 and sheet 2). Basically the data in sheet 2 column "C" must be copy to sheet 1 column "C".
The conditions are : -
There will be three columns in SHEET 1&2 A,B,C .
IF SHEET 1 B1 has a data let us take("88").Now,it should search how many of them ("88") are there in sheet2 B:B.
If there are more than one let us take "4" then those "4" sheet2 "C" values are belongs to the sheet 1
"A1". It should create another three rows with "sheet1 A1 & B1" Value then those 4 values must be
paste in "sheet1 "c" beside those four "Sheet A1&B1". iam unable to select those 4 SHEET2 "C" VALUES
If there is one "88" then it can just paste at sheet1"C1".
In this way it should do for every value in sheet 1 B:B.
At least Tell me what code is used to add rows with cell value through vba
How To Find Value & Copy Corresponding Cell
Sub copythedata()
Dim r As Long, ws As Worksheet, wd As Worksheet
Dim se As String
Dim sf As String
Dim fn As Integer
Dim y As Integer
Dim lrow As Long
Set ws = Worksheets("sheet2")
Set wd = Worksheets("sheet1")
y = 123
x = wd.Cells(Rows.Count, 1).End(xlUp).Row
MsgBox "Last Row: " & x
If x > y Then
wd.Range(wd.Cells(y, 1), wd.Cells(x, 1)).EntireRow.Delete Shift:=xlUp
End If
For r = wd.Cells(Rows.Count, "B").End(xlUp).Row To 2 Step -1
fn = Application.WorksheetFunction.countif(ws.Range("B:B"), wd.Range("B" & r).Value)
If fn = 1 Then
wd.Range("C" & r).Value = ws.Range("C" & r).Value
ElseIf fn > 1 Then
se = wd.Range(wd.Cells(A, r), wd.Cells(B, r)).EntireRow.Copy
wd.Range("A123").Rows(fn - 1).Insert Shift:=xlShiftDown
Else
wd.Range("C" & r).Value = "NA"
End If
Next r
End Sub
See Find and
FindNext
When using FindNext see the Remarks section for how to stop search after the 'wraparound' to the start, otherwise you get into an endless loop.
Option Explicit
Sub copythedata()
Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
Dim iLastRow1 As Integer, iLastRow2 As Long
Dim iRow As Integer, iNewRow As Long, iFirstFound As Long
Dim rngFound As Range, rngSearch As Range
Dim cell As Range, count As Integer
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("sheet2")
' sheet 2 range to search
iLastRow2 = ws2.Range("B" & Rows.count).End(xlUp).Row
Set rngSearch = ws2.Range("B1:B" & iLastRow2)
'Application.ScreenUpdating = False
' sheet1 range to scan
iLastRow1 = ws1.Range("B" & Rows.count).End(xlUp).Row
' add new rows after a blank row to easily identify them
iNewRow = iLastRow1 + 1
For iRow = 1 To iLastRow1
Set cell = ws1.Cells(iRow, 2)
Set rngFound = rngSearch.Find(what:=cell.Value, _
LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext)
If rngFound Is Nothing Then
'Debug.Print "Not found ", cell
cell.Offset(0, 1) = "NA"
Else
iFirstFound = rngFound.Row
Do
'Debug.Print cell, rngFound.Row
If rngFound.Row = iFirstFound Then
cell.Offset(0, 1) = rngFound.Offset(0, 1).Value
Else
iNewRow = iNewRow + 1
ws1.Cells(iNewRow, 1) = cell.Offset(, -1)
ws1.Cells(iNewRow, 2) = cell.Offset(, 0)
ws1.Cells(iNewRow, 3) = rngFound.Offset(0, 1).Value
End If
Set rngFound = rngSearch.FindNext(rngFound)
Loop Until rngFound.Row = iFirstFound
End If
Next
Application.ScreenUpdating = True
MsgBox "Finished", vbInformation
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

Resources