Compare two cells and run different code based on comparison - excel

I would like my code to compare cells using an If statement. When the cells are not equal, I want to run my other code.
It is showing red on my screen when I try to do the Else If statement.
Dim WS1 As Worksheet: Set WS1 = ThisWorkbook.Sheets("Increments")
Dim WS2 As Worksheet: Set WS2 = ThisWorkbook.Sheets("Output")
Dim LR1 As Long, LR2 As Long, WS1_Cell As Range, WS2_Cell As Range
LR1 = WS1.Range("S" & WS1.Rows.Count).End(xlUp).Row
LR2 = WS2.Range("H" & WS2.Rows.Count).End(xlUp).Row
For Each WS1_Cell In WS1.Range("S1:S" & LR1)
For Each WS2_Cell In WS2.Range("H1:H" & LR2)
Else If WS1_Cell = WS2_Cell Then
WS2_Cell.Offset(, 5).Value = WS1_Cell.Offset(, 5).Value
Next WS2_Cell
Next WS1_Cell
Else WS1_Cell <> WS2_Cell Then
Dim wsCopy2 As Worksheet
Dim wsDest2 As Worksheet
Dim lCopyLastRow2 As Long
Dim lDestLastRow2 As Long
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy2 = Worksheets("Increments")
Set wsDest2 = Worksheets("Output")
'1. Find last used row in the copy range based on data in column S
lCopyLastRow2 = wsCopy2.Cells(wsCopy2.Rows.Count, "S").End(xlUp).Row
'2. Find first blank row in the destination range based on data in column H
'Offset property moves down 1 row
lDestLastRow2 = wsDest2.Cells(wsDest2.Rows.Count, "H").End(xlUp).Offset(1).Row
'3. Copy & Paste Data if match not found
wsCopy2.Range("S3:X" & lCopyLastRow2).COPY
wsDest2.Range("H" & lDestLastRow2).PasteSpecial xlValues
End If

Related

How to filter then populate on a separate sheet?

I am trying to filter out anything that is below 70% to populate on a separate sheet.
Image of what I am pulling from.
I looked online and got a little code.
Here is what I have and am running into an error.
Private Sub CommandButton1_Click()
lastrow = Worksheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
For r = 2 To lastrow
If Worksheets("sheet1").Range("O" & r).Value < "70%" Then
Worksheets("sheet1").Rows(r).Copy
Worksheets("sheet2").Activate
lastrowrpt = Worksheets("sheet2").Range("O" & Row.Count).End(xlUp).Row
Worksheets("sheet2").Range("O" & lastrowrpt + 1).Select
ActiveSheet.Paste
End If
Next r
End Sub
This should get you started
In this case you can use the filter and visible cells to copy the range to the other worksheet.
Adjust it to fit your needs
Private Sub CommandButton1_Click()
Dim sourceSheet As Worksheet
Dim sourceRange As Range
Dim sourceFilteredRange As Range
Dim targetSheet As Worksheet
Dim targetCell As Range
Dim cell As Range
Dim sourceLastRow As Long
Dim targetLastRow As Long
' Define source and target objects
Set sourceSheet = ThisWorkbook.Worksheets("Sheet1")
Set targetSheet = ThisWorkbook.Worksheets("Sheet2")
' Get last row of source sheet
sourceLastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
' Get last row of target sheet
targetLastRow = targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Row + 1
' Set source range
Set sourceRange = sourceSheet.Range("A1:O" & sourceLastRow)
' Filter source range by route and shipped
With sourceRange
.AutoFilter Field:=15, Criteria1:="<70%"
End With
' Get filtered range
Set sourceFilteredRange = sourceRange.SpecialCells(xlCellTypeVisible)
' Copy filtered range to target sheet
sourceFilteredRange.Copy targetSheet.Range("A" & targetLastRow)
End Sub
Let me know if it works

Make all active cells in a specific Column to its Absolute Value

Can you help me correct my VBA codes. I want to convert the values of Column U until the active row to Absolute Values meaning to remove the negative amounts.
Here is my VBA code:
Sub MakeColumnsAbsoluteValue()
Dim sht As Worksheet
Dim rngToAbs As Range
Dim LastRow As Long
Dim c As Range
Set sht = ThisWorkbook.Sheets("MJEBlackline")
LastRow = sht.Cells(sht.Rows, Count, "U").End(xlUp).Row
Set rngToAbs = Range("U5:U" & LastRow)
For Each c In rngToAbs
c.Value = Abs(c.Value)
Next c
End Sub
Problem with line LastRow = sht.Cells(sht.Rows, Count, "U").End(xlUp).Row
Use of , instead of . and not specifying the sheet reference in rngToAbs
Try:
Sub MakeColumnsAbsoluteValue()
Dim sht As Worksheet
Dim rngToAbs As Range
Dim LastRow As Long
Dim c As Range
Set sht = ThisWorkbook.Sheets("FF")
LastRow = sht.Cells(sht.Rows.count, "U").End(xlUp).row
Set rngToAbs = sht.Range("U5:U" & LastRow)
For Each c In rngToAbs
c.Value = Abs(c.Value)
Next c
End Sub
You may try:
Sub MakeColumnsAbsoluteValue()
Dim sht As Worksheet
Dim rngToAbs As Range
Dim LastRow As Long
Set sht = ThisWorkbook.Sheets("MJEBlackline")
With sht
LastRow = .Cells(.Rows.Count, "U").End(xlUp).Row
Set rngToAbs = .Range("U5:U" & LastRow)
rngToAbs.Value = .Evaluate("=abs(" & rngToAbs.Address & ")")
End With
End Sub
Or even (inspired through #GarysStudent):
Sub MakeColumnsAbsoluteValue()
Dim sht As Worksheet
Dim rngToAbs As Range
Dim LastRow As Long
Set sht = ThisWorkbook.Sheets("MJEBlackline")
With sht
LastRow = .Cells(.Rows.Count, "U").End(xlUp).Row
Set rngToAbs = .Range("U5:U" & LastRow)
rngToAbs.Replace what:="-", lookat:=xlPart, replacement:=""
End With
End Sub
This would both convert the whole range in one go. Assuming that's what you meant with:
"I want to convert the values of Column U until the active row..."
You could try:
Option Explicit
Sub MakeColumnsAbsoluteValue()
Dim sht As Worksheet
Dim rngToAbs As Range, c As Range
Dim LastRow As Long, x As Long
Dim arr() As Variant
Set sht = ThisWorkbook.Sheets("MJEBlackline")
x = 0
With sht
LastRow = .Cells(.Rows.Count, "U").End(xlUp).Row
Set rngToAbs = .Range("U5:U" & LastRow)
'Loop range and create an array including all abs values
For Each c In rngToAbs
ReDim Preserve arr(x)
arr(x) = Abs(c.Value)
x = x + 1
Next c
'Paste the values of the array at once instead of pasting values one by one
.Range("U5:U" & LastRow).Value = Application.WorksheetFunction.Transpose(arr)
End With
End Sub

Dynamic Range to copy between two workbooks

I have problem to make a dynamic range to copy between two workbooks. I have create the following code and when I run step by step the code I take “Run time error 1004” “Method Range of object worksheet failed” My thought is to create dynamic range for the workbook with new data because is change all the time and the only last cell with data is in column “D” then expand this to column “S” and copy this to Master workbook Data sheet and again find the last used cell in column D and offset this to column “A” . How can make this task?
Sub CopyValuesToMaster()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim RngAC1 As Range
Dim RngAC2 As Range
Dim NewRng As Range
Dim DestLastRow As Long
Set wsCopy = Workbooks("sl0032019.xls").Worksheets("Sheet1")
Set wsDest = Workbooks("Master-Braun.xlsx").Worksheets("Data")
DestLastRow = Cells(Rows.Count, "D").End(xlUp).Offset(1, -3).Row
CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "D").End(xlUp).Row
Set RngAC1 = wsCopy.Range("A1")
Set RngAC2 = wsCopy.Range(Cells(Rows.Count, "D").End(xlUp).Offset(0, 15).Row)
Set NewRng = Range(RngAC1.Address & ":" & RngAC2.Address)
NewRng.Copy wsDest.Range("A" & DestLastRow)
End Sub
Try this.
Sub CopyValuesToMaster()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim RngAC1 As Range
Dim RngAC2 As Range
Dim NewRng As Range
Dim DestLastRow As Long
Set wsCopy = Workbooks("sl0032019.xls").Worksheets("Sheet1")
Set wsDest = Workbooks("Master-Braun.xlsx").Worksheets("Data")
DestLastRow = wsDest.Cells(Rows.Count, "D").End(xlUp).Offset(1, -3).Row
CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "D").End(xlUp).Row
With wsCopy
Set RngAC1 = .Range("A1")
Set RngAC2 = .Range("S" & .Cells(.Rows.Count, "D").End(xlUp).Row)
End With
Range(RngAC1, RngAC2).Copy wsDest.Range("A" & DestLastRow)
End Sub

Copy a range and not the entire row

I have code for matching values in column 'B' with the value in Cell 'M15' and copy and delete those rows.
I only need to copy and delete a range (A to J) and not the entire row.
Sub MoveRows()
Dim Sht1 As Worksheet, Sht3 As Worksheet
Dim tfRow As Range, C As Range
Dim CopyRng As Range
Dim LastRow As Long
Application.ScreenUpdating = False
Set Sht1 = Sheets("Sheet1")
Set Sht3 = Sheets("Sheet3")
With Sht1
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Set tfRow = .Range("B2:B" & LastRow)
For Each C In tfRow
If IsEmpty(C) Then
Exit Sub
End If
If C.Value = .Range("M15").Value Then
If Not CopyRng Is Nothing Then
Set CopyRng = Application.Union(CopyRng, C)
Else
Set CopyRng = C
End If
End If
Next C
End With
If Not CopyRng Is Nothing Then
LastRow = Sht3.Cells(Sht3.Rows.Count, "B").End(xlUp).Row
CopyRng.EntireRow.Copy Destination:=Sht3.Range("A" & LastRow + 1)
CopyRng.EntireRow.Delete (xlShiftUp)
End If
The basic code below uses an auto-filter(much faster then looping) and copies the visible cells in the range to the first empty cell in column A.
Deleting part of your data will cause the rows to shift and possible screw up your data. I also provided a line of code to only clear you range.
Dim ws1 As Worksheet, ws3 As Worksheet, lRow As Long, Rng As Range
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws3 = ThisWorkbook.Sheets("Sheet3")
lRow = ws3.Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = ws1.Range("A1").CurrentRegion
Rng.AutoFilter Field:=2, Criteria1:=ws1.Range("M15").Value
ws1.Range(Cells(2, 1), Cells(Rng.Rows.Count, 10)).SpecialCells(xlCellTypeVisible).Copy _
Destination:=ws3.Range("A" & lRow + 1)
'You must clear the range and then remove the filter before deleting blank cells.
ws1.Range(Cells(2, 1), Cells(Rng.Rows.Count, 10)).SpecialCells(xlCellTypeVisible).Clear
ws1.Cells.AutoFilter
ws1.Range(Cells(2, 1), Cells(Rng.Rows.Count, 10)).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp

VBA- Transferring data to another work sheet if a column has certain text and pasting it in a certain cell range

I am currently trying to filter data and paste it into another sheet to a certain range but it is only posting the latest data row. How do I fix the code so that it selects all the rows with the code word and pastes it into the other sheet.
This is my code:
Private Sub CommandButton1_Click()
Dim lastrow As Long, i As Long
lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If Sheets("sheet1").Cells(i, 1) = "pp" Then
Sheets("sheet1").Range(Cells(i, 2), Cells(i, 5)).Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet5").Range("A11:A22")
End If
Next
End Sub
I think this is what you want.
Private Sub CommandButton1_Click()
Dim ws1 as Worksheet: Set ws1 = Thisworkbook.Sheets("Sheet1")
Dim ws2 as Worksheet: Set ws2 = Thisworkbook.Sheets("Sheet5")
Dim LRow1 As Long, LRow2 as Long, i As Long
LRow1 = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row
LRow2 = ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If ws1.Cells(i, 1) = "pp" Then
ws1.Range(Cells(i, 1), Cells(i, 5)).Copy
ws2.Range("A" & LRow + 1).PasteSpecial xlPasteValues
End If
Next
End Sub
Here is a more effecient method using a For Each loop and one instance of Copy/Paste instead of 1 iteration for every matched cell.
Option Explicit
Sub Copy()
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")
Dim TargetRange As Range, TargetCell As Range, CopyRange As Range
Set TargetRange = ws1.Range("A2:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
For Each TargetCell In TargetRange
If TargetCell = "pp" Then
If CopyRange Is Nothing Then
Set CopyRange = TargetCell.Resize(1, 4)
Else
Set CopyRange = Union(CopyRange, TargetCell.Resize(1, 4))
End If
End If
Next TargetCell
CopyRange.Copy
ws2.Range("A" & ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row).PasteSpecial xlPasteValuesAndNumberFormats
End Sub
Another method would be to apply a filter for your target value pp and then copy/paste visible cells.

Resources