I need help with my if statements. In the code show below: The first if ... then section works just as intended, however the second section creates a run time error "13". Can someone please explain me how to fix this issue? / what i'm doing wrong?
Sub ASN_BaaN3()
Dim i As Integer
i = 0
ThisWorkbook.Sheets("BaaN").Activate
Do While ThisWorkbook.Sheets("BaaN").Cells(2 + i, 1) <> ""
'CHECK NON SERIALIZED
If Range("J" & 2 + i).Value = "N" Then
Range("P" & 2 + i).Value = "Ok, Non Serialized"
Range("P" & 2 + i).EntireRow.Interior.Color = RGB(198, 239, 206)
End If
If Range("J" & 2 + i).Value = "Y" And _
Range("M" & 2 + i).Value = "ACK" And _
Range("o" & 2 + i).Value = "TRUE" Then
Range("P" & 2 + i).Value = "Ok, Non Serialized"
Range("P" & 2 + i).EntireRow.Interior.Color = RGB(198, 239, 206)
End If
i = i + 1
Loop
End Sub
when typing the code like this i get the same error:
If Range("J" & 2 + i).Value = "Y" Then
If Range("M" & 2 + i).Value = "ACK" Then
If Range("O" & 2 + i).Value = "TRUE" Then
Range("P" & 2 + i).Value = "Ok, Non Serialized"
Range("P" & 2 + i).EntireRow.Interior.Color = RGB(198, 239, 206)
End If
End If
End If
Also when typing the code like this
If Range("J" & 2 + i).Value = "Y" Then
If Range("M" & 2 + i).Value = "Y" Then
Please help!
Related
We have a large excel spreadsheet that has been in use for a view years now, the original coder for it has moved on. Today we started to get an a runtime error "Compile error, Syntax error" highlighting the below code
For n = 7 To lastpivotrow
If Range("AA" & n).Value <> "" And lastpivotrow < 50 Then
Range("B" & nextentry).Value = Range("AB" & n).Value
Range("C" & nextentry).Value = Range("AC" & n).Value
Range("D" & nextentry).Value = Range("AD" & n).Value
Range("E" & nextentry).Value = Range("AE" & n).Value
Range("B" & nextentry, "E" & nextentry).BorderAround LineStyle:=xlContinuous
Range("B" & nextentry, "E" & nextentry).Borders(xlInsideHorizontal).LineStyle = xlContinuous
Range("B" & nextentry, "E" & nextentry).Borders(xlInsideVertical).LineStyle = xlContinuous
Range("B" & nextentr+hen Range("B" & nextentry, "E" & nextentry).Interior.ColorIndex = 39
nextentry = nextentry + 1
Can anyone see whats wrong with this and provide feedback?
I have a macro that runs on cell changes but every time I want to run it it keeps giving me "Next without for" or "double declaration within given range"
Here's the code:
Dim Lvl As Range
Set Lvl = Range("A5:A44")
Dim Full, Medium, Poor
Dim r As Long
For r = 5 To 44
Full = StrComp(Range("A" & r), Data.Range("A2"), 0)
Medium = StrComp(Range("A" & r), Data.Range("A3"), 0)
Poor = StrComp(Range("A" & r), Data.Range("A4"), 0)
If Not Intersect(Target, Lvl) Is Nothing Then
Dim i As Integer
For i = r To 44
If Full = 0 Then
Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * 1), 0)
ElseIf Medium = 0 Then
Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * (3 / 4)), 0)
ElseIf Poor = 0 Then
Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * (1 / 2)), 0)
Else
Range("I" & r).Value = 0
End If
If Not Range("A" & r) = Range("A" & r).Offset(-1, 0) And Not Range("A" & r) = A5 Then
If Range("A" & r).Offset(-1, 0) = Full And Range("A" & r) = Medium Then
Range("I" & i).Value = Range("I" & i).Value + 1
ElseIf Range("A" & r).Offset(-1, 0) = Medium And Range("A" & r) = Poor Then
Range("I" & i).Value = Range("I" & i).Value + 1
ElseIf Range("A" & r).Offset(-1, 0) = Full And Range("A" & r) = Poor Then
Range("I" & i).Value = Range("I" & i).Value + 2
End If
End If
Next
End If
Next
The Double Declaration Error I am getting probably has to do with the fact that I am using r = 5 To 44 AND i = r To 44
But I needed a way to get the Range("I" & i).value to work
There is an End If missing right before Next
Dim Lvl As Range
Set Lvl = Range("A5:A44")
Dim Full, Medium, Poor
r = 5
Full = StrComp(Range("A" & r), Data.Range("A2"), 0)
Medium = StrComp(Range("A" & r), Data.Range("A3"), 0)
Poor = StrComp(Range("A" & r), Data.Range("A4"), 0)
If Not Intersect(Target, Lvl) Is Nothing Then
Dim i As Integer
For i = r To 44
If Full = 0 Then
Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * 1), 0)
ElseIf Medium = 0 Then
Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * (3 / 4)), 0)
ElseIf Poor = 0 Then
Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * (1 / 2)), 0)
Else
Range("I" & r).Value = 0
End If
If Not Range("A" & r) = Range("A" & r).Offset(-1, 0) And Not Range("A" & r) = A5 Then
'^^-- this one is not closed by End If
If Range("A" & r).Offset(-1, 0) = Full And Range("A" & r) = Medium Then
'^^-- this is AAA
Range("I" & i).Value = Range("I" & i).Value + 1
ElseIf Range("A" & r).Offset(-1, 0) = Medium And Range("A" & r) = Poor Then
Range("I" & i).Value = Range("I" & i).Value + 1
ElseIf Range("A" & r).Offset(-1, 0) = Full And Range("A" & r) = Poor Then
Range("I" & i).Value = Range("I" & i).Value + 2
End If '<-- this closes the one I marked with AAA
End If '<-- missing here !!!
Next
End If
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:
I know a little bit about VBA but I can't seem to be able to work my way around this programming question.
I have a sheet where I want to program how many days it will take before the end of the tasking. Each Status are equal to a number of days, for exemple if a file is at the Pending stage it will take 180 total to be completed. But what i want is at each stage to write the number of days it will take. For example
Status is written in range E3:E160
If cell in range= Pending then
Offset 4 columns over and write 20, and offset 5 columns over and write 35 and offset 6 columns over and write 50, and offset 7 columns over and write 25, and offaet 8 columns over and write 15 and offset 9 columns over and write 15 and finally, offset 10 columns over and write 20
However if cell in range = "Planning" then offset 5 columns over and write 35, and offset 6 columns over and write 50 and so on until offset 10 columns over and write 20
The goal is tha for each status, the number of offset is based on the status.
Hope this help
I'm assuming ther will be a loop or something but I really can't figure it out.
Also it needs to be able to capture any new rows inserted within the range or outside the range.
Thanks to anyone who will be able to help me
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim LastRow As Long
Dim i As Long
LastRow = Range("E" & Rows.Count).End(xlUp).Row
For i = 3 To LastRow
If Range("E" & i).Value = "Pending" Then
Range("I" & i).Value = "20" And Range("J" & i).Value = 35 And Range("K" & i).Value = 50 And Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Planning" Then
Range("J" & i).Value = 35 And Range("K" & i).Value = 50 And Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Screening" Then
Range("K" & i).Value = 50 And Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Exam" Then
Range("L" & i).Value = 25 And Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Interview" Then
Range("M" & i).Value = 15 And Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "References" Then
Range("N" & i).Value = 15 And Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Closing" Then
Range("O" & i).Value = 20
End If
Next i
End Sub
If Range("E" & i).Value = "Pending" Then
Range("I" & i).Value = 20
Range("J" & i).Value = 35
Range("K" & i).Value = 50
Range("L" & i).Value = 25
Range("M" & i).Value = 15
Range("N" & i).Value = 15
Range("O" & i).Value = 20
ElseIf Range("E" & i).Value = "Planning" Then
Range("J" & i).Value = 35
Range("K" & i).Value = 50
Range("L" & i).Value = 25
Range("M" & i).Value = 15
Range("N" & i).Value = 15
Range("O" & i).Value = 20`
You need to get rid of all the And statements in your Then clause. This is an example. You can change the rest. You might want to look into the Case Select method as well.
I am relatively new to vba macros and have learned to get around, but am not very good at debugging or troubleshooting problems.
I have this macro that is exiting on the line:
.Range("B" & y_lst).Resize(34, 10).Insert Shift:=xlDown
The macro works with 2 workbooks. 'wscflow' is a sheet on the inactive open workbook 'wbsource' that are declared globally. Fluid_Results is a routine in the active workbook, but another module. 'wsactive' is the sheet the macro accesses and is set in the routine that calls Read_Complex. 'Read_Pipe' is a routine in this module.
Can anyone tell me why this line would cause the macro to exit?
I should clarify; the line executes, but exits immediately afterward, it does not execute the next line.
Regards,
arthur
Sub Read_Complex()
Dim x_lst As Integer, y_lst As Integer, z_lst As Integer, i As Integer
wscflow.Range("T7").Value = 10
Application.Run ("'" & wbsource.Name & "'!Set_Rng")
x_lst = wscflow.Range("S7").Value
z_lst = wsactive.Range("B" & Rows.Count).End(xlUp).Row - 23
z_lst = ((z_lst - 24) + 330) / 34
If wscflow.Range("O" & x_lst).Value = "Total:" Then
x_lst = x_lst - 2
End If
Call Fluid_Results
If x_lst = 10 Then
MsgBox ("Only one pipe," & vbCr & _
"please use Single Pipe Flow")
Else
For x = 10 To x_lst
y_lst = wsactive.Range("B" & Rows.Count).End(xlUp).Row - 22
wscflow.Range("T7").Value = x
Application.Run ("'" & wbsource.Name & "'!Set_Rng")
Application.Run ("'" & wbsource.Name & "'!Rev_Copy")
Application.Run ("'" & wbsource.Name & "'!Fwd_Copy")
With wsactive
If x < 12 Then
y_lst = y_lst - 1
Call Read_Pipe
If .Name = "Series_Pipe_Flow" Then
.Range("F" & y_lst).Offset(8, 0).Formula = "=F14+F48"
For i = 17 To 20
.Range("F" & y_lst).Offset(i, 0).Formula = "=F" & i + 7 & "+F" & i + 41
Next i
For i = 22 To 23
.Range("F" & y_lst).Offset(i, 0).Formula = "=F" & i + 11 & "+F" & i + 45
Next i
ElseIf .Name = "Parallel_Pipe_Flow" Then
.Range("F" & y_lst).Offset(15, 0).Formula = "=F22+F56"
.Range("F" & y_lst).Offset(19, 0).Formula = "=F27+F61"
.Range("F" & y_lst).Offset(22, 0).Formula = "=F33+F67"
.Range("F" & y_lst).Offset(23, 0).Formula = "=F34+F68"
End If
Else
If x_lst = z_lst Then
y_lst = y_lst - 1
Else
.Range("B35:K68").Copy
.Range("B" & y_lst).Resize(34, 10).Insert Shift:=xlDown
' .Range("B" & y_lst).PasteSpecial xlPasteAll
y_lst = y_lst + 33
Application.CutCopyMode = False
End If
Call Read_Pipe
If .Name = "Series_Pipe_Flow" Then
.Range("F" & y_lst).Offset(8, 0).Value = .Range("F" & y_lst).Offset(8, 0).Value _
+ .Range("F" & y_lst).Offset(-20, 0).Value
.Range("F" & y_lst).Offset(11, 0).Value = .Range("F" & y_lst).Offset(-17, 0).Value
.Range("F" & y_lst).Offset(14, 0).Value = .Range("F" & y_lst).Offset(-14, 0).Value
For i = 17 To 20
.Range("F" & y_lst).Offset(i, 0).Value = .Range("F" & y_lst).Offset(i, 0).Value _
+ .Range("F" & y_lst).Offset(i - 27, 0).Value
Next i
For i = 22 To 23
.Range("F" & y_lst).Offset(i, 0).Value = .Range("F" & y_lst).Offset(i, 0).Value _
+ .Range("F" & y_lst).Offset(i - 23, 0).Value
Next i
ElseIf .Name = "Parallel_Pipe_Flow" Then
.Range("F" & y_lst).Offset(15, 0).Value = .Range("F" & y_lst).Offset(15, 0).Value _
+ .Range("F" & y_lst).Offset(-12, 0).Value
.Range("F" & y_lst).Offset(19, 0).Value = .Range("F" & y_lst).Offset(19, 0).Value _
+ .Range("F" & y_lst).Offset(-7, 0).Value
.Range("F" & y_lst).Offset(22, 0).Value = .Range("F" & y_lst).Offset(22, 0).Value _
+ .Range("F" & y_lst).Offset(-1, 0).Value
.Range("F" & y_lst).Offset(23, 0).Value = .Range("F" & y_lst).Offset(23, 0).Value _
+ .Range("F" & y_lst).Offset(0, 0).Value
End If
End If
End With
Next x
End If
z_lst = wsactive.Range("B" & Rows.Count).End(xlUp).Row - 23
z_lst = ((z_lst - 24) + 330) / 34
For i = 0 To z_lst - x_lst + 1
wsactive.Range("B" & y_lst + 1).Offset(-i * 34, 0).PageBreak = xlPageBreakManual
Next i
wscflow.Range("T7").ClearContents
End Sub
I think you may have a size difference between the copy and the insert. The resize function has 34 but the next one increase the variable by 33. I think the 34 needs to be 33.
If you print your variables to the debug window you'll e able to see what they are, e.g. debug.print "y_1st = " & y_1st.
Also for a visual add a .Range("B" & y_lst).Resize(34, 10).Select and break the code the next line after it to see if it has the correct size in excel.