Multiple If Functions - excel

I need some help for my code. I want to copy client's name on column C based on these 2 conditions if:
Macro find value = "ongoing" on Column G
Macro find value = "Istry" on column D
In other words if macro find "ongoing" and "istry" at same row, it will copy automatically the client's name associated with these 2 values asked on another sheet.
I wrote a code but when I tried to run it, I didn't get any result on my sheet.
Sub Ss()
Dim finalrow As Long, i As Long, rowpt As Long, colpt As Long
finalrow = ShSReturn.Range("D" & "G" & Rows.Count).End(xlUp).Row
rowpt = ShPPT.Cells(Rows.Count, 1).End(xlUp).Row
colpt = ShPPT.Cells(Rows.Count, 1).End(xlUp).Row
Call Entry_Point
For i = 7 To finalrow
If ShSReturn.Cells(i, 4).Value = "Istry" & ShSReturn.Cells(i, 7).Value = "Ongoing" Then
ShSReturn.Cells(i, 3).Copy
ShPPT.Cells(rowpt + 6, 12).PasteSpecial xlPasteValues
rowpt = rowpt + 1
colpt = colpt + 1
End If
Next i
End Sub

Making some assumptions here about your intent for this code here is a quick rewrite:
Sub Ss()
Dim finalrow As Long, i As Long, rowpt As Long, colpt As Long
'Determine how many rows we need to loop:
finalDRow = ShSReturn.Range("D" & Rows.Count).End(xlUp).Row
finalGRow = ShSReturn.RAnge("G" & Rows.Count).End(xlUp).Row
'Loop only through rows were both G and D have records
If finalDRow < finalGRow Then finalrow = finalDRow Else finalRow = finalGRow
'I don't know what these two are doing, but they will return the same exact number (the last row populated in column A of whatever worksheet object is in ShPPT
rowpt = ShPPT.Cells(Rows.Count, 1).End(xlUp).Row
colpt = ShPPT.Cells(Rows.Count, 1).End(xlUp).Row
Call Entry_Point
'Loop through rows 7 to whatever finalRow shakes out to be above
For i = 7 To finalrow
'If column D is "Istry" AND column G is "Ongoing" Then execute this code.
If ShSReturn.Cells(i, 4).Value = "Istry" AND ShSReturn.Cells(i, 7).Value = "Ongoing" Then
ShSReturn.Cells(i, 3).Copy
ShPPT.Cells(rowpt + 6, 12).PasteSpecial xlPasteValues
rowpt = rowpt + 1
colpt = colpt + 1
End If
Next i
End Sub

You can use a Filter.
Be sure to set the appropriate worksheet references.
As written, the code copies the entire row, but you can easily modify it if you only want a few fields to be copied over.
Option Explicit
Option Compare Text
Sub filterName()
Const strG = "ongoing"
Const strD = "lstry"
Dim rCopyTo As Range
Dim rData As Range
Dim lastRow As Long, LastCol As Long
With Worksheets("Sheet6")
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rData = .Range(.Cells(1, 1), .Cells(lastRow, LastCol))
End With
Set rCopyTo = Worksheets("sheet7").Cells(1, 1)
Application.ScreenUpdating = False
rData.AutoFilter field:=4, Criteria1:=strD, visibledropdown:=False
rData.AutoFilter field:=7, Criteria1:=strG, visibledropdown:=False
rCopyTo.Cells.Clear
rData.SpecialCells(xlCellTypeVisible).Copy rCopyTo
rData.Worksheet.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub

Related

Filling out inserted cell with text

This code is running perfectly fine on inserting blank rows based on a cell value, but now I need to also fill out those new rows in range("E") saying "False". Not sure how to make this work.
Sub Procedure1()
Dim i As Integer
Dim LastRow As Long
LastRow = Sheets("Sheet1").Cells(Rows.Count, "F").End(xlUp).Row
For i = LastRow To 2 Step -1
a = Sheets("Sheet1").Cells(i, 8).Value
For j = 1 To a
Sheets("Sheet1").Rows(i + 1).Select
Selection.Insert Shift:=xlDown
Next
Next
Sheets("Sheet1").Cells(i, 1).Select
End Sub
Any thoughts?
You can avoid the inner loop using Resize.
And you can generally avoid Select.
Sub Procedure1()
Dim i As Long, j As Long
Dim LastRow As Long
Dim a
With Sheets("Sheet1")
LastRow = .Cells(Rows.Count, "F").End(xlUp).Row
For i = LastRow To 2 Step -1
a = .Cells(i, 8).Value 'best to check this is a number before going further
.Rows(i + 1).Resize(a).Insert Shift:=xlDown
.Range("E" & i + 1).Resize(a).Value = "False"
Next
End With
End Sub

Selecting range based on specific text then fill with text

I trying to write a code where I want to look select range from Start to End in column A then fill the selected range in Column B with '1'. But my code only fill up bottom parts and missed out top parts. Refer to this photo. How do i make sure it go through every row and when it encounter Start, it will look for nearest End and fill up column B?
Sub Select()
Dim LastRowA As Long, i As Long
Dim findrow As Long, findrow2 As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRowA
findrow = Range("A:A").Find("Start", Range("A1")).Row
findrow2 = Range("A:A").Find("End", Range("A" & findrow)).Row
Range("A" & findrow & ":A" & findrow2).Offset(0, 1).Value = "1"
Next i
End With
End Sub
This will find Start, loop through until it finds End. Tested and working as requested.
Sub Select()
Dim LastRowA, i As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRowA
If .Cells(i, 1) = "Start" Then
Do Until .Cells(i, 1) = "End"
.Cells(i, 2).Value = 1
i = i + 1
Loop
.Cells(i, 2).Value = 1
End If
Next i
End With
End Sub
Find is a precarious beast to use in VBA. It would be far simpler to just loop through the cells, keeping track of whether or not you're between Start and End:
Sub Select()
Dim LastRowA As Long, i As Long, b As Boolean
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRowA
If .Cells(i, 1).Value = "Start" Then
b = True
.Cells(i, 2).Value = 1
ElseIf .Cells(i, 1).Value = "End" Then
b = False
.Cells(i, 2).Value = 1
ElseIf b Then
.Cells(i, 2).Value = 1
End If
Next i
End With
End Sub

Why is my VBA code not iterating to the next i?

Trying to populate a sheet with only instances that meet a criteria. Here the criteria is a 1 in the last column of the dataset.
Current code is only pulling the first iteration. Does not go to next i. Next i in the current dataset is an instance that should be pulled so that is not the issue.
Sub Cleaned()
Dim LastRow As Integer, c As Long, i As Integer, erow As Integer
Worksheets("SPData").Activate
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
c = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To 600
If Cells(i, c) = 1 Then
Range(Cells(i, 1), Cells(i, c)).Select
Selection.Copy
Worksheets("CleanedData").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
Next i
End Sub
Also tried:
Sub Moving()
Dim r As Long, c As Long, i As Integer, erow As Integer
Worksheets("SPData").Activate
r = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
c = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2 To r
If Cells(i, c) = 1 Then
Range(Cells(i, 1), Cells(i, c)).Select
Selection.Copy
Worksheets("CleanedData").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
End If
Next i
End Sub
Correct me if I am wrong - you want to copy the entire row if the value in the last column is equal to 1?
If so then this code works:
Sub Moving()
Dim r As Long
Dim c As Long
Dim i As Integer
Dim erow As Integer
With Worksheets("SPData")
r = .Cells(.Rows.Count, 2).End(xlUp).Row
c = .Cells(1, .Columns.Count).End(xlToLeft).Column
For i = 2 To r
If .Cells(i, c) = 1 Then
.Range(.Cells(i, 1), .Cells(i, c)).Copy
With Worksheets("CleanedData")
erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
.Paste Destination:=.Cells(erow, 1)
End With
End If
Next i
End With
End Sub
I would strongly advise you to avoid using .Select in VBA whenever you can.
Also it is usually much better to refer to the actual sheet rather than using ActiveSheet.

Extract rows from multiple sheets into one and exclude any row with #N/A

I have one sheet of data where I need to extract the values from multiple columns and assign them a value. Column A is a string where column B is the assigned value. Columns C and D are vlookups based on column A and they will need the assigned value from column B as well. Please see the screenshots. I would need to compile a list on a separate sheet. Ideally column A would have the data from columns A, C and D from the other sheet and column B would have the assigned values. Only caveat is I need to exclude any row that has #N/A
Any macro that may work would be very helpful!
Code I was using
Sub Life_Saver_Button()
Dim lastrow As Long, erow As Long
Set S1 = Worksheets("Sheet1")
Set S2 = Worksheets("Sheet2")
lastrow = ThisWorkbook.Sheets("S1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
S1.Cells(i, 1).Copy
erow = ThisWorkbook.Sheets("S2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ThisWorkbook.Sheets("S1").Paste Destination:=ThisWorkbook.Sheets("S2").Cells(erow, 1)
ThisWorkbook.Sheets("S1").Cells(i, 2).Copy
ThisWorkbook.Sheets("S1").Paste Destination:=ThisWorkbook.Sheets("S2").Cells(erow, 2)
ThisWorkbook.Sheets("S1").Cells(i, 3).Copy
ThisWorkbook.Sheets("S1").Paste Destination:=ThisWorkbook.Sheets("S2").Cells(erow, 1)
ThisWorkbook.Sheets("S1").Cells(i, 4).Copy
ThisWorkbook.Sheets("S1").Paste Destination:=ThisWorkbook.Sheets("S2").Cells(erow, 2)
ThisWorkbook.Sheets("S1").Cells(i, 5).Copy
ThisWorkbook.Sheets("S1").Paste Destination:=ThisWorkbook.Sheets("S2").Cells(erow, 1)
ThisWorkbook.Sheets("S1").Cells(i, 5).Copy
ThisWorkbook.Sheets("S1").Paste Destination:=ThisWorkbook.Sheets("S2").Cells(erow, 2)
Next i
Application.CutCopyMode = False
ThisWorkbook.Sheets("S2").Columns().AutoFit
Range("A1").Select
End Sub
Try:
Option Explicit
Sub test1()
Dim LastrowA As Long, Lastrow As Long, cell As Range, Code As Long
Dim Desc As String
With ThisWorkbook.Worksheets("Sheet1")
LastrowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For Each cell In .Range("A1:D" & LastrowA)
If Not IsError(cell.Value) = True And Not IsNumeric(cell.Value) = True Then
Desc = cell.Value
Code = .Range("B" & cell.Row).Value
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
If LastrowA = Lastrow Then
.Range("A" & Lastrow + 2).Value = Desc
.Range("B" & Lastrow + 2).Value = Code
Else
.Range("A" & Lastrow + 1).Value = Desc
.Range("B" & Lastrow + 1).Value = Code
End If
End If
Next
End With
End Sub
Results:

VBA Excel Automatically Copy & Paste Specific cells based on IF statement

The code I have placed below is a combination of what works and what i can't get to work.
The code that is not commented will copy cells to "sheet2" from "sheet1".
What I cannot get to work correctly is the code that I have disabled that would replace my Range Method of coping from "sheet1" to "sheet2".
Also my If Then Code is what will some up what I'm trying to accomplish. I'm trying to get the If statement to search all of column A and copy all Cars that are year 1991 to sheet2.
Mind my poor coding skills I'm Doing my best to show & explain so I can be helped.
Here is the Sheets 1 & 2
(hxxp://s15.postimg.org/orfw7tlaz/test.jpg)
OLD CODE
Sub Macro1()
Set a = Sheets("Sheet1")
Set b = Sheets("Sheet2")
Set c = Sheets("Sheet3")
Dim x
Dim z
Dim lastrow As Long, erow As Long
x = x + 1
z = 2
'lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
'lastrow = b.Cells(Rows.Count, 1).End(xlUp).Row
'For i = 2 To lastrow
lastrow = b.Range("A" & Rows.Count).End(xlUp).Row + x
'If a.Cells(i, 1) = “1991” Then
'a.Cells(i, 1).Copy
'erow = b.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'b.Paste Destination:=b.Range.Cells(erow, 4)
Range("A" & z).Copy Destination:=b.Range("D" & lastrow)
'a.Cells(i, 1).Copy
'b.Paste Destination:=b.Range.Cells(erow, 1)
Range("B" & z).Copy Destination:=b.Range("A" & lastrow)
'a.Cells(i, 1).Copy
'b.Paste Destination:=b.Range.Cells(erow, 3)
Range("C" & z).Copy Destination:=b.Range("C" & lastrow)
'a.Cells(i, 1).Copy
'b.Paste Destination:=b.Range(erow, 2)
Range("D" & z).Copy Destination:=b.Range("B" & lastrow)
'End If
'Next i
Application.CutCopyMode = False
Sheet2.Columns().AutoFit
'b.Range("A1").Select
End Sub
So I added some Lines and Began changing the cell locations to reflect the format I need and now when I run the macro it only copys the very last line from Sheet1 to sheet2. I believe it has to do with the order of the way these cells are.
b.Cells(erow, 1) = a.Cells(i, 1)
b.Cells(erow, 2) = a.Cells(i, 2)
b.Cells(erow, 3) = a.Cells(i, 3)
b.Cells(erow, 4) = a.Cells(i, 4)
Changing these back fixes it so it copys all the cells but its not what I'm trying to do.
The Code I'm Trying to run is Below
NEW CODE Working Thanks to EntryLevel!
Sub TakeTwo()
Dim a As Worksheet
Dim b As Worksheet
Dim c As Worksheet
Set a = ThisWorkbook.Sheets("Sheet1")
Set b = ThisWorkbook.Sheets("Sheet2")
Set c = ThisWorkbook.Sheets("Sheet3")
Dim i As Long
Dim lastrowsheet1 As Long
Dim erow As Long
lastrowsheet1 = a.Cells(Rows.Count, 1).End(xlUp).Row
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For i = 2 To lastrowsheet1
If a.Cells(i, 1).Value = "AEM" Then
b.Cells(erow, 31) = a.Cells(i, 1) '<------When I modify these
b.Cells(erow, 6) = a.Cells(i, 4) '<------The copied cells
b.Cells(erow, 28) = a.Cells(i, 5) '<------Don't show up
b.Cells(erow, 26) = a.Cells(i, 6) '<------In Sheet2
b.Cells(erow, 46) = a.Cells(i, 11) '<------Only the last
b.Cells(erow, 29) = a.Cells(i, 14) '<------Line found Is copied to sheet2
erow = erow + 1
End If
Next i
Application.CutCopyMode = False
b.Columns.AutoFit
'b.Range("A1").Select
End Sub
Now Using Same Working Code But Different function Not Working
Sub TakeThree()
Dim a As Worksheet
Dim b As Worksheet
Dim c As Worksheet
Set a = ThisWorkbook.Sheets("Sheet1")
Set b = ThisWorkbook.Sheets("Sheet2")
Set c = ThisWorkbook.Sheets("Sheet3")
Dim i As Long
Dim lastrowsheet1 As Long
Dim erow As Long
lastrowsheet1 = c.Cells(Rows.Count, 1).End(xlUp).Row
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For i = 2 To lastrowsheet1
If c.Cells(i, 1).Value = b.Cells(erow, 6).Value Then 'If serial number is found from sheet2 column 6 in sheet3 Column 1
b.Cells(erow, 8) = c.Cells(i, 2) 'Then copy description from sheet3 cell row to Sheet2 cell row
erow = erow + 1
End If
Next i
Application.CutCopyMode = False
b.Columns.AutoFit
c.Columns.AutoFit
'b.Range("A1").Select
End Sub
So I added another For Loop with Dim r and added another Line erow = erow + r & now the code copys the first 2 rows needed but does not continue iterating down the list. which is confusing me. here is the code below i have added.
Dim r As Long
Dim i As Long
Dim lastrowsheet1 As Long
Dim erow As Long
lastrowsheet1 = c.Cells(Rows.Count, 1).End(xlUp).Row
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For r = 1 To erow
For i = 2 To lastrowsheet1
If c.Cells(i, 1) = b.Cells(erow, 6) Then
b.Cells(erow, 8) = c.Cells(i, 2)
erow = erow + r
End If
Debug.Print i
Next i
Next r
Based on your statement that I'm trying to get the If statement to search all of column A and copy all Cars that are year 1991 to sheet2, it seems like Autofilter might be an easier solution than looping. You should be able to use something like this:
Sub TestyTestTest()
Dim lastrowsheet1 As Long
Dim lastrowsheet2 As Long
With ThisWorkbook.Sheets("Sheet1")
.AutoFilterMode = False
lastrowsheet1 = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(1, 1), .Cells(lastrowsheet1, 4)).AutoFilter Field:=1, Criteria1:="1991"
.Range(.Cells(2, 1), .Cells(lastrowsheet1, 4)).SpecialCells(xlCellTypeVisible).Copy
.AutoFilterMode = False
End With
With ThisWorkbook.Sheets("Sheet2")
.AutoFilterMode = False
lastrowsheet2 = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(lastrowsheet2 + 1, 1).PasteSpecial Paste:=xlPasteValues
.AutoFilterMode = False
End With
Application.CutCopyMode = False
End Sub
EDIT:
Trying to stick close to your original code, would something like this be more like what you are looking for?
Sub TakeTwo()
Dim a As Worksheet
Dim b As Worksheet
Dim c As Worksheet
Set a = ThisWorkbook.Sheets("Sheet1")
Set b = ThisWorkbook.Sheets("Sheet2")
Set c = ThisWorkbook.Sheets("Sheet3")
Dim i As Long
Dim lastrowsheet1 As Long
Dim erow As Long
lastrowsheet1 = a.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrowsheet1
If a.Cells(i, 1).Value = 1991 Then
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
b.Cells(erow, 1) = a.Cells(i, 1)
b.Cells(erow, 2) = a.Cells(i, 2)
b.Cells(erow, 3) = a.Cells(i, 3)
b.Cells(erow, 4) = a.Cells(i, 4)
End If
Next i
Application.CutCopyMode = False
b.Columns.AutoFit
End Sub
SECOND EDIT - OP's NEW PROBLEM:
It looks like your data is just pasting over itself because erow is defined as the row after the last row in column 1 that is not empty, but you are not actually putting any data into that column, so erow isn't moving down to the next line.
Basically, change the column number in this line:
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
The 1 in b.Cells(b.Rows.Count, 1) should be changed to a column number that you paste data into every time. Alternatively, you could use erow as a counter and increment it manually each time through the loop. In that case move the existing line that defines erow up underneath the line that defines lastrowsheet1 and then put erow = erow + 1 inside the loop after all the pasting has taken place but before End if. If you put it after End If, you'll end up with a bunch of blank lines between your data.

Resources