Find cell content and copy row onto new sheet - excel

I want to paste a list in the "InsertList" sheet. This list will only contain the word "Correct" or "False". From then on i need a way to search for the word "Correct" or "False" in the columnS P,Q,R,S,T,U,V.
e.g. If in the column "P" on the "InsertList" sheet the word "Correct" is found, i need that entire row from A to V to be copied onto it's destination, in this case "sheet1".
If the word "Correct" is found on the column "Q" on the "InsertList" sheet, the rows from A to V need to be copied in the Sheet2. And so on..

For i = 2 to Thisworkbook.Sheets(“ÏnsertList”).Range(“A64000”).End(xlup).row
If Thisworkbook.Sheets(“ÏnsertList”).Range(“P” & i).Value = “Correct” Then
Thisworkbook.Sheets(ÏnsertList”).Range(“A” & i & ":V" & i).Copy Thisworkbook.Sheets(“Sheet1”).Range(“A” & i)
ElseIf Thisworkbook.Sheets(“ÏnsertList”).Range(“Q” & i).Value = “Correct” Then
Thisworkbook.Sheets(ÏnsertList”).Range(“A” & i & ":V" & i).Copy
Thisworkbook.Sheets(“Sheet2”).Range(“A” & i)
End If
Next
Try using the above code in one of the modules

Related

Compute countif in column Range & combine if with count if

I have two formulas that I need to transfer to VBA.
On Excel, my formula would be =countif(A$2:A2,A2) so I transferred that using this formula but everything is returning to 1. The rows didn't become dynamic and I want only the values to be displayed.
For a = 2 To lrow
ws.Range("T" & a).Formula = "=CountIf(A$2&"":""&A2)"",""&A2)"
Next a
Next formula that I use in Excel is
=IF(COUNTIF(A:A,A2)>Q2,"Check","Ok")
I tried this formula in VBA:
For i = 2 to lrow
If Countif(ws.Range("A2:A" & lrow), "A2") > ws.Range("Q2:Q", & lrow) Then
ws.Range("T" & i).Value = "Check"
Else
ws.Range("T" & i).Value = "Ok"
End If
Next i
You could populate column T with your first formula with this line of code:
ws.Range("T2:T" & lrow).FormulaR1C1 = "=COUNTIF(R2C[-19]:RC[-19],RC[-19])"
I can't advise on your second formula unless you clarify where you want to write it...

Looping through all the string values of cell by vba

So this is what I want to do:
For example, in the sheet named "Outputs", the A1 cell input is
= "abcd" & Input!A5 & "efgh",
where Input!A5 is the cell value of the different sheet in the excel file.
I want to make it change so that after executing all the methods after selecting the A1 cell as
= "abcd" & Input!A5 & "efgh", I want to change the A1 cell as, = "abcd" & Input!A6 & "efgh", and then = "abcd" & Input!A7 & "efgh" and so on. (So it's basically replacing the values as A1 to Ai)
I thought of using Replace function, by writing the replacing string as Ai, and replacing i with i +1 by starting with for loop.
But I don't think this is a right method.
Could anyone shed light on how to address this?
Thanks in advance.
Check this
InputWorksheetLastRow = 7 'place here your last row value
Set ws = Worksheets("Input")
For i = 5 To InputWorksheetLastRow
Worksheets("Outputs").Range("A" & i - 4).Formula = "=" & Chr(34) & "abcd" & Chr(34) & "&Input!" & ws.Cells(i, 1).Address(0, 0) & "&" & Chr(34) & "efgh" & Chr(34)
Next i

Copy and paste area including merged cells

Hi I have a block of cells I need to copy and paste to a second sheet. Some of the cells in the block are merged. I have formatted the destination so the cells are also merged where they will receive merged cells.
Is their code to copy and paste as a block, the standard
Worksheets("9B").Range("C" & a & ":P " & b).Copy Worksheets("Undo").Range("C" & a & ":P" & b)
will not work with merged cells and I was hoping to avoid having to do each part individually as it is a reasonable size.
Thanks
If you have prepared cells in second sheet then do:
Sub CopyMerged()
a = 1
b = 10
'First method
Worksheets("9B").Range("C" & a & ":P " & b).Copy
Worksheets("Undo").Range("C" & a & ":P" & b).PasteSpecial
'Second method
Worksheets("Undo").Range("C" & a & ":P" & b).Value = Worksheets("9B").Range("C" & a & ":P " & b).Value
End Sub

Concatenate multiple cells in a column into one cell

I need to combine multiple cells in a column in one cell.
Data is present in Column A:
0100
0800
ABCD
LMKQ
.
.
.
and so on
Cell B2 should have the below value:
'0100', '0800', 'ABCD', 'LMKQ'.... and so on
My code identifies all the data but populates just the data in last cell:
Sub concatMyData()
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
Cells(2, "B").Value = "'" & Cells(i, "A").Value & " '" & Cells(i + 1, "A").Value & "'"
Next i
End Sub
Check this:
Sub concatMyData()
Dim first As Boolean
first = True
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
If Not first Then Cells(2, "B").Value = Cells(2, "B").Value & ", "
Cells(2, "B").Value = Cells(2, "B").Value & "'" & Cells(i, "A").Value & "'"
first = False
Next i
End Sub
You can do this with one line of code and not have to loop (much MUCH faster):
Range("B2").Formula = "'" & join(application.transpose(Range("A2:A" & range("A" & rows.count).end(xlup).row)),"', '") & "'"
I assumed you have a header in A1 so started on A2, you can change it to A1 if you wish.
I noticed you asked for an explanation on the other answer so:
Egan's code will loop through each cell in column A and add it to what is currently in cell B2. So it basically adds to B2 the value of what cell it is up in in column A and a ', 'each time it loops until it runs out of data in column A. It also puts a ' on the start and end.
My code will take the range and transpose it into an array (the data goes down, an array goes sideways so we transpose it), then it will join the array of values into a single string. We do this using the JOIN function and we specify ', ' as what it should put between each element of the array (the delimiter).
Then we simply add a ' on the start and the end as it won't have it there to begin with and voila, one string of joined values without looping.
They effectively do the same thing, mine just does it in one go rather than a cell at a time.

Translate a worksheet formula in VBA

Can somebody help me to write this formula in excel VBA?
=IF(ISERROR(VLOOKUP(A3,Temp!$A$3:$A$595,1,FALSE)),A3,"0")
My code is getting stuck with :"syntax error"
Sub checkDuplitems()
Application.ScreenUpdating = False
Const top As Integer = 3
Dim bottom As Long
bottom = Sheets("Temp").Cells(Rows.Count, top).End(xlUp).row
With ThisWorkbook.Sheets("trash").Range("A" & top & ":A" & bottom)
.Formula = "=IF(ISERROR(VLOOKUP(A" & top & ",Temp!$B$" & top & ":$B$" & bottom & _
",1,FALSE)),A" & top & ", & '" 0" & ," '")"
.Value = .Value
.SortSpecial
End With
'Call something...
End Sub
You have a concatenation problem in the second line of the .Formula line.
To emulate the formula you have at the top of your question (which is wrong incidentally because you should be pointing to $B$3:$B$595 or something like that because your look up cell A3 should not be inside the VLOOKUP range).
Try this new .Formula line:-
.Formula = "=IF(ISERROR(VLOOKUP(A" & top & ",Temp!$B$" & top & ":$B$" & bottom & _
",1,FALSE)),A" & top & ", " & "0)"
Are you sure you want to use top as both the starting row in column A and the column to get the bottom row from the Temp worksheet? The important column on the Temp worksheet is column B (i.e. 2) not C (i.e. 3).
If you are putting formula(s) into Trash!A3:A595 that reference Trash!A3:A595 then these are circular references and cannot be resolved under normal conditions. I'll put the formulas into column Z.
If you are operating with Excel 2007 or newer then I would humbly propose this alternate that uses the worksheet's IFERROR function and does not attempt to make text out of the 0 returned value.
Const top As Integer = 3
Dim bottom As Long
bottom = Sheets("Temp").Cells(Rows.Count, "B").End(xlUp).Row '<~~change here
With ThisWorkbook.Sheets("trash")
With .Range("Z" & top, .Cells(Rows.Count, "A").End(xlUp).Offset(0, 25))
.Formula = "=IFERROR(VLOOKUP(A" & top & ", Temp!$B$" & top & ":$B$" & bottom & _
", 1, FALSE), 0)" '<~~ big change here
.Value = .Value
End With
End With
It is also curious as to why the number of rows of formulas in the Trash worksheet must be governed by the number of rows of data in the Temp worksheet. I would have thought that the number of values in column A of the Trash sheet should govern how many formulas go into the Trash worksheet.

Resources