Macro - IF value meets condition copy paste to another sheet - vba

Can I get some help with this macro. I want to go through all values in column P of a table in 'Sheet1'. Here is a picture of the table:
First Table
If value is <1 I want to copy paste the values in columns with the headings in the picture to a table in an 'explanation' sheet in the following format:
Second Table
Any help with this would be appreciated

On the sheet you want the data to go into (table 2) and under your header (in table 2) set the cell equal to the cell you want from table 1. Do this by entering pressing "=" and then clicking to the sheet with table 1 and click on the cell on the first row. Repeat this for the Name, Identifier, Sector, Rate... Once you have the first row done, highlight the cells on the second row and click and hold the bottom right of the black out line and drag down to the same number of rows from table 1.
If you want the values pasted there, then highlight the range and copy and right click paste > paste special (click on paste special) and select values.
Then you can sort the data based on the explanation sheet by the column that's value you need to be less than 1 and delete the rows that are greater than 1.

I have no idea what columns you want copied as they are not named in the first table image you have, so I just did up a macro that you can adjust the ranges yourself as you need copied and to what sheet you need.
The below code loops through your data, checks if the data in Column P for each row is less than 1 and puts the data into a variable where you can copy and paste to where ever you need that data,
The code,
Sub d()
Dim i As Long
Dim lastrow As Long
Dim rng As Range
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
If Range("P" & i).Value < 1 Then
If Not rng Is Nothing Then
Set rng = Union(rng, Range("B" & i), Range("C" & i), Range("D" & i), Range("Q" & i), Range("H" & i), Range("I" & i), Range("P" & i))
Else
Set rng = Union(Range("B" & i), Range("C" & i), Range("D" & i), Range("Q" & i), Range("H" & i), Range("I" & i), Range("P" & i))
End If
End If
Next
rng.Copy Sheets("explanation").Range("A2")
End Sub
See if this does not help any?

Related

VBA - Add new Column then COPY cells and then SUM cells

I am attempting to create a new column that consolidates multiple values into a single Column. The code below has no problems creating a new column exactly where I need it.
Within each newly created column however, I would like my code to Sum the previous 5 cells within the same row. It works currently, but is static(The new column is G in the below example, and it sums B:F). I think I need some sort of Offset function built into it to allow the Range it sums to be dynamic (Previous 5 columns), as it loops through the entire sheet.
Additionally, I would love it to copy the Header information from rows 1-9 from the column to the left of the new column, into the new column, and place the word "Combo" into Row 10 of the new column.
Office 2013
Any help with this endeavor would be great.
Thanks
Dim currentColumn As Integer
Dim columnHeading As String
For currentColumn = Sheets("SLTData").UsedRange.Columns.Count To 2 Step -1
columnHeading = Sheets("SLTData").UsedRange.Cells(10, currentColumn).Value
'CHECK WHETHER TO KEEP THE COLUMN
Select Case columnHeading
Case "PD.G0100", "PD.G0500", "PD.G0800", "PD.G0900", "PD.G1000", "PD.G0300", "PD.G0400", "PD.G0150", "PD.G0600"
'Do nothing
Case Else
If InStr(1, _
Sheets("SLTData").UsedRange.Cells(10, currentColumn).Value, _
"Homer", vbBinaryCompare) = 0 Then
Sheets("SLTData").Columns(currentColumn).Insert
For i = 11 To Cells(Rows.Count, "B").End(xlUp).Row
Range("G" & i) = WorksheetFunction.Sum(Range("B" & i), Range("C" & i), Range("D" & i), Range("E" & i), Range("F" & i))
Next i
End If
End Select
Next
Instead of Range("G" & i) use Cells(row, column) (both row and column are integer). To sum up the 5 cells left of it: Cells(row, column) = WorksheetFunction.Sum(Range(Cells(row, column-5), Cells(row, column-1)))

VBA Whitelist - match against whitelist then delete specific range

I want to creat a macro that matches against a whitelist, then delete everything that's not on the whitelist. I have the following code:
Sub WHITELIST()
Dim LR As Long, i As Long
With Sheets("Sheet1")
LR = .Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
If IsError(Application.Match(.Range("A" & i).Value, Sheets("Whitelist").Columns("A"), 0)) Then .Rows(i).Delete
Next i
End With
End Sub
But my code deletes the entire row. I only want to delete the range A:B and shift the cells up (the row size is variable so i always want to check till the last row - up to 40.000 rows). Its important that "important Data" doesn't gets deleted too. Here is an example how the macro SHOULD work:
Hope someone can help me
Greetings
Try changing this line
If IsError(Application.Match(.Range("A" & i).Value, Sheets("Whitelist").Columns("A"), 0)) _
Then .Rows(i).Delete
to this
If IsError(Application.Match(.Range("A" & i).Value, Sheets("Whitelist").Columns("A"), 0)) _
Then .Range("A" & i & ":B" & i).Delete Shift:=xlUp
This will only delete the cells in columns A and B.
Using the Shift:=xlUp will allow you delete those specific cells in the range you define and move the entire block of cells below that range up.

Excel VBA range select

I have a macro in which I need to select the range R2:last row in sheet. However the last row in my sheet might be blank in column R. At the moment I am using this
Dim t As Range
Set t = Range("R2", Range("R1000").End(xlUp))
For Each Cell In t
If IsEmpty(Cell) Then
Cell.Activate
ActiveCell.Value = "To Be Picked Up"
End If
Next
However if the last row has a blank in column R then it gets ignored. I am hoping to pull the range using column A, as the last row of data always has column A. So something like,
Dim t As Range
Set t = Range("R2", Range("A1000").End(xlUp).ActiveCell.Offset(0, 17))
For Each Cell In t
If IsEmpty(Cell) Then
Cell.Activate
ActiveCell.Value = "To Be Picked Up"
End If
Next
It seems so simple but I'm sure im missing something stupid. Any help or alternative methods would be helpful thank you.
This should do the trick in one line:
Sub SO()
Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks).Value = "To Be Picked Up"
End Sub
But in answer to your question specifically
Set t = Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row)
The Range() method will accept a string as an argument to obtain a range.So we can build this string any way we want:
"A1000" '// A1000
"A" & 1000 '// A1000
"A" & "10" & "00" '// A1000
"A" & CStr(1001 - 1) '// A1000
"A" & Rows.Count will return A65536 or A1048576 depending on the type of worksheet.
Range("A1048576").End(xlUp) as you know, will retrieve the last cell in that area, or the first cell in the next area on the direction specified.
Range("A1048576").End(xlUp).Row will return the row number of that cell (let's say it's A1000 for argument's sake) so the return value is 1000.
"R2:R" & Range("A" & Rows.Count).End(xlUp).Row therefore makes the string R2:R1000.
So finally, Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row) is the same as Range("R2:R1000")

Copying to cells without having to select them first

I'm currently trying to use VBA to copy some cells from one location to another and because I'm new to VBA I was wondering if anyone could help me make my code a bit more efficient I know there must be a way to copy to a cell without having to select the cell and then copy to it
For i = 1 To dataSheet.Range("A" & dataSheet.Rows.Count).End(xlUp).Row
dataSheet.Range("A" & i & ":" & "CT" & i).Copy
Set rCell = dataSheet.Range("C" & i)
pasteSheet.Activate
If rCell = condition1 Then
With ActiveSheet
.Range("CU" & rowLoop2).Select
ActiveSheet.paste
End With
You have 2 options. Either use the .PasteSpecial method, or you can just reference the original range and set the new range to it's value.
.Range("CU" & rowLoop2).PasteSpecial Paste:=xlPasteAll
With the setting values option, you have to define the whole range which the values should fill.
Range("A3:E3").Value = Range("A1:E1").Value
If you just used Range("A3").Value = Range("A1:E1").Value only cell A3 would be populated, and it would take th value from cell A1. Hope this helps.
Edit: it's worth noting that you do not have to change sheets to paste either. Your code could be amended to the below:
With dataSheet
For i = 1 To .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A" & i & ":" & "CT" & i).Copy
Set rCell = .Range("C" & i)
If rCell = condition1 Then
pasteSheet.Range("CU" & rowLoop2).PasteSpecial Paste:=xlPasteAll
End If
Next i
End With

Assigning variable Row numbers to fixed Columns in a range

I have a code module that adds rows to a sheet and then populates them with formulas. Below the last row is a Totals row i.e. a non-contiguous range H21:M21,W21:AC21,AF21,AL21:AR21,AU21.
What I want to achieve is assign the row number from a variable that has been used to find the last row (before the totals row) i.e. i1stSumRow and then copy the Sum formula to each cell in the range.The column ID is fixed, only the row number changes.
The section of code is as follows:
With ActiveSheet
i1stSumRow = Cells(.Rows.count, "G").End(xlUp).Row
.Range("G" & (i1stSumRow)).Select
End With
ActiveCell.Select
formulatext = "=SUM(G" & (intLast_CS_Sheet) & ":" & "G" & (i1stSumRow - 2) & ")"
ActiveCell.Formula = formulatext
Selection.Copy
Range("H21:M21,W21:AC21,AF21,AL21:AR21,AU21").Select
Range("AU21").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
Range("C10").Select
I tried Range("H" & (i1stSumRow) & ":" & "M" & (i1stSumRow) etc. but without success.
What about something like this?
for j = 7 to 13 //Columns G through M
cells(i1stSumRow, j) = worksheetfunction.sum(range(cells(intLast_CS_Sheet,j),cells(i1stSumRow - 2, j)))
next j