Paste Results from Find Greater Than in Next Cell Down - excel

I am automating a Workbook my team complete each month and have become stuck with a code for returning results on Find Greater Than.
I did not write this code originally, I found it on Stackoverflow and adapted for my purpose. Original code is from:
excel vba copy cells to another sheet if cell value is greater than 0
On my sheet "Agent Count" I have information in Columns A, B, C, with C containing a numerical count result. The code finds any count greater than 50.
When the code finds a count greater than 50 in Column C, it then copy and pastes the three cells to a new location on the same sheet, starting at "F2". Creating a separate summary table of counts greater than 50.
The code is successfully finding and copying and pasting counts greater than 50. However after pasting the result it is not moving down to the next row. So pastes the next result over the top of the previous result.
How to write the code so the paste moves down through the rows F2, F3, F4 etc for each result?
Sub FindGreaterThan50V3()
Dim range1 As Range
Dim cell As Range
Set range1 = Sheets("Agent Count").Range("c:c")
For Each cell In range1
If cell.Value > 50 Then
With Sheets("Agent Count")
.Range(.Cells(cell.Row, "a"), .Cells(cell.Row, "c")).Copy _
Sheets("agent count").Range("f2").End(xlUp).Offset(1, 0)
End With
End If
Next cell
End Sub

This:
.Range(.Cells(cell.Row, "a"), .Cells(cell.Row, "c")).Copy _
Sheets("agent count").Range("f2").End(xlUp).Offset(1, 0)
Should probably be:
.Range(.Cells(cell.Row, "a"), .Cells(cell.Row, "c")).Copy _
Sheets("agent count").Cells(Rows.Count, "F").End(xlUp).Offset(1, 0)
Couple more suggestions:
Sub FindGreaterThan50V3()
Dim range1 As Range, ws As WorkSheet
Dim cell As Range
Set ws = ThisWorkbook.Sheets("Agent Count")
'no need to scan the whole column
Set range1 = ws.Range("C1:C" & ws.cells(ws.Rows.Count, "C").End(xlUp).Row)
For Each cell In range1.Cells
If cell.Value > 50 Then
cell.Resize(1, 3).Copy _
ws.Cells(ws.Rows.Count, "F").End(xlUp).Offset(1, 0)
End If
Next cell
End Sub

Related

If cell does not contain values from a named list

I have a piece of VBA code that sorts through a worksheet and deletes all rows that in which one of the columns does not contain specific values
Sub DeleteRows()
' Defines variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long, TestRange As Range, MyRange As Range
' Defines LastRow as the last row of data based on column C
LastRow = Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row
' Sets check range as E1 to the last row of C
Set cRange = Range("C1:C" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
With cRange.Cells(x)
' If the cell does not contain one of the listed values then...
If .Value <> "Location1" And .Value <> "Location2" And .Value <> "Location3" Then
' Delete that row
.EntireRow.Delete
End If
End With
' Check next cell, working upwards
Next x
End Sub
The problem is I have a very long and growing list of locations. Instead of specifying locations (e.g., "Location1", "Location2", etc.), I want the code to compare each cell in the check range against a named list ("ReferenceLocations") and delete the row if the cell contains a location name not in that list.
How can I change that section of code (if .value<>...) to achieve this?
Using Application.Match and IsError:
If IsError(Application.Match(.Value, Range("ReferenceLocations"), 0)) Then
.EntireRow.Delete
End If
This assumes that your named range is a single row or column. If that is not a safe assumption, then:
If Application.CountIfs(Range("ReferenceLocations"), .Value) = 0
.EntireRow.Delete
End If

Copy paste a fixed row based on some condition else skip to next column in that row

Have data set in I11:X11 and I want to copy formulas seating I12:I12 into I13:X20 based on data contained in I11:X11.
Starting with I11, if that contains certain value lets say TEST, then want to increment row for that range to next column that is J11 and if J11 <> TEST, then copy J12:X12 to J13:X20.
Further want to skip pasting this entire logic based on flag seating in column H13:H20, for example if H13 = Y, then skip to next row.
Adding a screenshot to further explaining the issue.
Condition should start with first member in range I11:X11, if it encounters first member <> TEST till T11 , then it should start copying from that range. In this case it encountered first <> TEST member at L11, then it should copy from L12:T12 to L13:T24 and V12:X12 to V13:X13. Further this logic should work on the flag contained in column H. If this column H Contains Y,then above logic should not paste in that row, this pasting activity should go on until last value in column H starting from H13.
The condition value from I11:T11 can change between TEST and any other values, not further.
Want to achieve this on a button click using a VBA code.
Adding Code, but it limits to the fixed column H values and Fixed row values.
Sub CopyOnCondition1()
Dim sh1 As Worksheet, c As Range
Set sh1 = Worksheets("SheetNameHere") 'change the sheetname
For Each cel In sh1.Range("I11:T11")
If Not cel.Value = "TEST" Then
sh1.Range(Cells(12, cel.Column), Cells(12, 20)).Copy
sh1.Range(Cells(13, cel.Column), Cells(24, 20)).PasteSpecial xlPasteFormulas
End If
Next
For Each cel In sh1.Range("H13:H24")
If cel.Value = "Y" Then sh1.Range("I" & cel.row & ":T" & cel.row).ClearContents
Next
End Sub
enter image description here
As I could understand from the Question I think you are looking for something like this:
Sub CopyOnCondition1()
Dim sh1 As Worksheet, c As Range
Set sh1 = Worksheets("SheetNameHere") 'change the sheetname
For Each cel In sh1.Range("I11:T11")
If Not cel.Value = "TEST" Then
sh1.Range(Cells(12, cel.Column), Cells(12, 20)).Copy
sh1.Range(Cells(13, cel.Column), Cells(24, 20)).PasteSpecial xlPasteFormulas
End If
Next
For Each cel In sh1.Range("H13:H24")
If cel.Value = "Y" Then sh1.Range("I" & cel.row & ":T" & cel.row).ClearContents
Next
End Sub
First It will paste in the complete Range. Then it wo go and check if H have Y, if yes, then it will delete the formula from that row.

How to copy/paste the formulas of Row 2 into lower rows IF Column A isn't empty?

I have a sheet where the cells in Column A auto-populate based on user input. Row 1 is the Headers. Row 2 is fully setup from B:JG with formulas as an example. I would like to have a button that runs a script to check Column A of each row, starting with 3, to see if its empty. If Column A is not empty, it should copy the FORMULAS from B2:JG2 and paste them into Columns B:JG on each row. If Column A is empty, I want it to leave the other columns blank.
I'm just diving into VBA, so any help with a script to accomplish is appreciated.
Example: Rows 3-110 have data in Column A, so B2:JG2 FORMULAS get copied into their B:JG columns. All rows after 110 get nothing because Column A is empty.
The button is on a sheet called "HexBox" and the sheet I need to update is "HexClean".
The user enters some info on the "HexBox" sheet and A:A is auto-populated based on their answers. So there could be 10 or 1000 rows in A:A with values and the rest up to 5000 will be "" if not applicable.
This approach simply
Copies the formulas from 2nd row down to the last used row as determined by Column A (one operation). Note that this step is indifferent of blanks in your column. That is handled in the following 2 steps
Loops through Column A and gather up instances of blank rows by adding them to a Union (collection of cells) (0 operations)
Clears the contents of the Union that is built in step 2 (one operation)
This is a more effecient way to go. Copying & pasting the formulas inside your loop one row at a time will lead to a lot of spread sheet operations. This method has a max of 2 operations
Sub HexSub()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("HexClean")
Dim LR As Long, i As Long, ClearMe As Range
LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
ws.Range("B2:JG2").Copy
ws.Range(ws.Cells(3, "B"), ws.Cells(LR, "JG")).PasteSpecial xlPasteFormulas
For i = 3 To LR
If ws.Range("A" & i) = "" Then
If Not ClearMe Is Nothing Then
Set ClearMe = Union(ClearMe, ws.Range("A" & i))
Else
Set ClearMe = ws.Range("A" & i)
End If
End If
Next i
If Not ClearMe Is Nothing Then ClearMe.EntireRow.ClearContents
End Sub
If your range will never have blanks followed by more values, then you can just get rid of the loop and everything below it
If the non-blank cells in column A are typed values then SpecialCells should be able to find them quickly.
Sub populateBelow()
Dim frng As Range
With Worksheets("sheet3")
Set frng = .Range(.Cells(2, "B"), .Cells(2, "JG"))
With .Range(.Cells(3, "A"), .Cells(.Rows.Count, "A").End(xlUp))
With .SpecialCells(xlCellTypeConstants, xlTextValues + xlNumbers)
frng.Copy Destination:=.Offset(0, 1)
End With
End With
End With
End Sub
Sub CopyToMany()
Dim xRow As Range, aCel As Range
For Each xRow In ActiveSheet.UsedRange.Rows
If xRow.Row > 2 Then
Set aCel = xRow.Cells(1, 1)
If aCel.Value <> "" Then
ActiveSheet.Range("B2:JG2").Copy Destination:=ActiveSheet.Range("B" & xRow.Row & ":JG" & xRow.Row)
End If
End If
Next xRow
End Sub

Replacing the values in a column depending on a match condition using VBA

Test_FileI am trying to replace the values in a column with a specific condition:
IF(C37808=$Q$1,D37808,0); So I have copied the filtered column D(D31924:D37908) and pasted it another column F. My goal is to replace the corresponding values of Column F based on the IF-ELSE condition mentioned above.
The first challenge was locating the range of Column C & D because they're are dynamic meaning the first value of Column C or D could start from row number 1 or row number 100 based on a filtered criteria. I have figured out a way to locate that dynamic range so no issue there. Now when I try to replace the values in the corresponding cells of Column F, I see that it pastes only one value. Sharing the code segment below:
Sub Replacement()
Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim v
With ActiveSheet.AutoFilter.Range
.SpecialCells(xlCellTypeVisible).Areas(2)(1, 3).Select
End With
Set rng1 = Range(Selection, Selection.End(xlDown)) 'Finding the range of column C from the filtered region
rng1.Copy
v = WorksheetFunction.Mode(rng1) 'finding the mode/highest occuring number in rng1
Range("Q1").Value = v 'storing if for comparison purpose later
'Navigating to column F
With ActiveSheet.AutoFilter.Range
.SpecialCells(xlCellTypeVisible).Areas(2)(1, 3).Select
End With
ActiveCell.Offset(0, 3).PasteSpecial 'pasting the copied column C into column F
With ActiveSheet.AutoFilter.Range
.SpecialCells(xlCellTypeVisible).Areas(2)(1, 6).Select
End With
Set rng2 = Range(Selection, Selection.End(xlDown)) 'storing the column F's range for replacing
With ActiveSheet.AutoFilter.Range
.SpecialCells(xlCellTypeVisible).Areas(2)(1, 4).Select
End With
Set rng3 = Range(Selection, Selection.End(xlDown)) 'storing the column D's range for using in Column F
'Looping through each element of column F now and matching against my condition
For Each z1 In rng2
If z1.Value = v Then
z1.Value = rng3.Value 'expectation is to implement the formula :IF(C37808=$Q$1,D37808,0)
Else: z1.Value = 0
End If
Next z1
End Sub
After executing the code, I get the only 1st value of column D in all the cells of column F. I would really appreciate if someone can help me fix this issue. Attached is the screenshot of the result.
result
I commented and re-worked your code. Now it looks like this.
Sub Replacement()
Dim RngC As Range ' Range is in column C
Dim RngF As Range ' use descriptive names
Dim RngD As Range
Dim Cell As Range
Dim v As Variant
' don't Select anything
' With ActiveSheet.AutoFilter.Range
' .SpecialCells(xlCellTypeVisible).Areas(2)(1, 3).Select
' End With
' the next line will produce an error if no filter is set
On Error Resume Next
Set RngC = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas(2)(1, 3)
If Err Then
MsgBox "Please specify filter criteria", vbCritical, "Can't Proceed"
Exit Sub
End If
On Error GoTo 0
Set RngC = Range(RngC, RngC.End(xlDown)) 'Finding the range of column C from the filtered region
v = WorksheetFunction.Mode(RngC) 'finding the mode/highest occuring number in RngC
' no need to write v to the sheet
' Range("Q1").Value = v 'storing if for comparison purpose later
'Navigating to column F ' don't "navigate". Just address
' With ActiveSheet.AutoFilter.Range
' this is the first cell of RngC
' .SpecialCells(xlCellTypeVisible).Areas(2)(1, 3).Select
' End With
' ActiveCell.Offset(0, 3).PasteSpecial 'pasting the copied column C into column F
RngC.Copy Destination:=RngC.Cells(1).Offset(0, 3)
' don't Select anything!
' With ActiveSheet.AutoFilter.Range
' .SpecialCells(xlCellTypeVisible).Areas(2)(1, 6).Select
' End With
Set RngF = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas(2)(1, 6)
' as an alternative you might specify:-
' Set RngF = RngC.Offset(0, 3)
Set RngF = Range(RngF, RngF.End(xlDown)) 'storing the column F's range for replacing
' With ActiveSheet.AutoFilter.Range
' .SpecialCells(xlCellTypeVisible).Areas(2)(1, 4).Select
' End With
' Set RngD = Range(Selection, Selection.End(xlDown)) 'storing the column D's range for using in Column F
Set RngD = RngC.Offset(0, 1)
' Looping through each element of column F now and matching against my condition
For Each Cell In RngF
With Cell
If .Value = v Then
' your line always inserts RngD.Value.
' Since RngD is an array but the cell can only take one value
' VBA inserts the first value of the array.
' z1.Value = RngD.Value 'expectation is to implement the formula :IF(C37808=$Q$1,D37808,0)
.Value = RngD.Cells(.Row).Value
Else
.Value = 0
End If
End With
Next Cell
End Sub
It pastes different values now but seems to select them from the wrong rows. The reason is the method by which you set ranges. I don't believe it works as you expect and I can't test it because I don't have data. Two facts:-
A range defined as starting from any cell (such as ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas(2)(1, 3)) and ending at the last used cell, will include both visible and invisible cells.
The index number of the cells in your ranges aren't aligned with the row numbers of the worksheet.
The task actually is very simple. Just loop through all cells in column F and if an examination meets certain criteria copy the value from column D in the same row. The row number is never in question if handled in this way. So, if my solution doesn't copy from the correct row I suggest you redesign the loop.

Understanding & Additional code for excel row copy based on value

Please see below code I have found on the internet, which is currently working to a certain degree for me.
Could someone possibly commentate on what each line of this code means so I can understand what its doing?
Im trying to understand it with little programming knowledge and add additional code to look for additional values to paste into additional sheets.
I'm also trying to work out how to make them paste to certain rows one after the other and not maintain the row they were originally in on sheet 1.
Code:
Sub Test()
Dim rw As Long, Cell As Range
For Each Cell In Sheets(1).Range("H:H")
rw = Cell.Row
If Cell.Value = "Dept 1" Then
Cell.EntireRow.Copy
Sheets("Sheet2").Range("A" & rw).PasteSpecial xlPasteValues
End If
Next
End Sub
--
Many thanks
I've added comments as requested. To paste them onto the same row, look at removing the rw variable and replacing it with something that increments by one each time
Sub Test()
'declare variables
Dim rw As Long, Cell As Range
'loop through each cell the whole of column H in the first worksheet in the active workbook
For Each Cell In Sheets(1).Range("H:H")
'set rw variable equal to the row number of the Cell variable, which changes with each iteration of the For loop above
rw = Cell.Row
'check if the value of Cell variable equals Dept 1
If Cell.Value = "Dept 1" Then
'copy the entire row if above is true
Cell.EntireRow.Copy
'paste to the same row of Sheet 2
Sheets("Sheet2").Range("A" & rw).PasteSpecial xlPasteValues
End If
Next
End Sub
Here is your Code Commented hope you understand:
Sub Test()
' Variables Defined as follows:
Dim rw As Long, Cell As Range
' Loop Searching each Cell of (Range H1 to end of last H on sheet1
For Each Cell In Sheets(1).Range("H:H")
' now determine current row number:
rw = Cell.Row
' Test cell value if it contain >> Dept 1 as value:
If Cell.Value = "Dept 1" Then
' Select that row and copy it:
Cell.EntireRow.Copy
' now paste the values of that row into A column and rw row on sheet2:
Sheets("Sheet2").Range("A" & rw).PasteSpecial xlPasteValues
' You should add following to:
' Disable marching ants around copied range:
Application.CutCopyMode = False
End If
Next
End Sub

Resources