Excel VBA that uses nested If statements for multiple worksheets - excel

I'm trying to get an output in column U for multiple worksheets in a workbook. The return string will either be "Yes" or "No" depending on which column is not blank and if the difference between two dates are > 150. This is the code I have written, but nothing shows up in column U. Could anyone help me figure out why this isn't working?
Sub Compliance()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim i As Integer
Dim listLength
listLength = ws.Cells(Rows.Count, "M").End(xlUp).Row - 1
For i = 2 To listLength + 2
If IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And IsEmpty(ws.Range("N" & i)) = True And DateDiff("d", ws.Range("M" & i), ws.Range("K" & i)) > 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And DateDiff("d", ws.Range("O" & i), ws.Range("N" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
Else
ws.Range("U" & i) = "No"
End If
Next
Next ws
End Sub

When you start your loop with For Each ws In ThisWorkbook.Worksheets then Excel starts processing from Sheet1 and I'm sure your list in another sheet.
So, the reason nothing changes in your U column is, because you are
looking at wrong Worksheet.
As long as you would like to loop through all of your worksheets, start to follow your lists from Sheet1. If you would like to loop in different order, you should define this in your code.
And below I edited some part of your code and also added msgbox ws.name to show you in which sheet Excel is working now.
Why Use Integer Instead of Long?
Option Explicit
Sub Compliance()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim i As Long
Dim listLength
MsgBox ws.Name
listLength = ws.Cells(ws.Rows.Count, "M").End(xlUp).Row - 1
For i = 2 To listLength + 2
If IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And IsEmpty(ws.Range("N" & i)) = True And DateDiff("d", ws.Range("M" & i), ws.Range("K" & i)) > 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And DateDiff("d", ws.Range("O" & i), ws.Range("N" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
Else
ws.Range("U" & i) = "No"
End If
Next
Next ws
End Sub

Related

VBA - IFELSE and Continuous Looping

I want to have 2 formulas with continuous looping as long as there is value in the cell next to the targeted cell, thus i need to have ifelse function but with continuous looping aswell. for now i don't know how to insert the second formula.
Range("D9").Select
Set ws = Sheets("LAP KEL BIAYA")
lastRow = ws.Range("C" & Rows.Count).End(xlUp).Row
With ws
For i = 9 To lastRow
If Len(Trim(.Range("C" & i).Value)) <> 0 Then
Function Total(Text, Number)
.Range("D" & i).Formula = "=IF(RC[-3]=""B"",IF(AND(R4C3>0,R5C3>0),SUMIFS(FBL3N!C[11],FBL3N!C[21],R4C3,FBL3N!C[22],R5C3,FBL3N!C[16],R7C4,FBL3N!C[47],RC[-1]),IF(AND(R4C3>0,R5C3=""""),SUMIFS(FBL3N!C[11],FBL3N!C[21],R4C3,FBL3N!C[16],R7C4,FBL3N!C[47],'LAP KEL BIAYA'!RC[-1]),IF(AND(R4C3=0,R5C3>0),SUMIFS(FBL3N!C[11],FBL3N!C[22],R5C3,FBL3N!C[16],R7C4,FBL3N!C[47],RC[-1]),IF(AND(R4C3=0,R5C3=0),SUMIFS(F" & _
"BL3N!C[11],FBL3N!C[16],R7C4,FBL3N!C[47],RC[-1]),"""")))))" & _
""
ElseIf Total = "False" Then
If Len(Trim(.Range("C" & i).Value)) <> 0 Then
.Range("D" & i).Formula = "=IF(AND(R4C3>0,R5C3>0,OR(R[-1]C1=""7"",R[-1]C1=""5"",R[-1]C1=""4"")),SUMIFS(FBL3N!C[11],FBL3N!C[21],R4C3,FBL3N!C[22],R5C3,FBL3N!C[16],R7C4,FBL3N!C,R[-1]C[-1]),IF(AND(R4C3>0,R5C3=0),SUMIFS(FBL3N!C[11],FBL3N!C[21],R4C3,FBL3N!C[16],R7C4,FBL3N!C,'LAP KEL BIAYA'!R[-1]C[-1]),IF(AND(R4C3=0,R5C3>0),SUMIFS(FBL3N!C[11],FBL3N!C[22],R5C3,FBL3N!C[16],R7C4,FBL3N!C,R[-1]C[-1]),IF(" & _
"AND(R4C3=0,R5C3=0),SUMIFS(FBL3N!C[11],FBL3N!C[16],R7C4,FBL3N!C,R[-1]C[-1])))))" & _
""
.Range("D" & i).Font.Color = vbRed
End If
Next i
End Function
End With

VBA, Dealing with E-10

I have cells that are supposed to be 0 I believe. 7.45058059692383E-12.
How do I make these 0 in my code?
Sheets("MainData").Range("C" & i) = Sheets("CM_MainData").Range("C" & i)
I thought converting NumberFormat to 0 may help but it did not work.
Sheets("MainData").Range("C" & i) = Sheets("CM_MainData").Range("C" & i).NumberFormat = "0.00"
Instead the result is FALSE
Please Help!
Full code:
Sub CopyData()
Worksheets("MainData").Rows("2:" & Rows.Count).ClearContents
'Copy data from the CM Commentary File to Template
Application.ScreenUpdating = False
Set MainDataCM = Workbooks.Open(Sheets("Input").Range("B3") & Sheets("Input").Range("B6"))
MainDataCM.Sheets("Main Data").Copy After:=ThisWorkbook.Sheets(1)
MainDataCM.Close savechanges:=False
Application.ScreenUpdating = True
Sheets("Main Data").Name = "CM_MainData"
Worksheets("CM_MainData").Visible = False
'Read the CM_MainData tab and copy the required columns in the MainData tab
Dim k As Long
k = Sheets("CM_MainData").Range("A1", Sheets("CM_MainData").Range("A1").End(xlDown)).Rows.Count
Debug.Print (k)
i = 2
While i <= k
Sheets("MainData").Range("A" & i) = Sheets("CM_MainData").Range("A" & i)
Sheets("MainData").Range("B" & i) = Sheets("CM_MainData").Range("B" & i)
Sheets("MainData").Range("C" & i) = Sheets("CM_MainData").Range("C" & i).NumberFormat = "0.00"
Sheets("MainData").Range("D" & i) = Sheets("CM_MainData").Range("D" & i)
Sheets("MainData").Range("E" & i) = Sheets("CM_MainData").Range("C" & i) * 1000
Sheets("MainData").Range("F" & i) = Sheets("CM_MainData").Range("H" & i)
'Sheets("MainData").Range("E" & i).NumberFormat = "0.00"
If Sheets("MainData").Range("F" & i) = "" Then
Sheets("MainData").Range("F" & i) = "RBC INVESTOR SERV O/H & MISC"
End If
i = i + 1
Wend
Worksheets("Macro").Activate
Worksheets("Macro").Select
MsgBox "Step 1 Completed"
End Sub
Value transfer:
Sheets("MainData").Range("C" & i).Value = Sheets("CM_MainData").Range("C" & i).Value
Number format (a separate step, and does not change the underlying value):
Sheets("MainData").Range("C" & i).NumberFormat = "0.00"
Or use WorksheetFunction.Round (does change the underlying value).
Sheets("MainData").Range("C" & i).Value = WorksheetFunction.Round(Sheets("CM_MainData").Range("C" & i).Value, 2)

Built MS Project from Excel skipping empty rows

This question is not that much about any application but more about finding algorithm that would work.
I have a code for moving data from Excel to MS Project. My current solution works until I don't have empty rows in between.
I have data starting from row 19. So 19 - 18 is first task, 20 - 18 is second task and so on. However once I have empty rows in between, let's say:
Task 1 (row 19)
Task 2 (row 20)
Task 3 (row 22)
My code is not working as there is task 2 in MS Project and next should be number 3, however 22 - 18 is 4. Anybody have any good solution for this?
Here is the code:
' Move data to project
For i = 19 To lRow
strValue = WorksheetToOperate.Range("C" & i)
strStartDate = WorksheetToOperate.Range("E" & i)
strEndDate = WorksheetToOperate.Range("F" & i)
Strresource = WorksheetToOperate.Range("J" & i)
' Import tasks
If (WorksheetToOperate.Range("C" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("C" & i).Value)) Then
newproj.Tasks.Add strValue
End If
' Import start date
If (WorksheetToOperate.Range("E" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("E" & i).Value)) Then
newproj.Tasks(i - 18).Start = strStartDate
End If
' Import end date
If (WorksheetToOperate.Range("F" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("F" & i).Value)) Then
newproj.Tasks(i - 18).Finish = strEndDate
End If
' Import recources
If Not ExistsInCollection(newproj.Resources, Strresource) Then _
newproj.Resources.Add.Name = Strresource
If (WorksheetToOperate.Range("J" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("J" & i).Value)) Then
newproj.Tasks(i - 18).ResourceNames = Strresource
End If
Next i
The problem of skipping empty rows is greatly simplified by using a Task object variable to keep track of the task that was just added. Also, use the With statement with the Worksheet object to further simplify the code.
Sub CreateSchedule()
Dim prj As MSProject.Application
Set prj = CreateObject("MSProject.Application")
prj.Visible = True
Dim newProj As MSProject.Project
Set newProj = prj.Projects.Add
Dim i As Long
Dim t As MSProject.Task
For i = 19 To 28 'lRow
With WorksheetToOperate
If Not IsEmpty(.Range("C" & i)) Then
Set t = newProj.Tasks.Add(CStr(.Range("C" & i)))
t.Start = CDate(.Range("E" & i))
t.Finish = CDate(Range("F" & i))
t.ResourceNames = CStr(.Range("J" & i))
End If
End With
Next i
End Sub
I have managed to get it working with additional variable m:
Dim m
m = 0
' Move data to project
For i = 19 To lRow
If IsEmpty(WorksheetToOperate.Range("C" & i).Value) Then
m = m + 1
Else
strValue = WorksheetToOperate.Range("C" & i)
strStartDate = WorksheetToOperate.Range("E" & i)
strEndDate = WorksheetToOperate.Range("F" & i)
Strresource = WorksheetToOperate.Range("J" & i)
' Import tasks
If (WorksheetToOperate.Range("C" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("C" & i).Value)) Then
newproj.Tasks.Add strValue
End If
' Import start date
If (WorksheetToOperate.Range("E" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("E" & i).Value)) Then
newproj.Tasks(i - (18 + m)).Start = strStartDate
End If
' Import end date
If (WorksheetToOperate.Range("F" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("F" & i).Value)) Then
newproj.Tasks(i - (18 + m)).Finish = strEndDate
End If
' Import recources
If Not ExistsInCollection(newproj.Resources, Strresource) Then _
newproj.Resources.Add.Name = Strresource
If (WorksheetToOperate.Range("J" & i).Value <> "") And _
(Not IsError(WorksheetToOperate.Range("J" & i).Value)) Then
newproj.Tasks(i - (18 + m)).ResourceNames = Strresource
End If
End If
Next i

Loop inside loop using multiple variables

I am having trouble making a program work in Excel.
I need loop through all the lines in my excel spreadsheet, and loop through multiple possible cells for each line.
Sub DoubleLoop()
Dim i As Long
Dim Carr As Integer
For i = 2 To 49235
For j = 2 To 27
If Range("P" & i).Value = ("Y" & j) And Range("S" & i).Value = ("Z" & j) And Range("P" & i).Value = ("AA" & j) Then
Range("P" & i).Value = "Keep"
ElseIf j < 27 Then
j = j + 1
ElseIf j = 27 Then
Range("X" & i).Value = "Remove"
End If
Next i
End Sub
As stated in the comments. Use Next j and exit the inner loop when criteria is met.
Sub DoubleLoop()
Dim i As Long, j As Long
Dim Carr As Integer
For i = 2 To 49235
Range("P" & i).Value = "Remove"
For j = 2 To 27
If Range("P" & i).Value = ("Y" & j) And Range("S" & i).Value = ("Z" & j) And Range("P" & i).Value = ("AA" & j) Then
Range("P" & i).Value = "Keep"
Exit For
End If
Next j
Next i
End Sub

Inserting blank rows in VBA excel

Final One:enter image description hereI want to insert blank row with a specific column range above a particular row.
For example:
There were 2 sets of data in a single sheet ,ie, 1st set col A to Col E and 2nd set Col F to Col J. I need to compare Column Ai with Column Fi (where i indicates the position of row) and if both values are same then the comparison can be proceeded like Bi with Gi, Ci with Hi and so and so and if not, I need to shift that set of 2nd data Fi to Ji to next row..ie. if the whole set is in 6th position I need to shift them down to 7th position and make the 6th position of Fi to Ji blank....
Sub Dcompare()
Dim endRow As Long
Dim lRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
endRow = Sheet1.Range("A999999").End(xlUp).Row
For i = 2 To endRow
If Sheet1.Range("A" & i).Value = Sheet1.Range("F" & i).Value Then
Sheet1.Range("K" & i).Value = "Yes"
Else
ws.Range("F" & i & ":J" & i).Offset(1, 0).Value = ws.Range("F" & i & ":J" & i).Value
ws.Range("F" & i & ":J" & i).Value = ""
End If
Next i
For j = 2 To endRow
If Sheet1.Range("K" & j).Value = "Yes" Then
If Sheet1.Range("B" & j).Value = Sheet1.Range("G" & j).Value Then
Sheet1.Range("L" & j).Value = "Yes"
Else
Sheet1.Range("L" & j).Value = "No"
End If
If Sheet1.Range("C" & j).Value = Sheet1.Range("H" & j).Value Then
Sheet1.Range("M" & j).Value = "Yes"
Else
Sheet1.Range("M" & j).Value = "No"
End If
If Sheet1.Range("D" & j).Value = Sheet1.Range("I" & j).Value Then
Sheet1.Range("N" & j).Value = "Yes"
Else
Sheet1.Range("N" & j).Value = "No"
End If
If Sheet1.Range("E" & j).Value = Sheet1.Range("J" & j).Value Then
Sheet1.Range("O" & j).Value = "Yes"
Else
Sheet1.Range("O" & j).Value = "No"
End If
End If
Next j
End Sub
------>Final Code Inserted---------
Sub Dcompare()
Dim endRow As Long
Dim ws As Worksheet
Dim dShift As Boolean
Set ws = ThisWorkbook.Worksheets(1)
endRow = ws.Range("A999999").End(xlUp).Row
For i = 2 To endRow + 1
If ws.Range("A" & i).Value = ws.Range("F" & i).Value Then
dShift = False
ws.Range("K" & i).Value = "Yes"
Else
If Not dShift Then
ws.Range("F" & i & ":J" & i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ws.Range("A" & i + 1 & ":E" & i + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
endRow = endRow + 1
dShift = True
Else
dShift = False
End If
End If
j = i
If ws.Range("K" & j).Value = "Yes" Then
If ws.Range("B" & j).Value = ws.Range("G" & j).Value Then
ws.Range("L" & j).Value = "Yes"
Else
ws.Range("L" & j).Value = "No"
End If
If ws.Range("C" & j).Value = ws.Range("H" & j).Value Then
ws.Range("M" & j).Value = "Yes"
Else
ws.Range("M" & j).Value = "No"
End If
If ws.Range("D" & j).Value = ws.Range("I" & j).Value Then
ws.Range("N" & j).Value = "Yes"
Else
ws.Range("N" & j).Value = "No"
End If
If ws.Range("E" & j).Value = ws.Range("J" & j).Value Then
ws.Range("O" & j).Value = "Yes"
Else
ws.Range("O" & j).Value = "No"
End If
Else
End If
Next i
MsgBox "The value of endRow is : " & endRow, vbInformation
End Sub
Based on your explanations, this is what I interpret your challenge as:
Evaluate Ai with Fi --> Ei with Ji from left to right, and indicate in helper-columns whether the evaluation succeeded or not
If the first evaluation is Not Equal, offset the range Fi:Ji downwards exactly one row
If a range has been shifted down, the loop should evaluate this line but never shift it again regardless of outcome of the evaluation
This code satisfies those conditions (change i and other row variables to your needs):
Sub Dcompare()
Dim endRow As Long
Dim ws As Worksheet
Dim dShift As Boolean
Set ws = ThisWorkbook.Worksheets(1)
endRow = ws.Range("A999999").End(xlUp).Row
' Set initial value of helper columns to no - saves miniscule time and complexity in the loop
ws.Range("L" & 1 & ":O" & endRow).Value = "No"
For i = 1 To endRow
If ws.Range("A" & i).Value = ws.Range("F" & i).Value Then
dShift = False
ws.Range("L" & i).Value = "Yes"
Else
If Not dShift Then
ws.Range("F" & i & ":J" & i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
' Remember that we just shifted a row
dShift = True
Else
' Reset shift counter
dShift = False
End If
End If
For j = 2 To 4
If dShift Then Exit For
If ws.Cells(i, j).Value = ws.Cells(i, j + 5).Value Then ws.Cells(i, j + 11).Value = "Yes"
Next j
Next i
End Sub
However, it seems strange to me that you would want this functionality? Please confirm that it is correct. The behavior it yields in the worksheet is very strange.
Let me show with images. Orange background means the code will show the cell as a match. Green background means the code will show that the cell doesn't match.
Before the code it looks like this:
After the code it looks like this:

Resources