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.
Related
This is looping through a worksheet that is about 10k rows and it is taking a considerable amount of time. Is there a way to do this faster aside from an array? thank you
For i = 2 To spberowcnt
With spbe30
.Range("b" & i).Value = LCase(.Range("b" & i).Value)
.Range("d" & i).Value = LCase(.Range("d" & i).Value)
.Range("i" & i).Value = LCase(.Range("i" & i).Value)
.Range("j" & i).Value = LCase(.Range("j" & i).Value)
.Range("l" & i).Value = LCase(.Range("l" & i).Value)
.Range("m" & i).Value = LCase(.Range("m" & i).Value)
.Range("n" & i).Value = LCase(.Range("n" & i).Value)
.Range("p" & i).Value = LCase(.Range("p" & i).Value)
.Range("q" & i).Value = LCase(.Range("q" & i).Value)
.Range("r" & i).Value = LCase(.Range("r" & i).Value)
.Range("z" & i).Value = LCase(.Range("z" & i).Value)
.Range("aa" & i).Value = LCase(.Range("aa" & i).Value)
End With
Next i
For i = 2 To spberowcnt
With spbe60
.Range("b" & i).Value = LCase(.Range("b" & i).Value)
.Range("d" & i).Value = LCase(.Range("d" & i).Value)
.Range("i" & i).Value = LCase(.Range("i" & i).Value)
.Range("j" & i).Value = LCase(.Range("j" & i).Value)
.Range("l" & i).Value = LCase(.Range("l" & i).Value)
.Range("m" & i).Value = LCase(.Range("m" & i).Value)
.Range("n" & i).Value = LCase(.Range("n" & i).Value)
.Range("p" & i).Value = LCase(.Range("p" & i).Value)
.Range("q" & i).Value = LCase(.Range("q" & i).Value)
.Range("r" & i).Value = LCase(.Range("r" & i).Value)
.Range("z" & i).Value = LCase(.Range("z" & i).Value)
.Range("aa" & i).Value = LCase(.Range("aa" & i).Value)
End With
Next i
This is the array solution
Sub test()
Application.ScreenUpdating = False
Dim arrWorksheets(1) As Variant, ws As Worksheet
Set arrWorksheets(0) = spbe30
Set arrWorksheets(1) = spbe60
Dim arrColumns As Variant
arrColumns = Array("B", "D", "AA") 'adjust to your needs
Dim arrValues As Variant
Dim iWs As Long, iC As Long, i As Long
For iWs = 0 To UBound(arrWorksheets)
Set ws = arrWorksheets(iWs)
For iC = 0 To UBound(arrColumns)
arrValues = ws.Range(arrColumns(iC) & "2").Resize(spberowcnt).Value
For i = 1 To UBound(arrValues, 1)
arrValues(i, 1) = LCase(arrValues(i, 1))
Next
ws.Range(arrColumns(iC) & "2").Resize(spberowcnt).Value = arrValues
Next
Next
Application.ScreenUpdating = True
End Sub
Alternative: helper columns ...
You could try something like the following, looping over the columns instead of the individual cells and using Evaluate and Lower on the entire column. You could also process adjacent columns together.
cols = Array("B", "D", "I:J", "L:N", "P:R", "Z:AA")
For i = LBound(cols) to Ubound(cols)
Dim col As String
col = cols(i)
With spbe30
Dim rng As Range
Set rng = .Rows("2:" & spberowcnt).Columns(col)
rng.Value = .Evaluate("LOWER(" & rng.Address & ")")
End With
Next
But as mentioned in comments, an array is probably the way to go.
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 am trying to import tasks from one sheet(WS) to the main tracker sheet (Sub Tasks),. The import sheet has milestones and then sub tasks within each Milestone.
My code imports the milestones then goes back and imports the sub tasks for each milestone. However, if the milestones being added have the same title as ones already in the Sub Task sheet, it will add the sub tasks to the inocrrect milestone. I am using Range.Find, i understand it would find the first match, which is not want i want all the time. So i want to add criteria in, so if a match if found, and the value in column H is equal to the value in the N9 in the worksheet, and then value in Sub Task Column I is equal to the value in value N10 in WS, then add.
If not, find next and test again. However, i cannot seem to get it to work.
UPDATED
If .Cells(findactivityintasks.row, TDSNumCol).Value = ImportWs.Range("N9").Value And .Cells(findactivityintasks.row, MileStoneNumCol).Value = DeliverableActivity Then
newdeliverablerow = (findactivityintasks.row)
Else
Do
Set findactivityintasks = .Range("B3:B" & lastrowsubtasks1).FindNext(findactivityintasks)
Loop Until .Cells(findactivityintasks.row, TDSNumCol).Value = ImportWs.Range("N9").Value And .Cells(findactivityintasks.row, MileStoneNumCol).Value = DeliverableActivity
newdeliverablerow = (findactivityintasks.row)
End If
ALL CODE FOR SUB
'Add Milestone to subtask sheets
For Each cell In ImportWs.Range("B" & activityStart & ":B" & activityend)
NewRowSubTasks = lastrowsubtasks + i
DeliverableActivity = Int(cell.Offset(0, -1).Value)
With subtaskws
.Range(SubTaskCol & NewRowSubTasks & ":" & lastcollet & NewRowSubTasks).Interior.ColorIndex = 16
.Range(SubTaskCol & NewRowSubTasks & ":" & lastcollet & NewRowSubTasks).Font.ColorIndex = 2
.Range(SubTaskCol & NewRowSubTasks & ":" & lastcollet & NewRowSubTasks).Font.Size = 12
.Range(IDCol & NewRowSubTasks).Interior.ColorIndex = 23
.Range(IDCol & NewRowSubTasks).Font.ColorIndex = 2
.Range(IDCol & NewRowSubTasks).Font.Size = 16
.Range(IDCol & NewRowSubTasks).NumberFormat = "0"
.Cells(NewRowSubTasks, SubTaskCol).Value = ImportWs.Range("B" & cell.row).Value
.Cells(NewRowSubTasks, IDCol).Value = Application.WorksheetFunction.RoundUp((subtaskws.Range("A" & NewRowSubTasks - 1).Value + 0.01), 0)
.Cells(NewRowSubTasks, TDSNumCol).Value = ImportWs.Range("N9").Value
.Cells(NewRowSubTasks, MileStoneNumCol).Value = DeliverableActivity
.Cells(NewRowSubTasks, BWLCol).Value = ImportWs.Range("L" & cell.row).Value
End With
i = i + 1
Next cell
'find start and end of deliverables
DeliverableStart = valuePos(ImportWs, "C:G", "Outputs / Deliverables") + 1
DeliverableEnd = valuePos(ImportWs, "A:G", "Tools / constraints") - 1
'find deliverables to add to Milestones and find what Milestones to add them too
For Each cell In ImportWs.Range("C" & DeliverableStart & ":C" & DeliverableEnd)
DeliverableActivity = Int(cell.Offset(0, -1).Value)
Set finddeliverableactivity = ImportWs.Range("A" & activityStart & ":A" & activityend).Find(What:=("# " & (DeliverableActivity + 1)), Lookat:=xlWhole)
If finddeliverableactivity Is Nothing Then
With subtaskws
Dim lastrowsubtasks1 As Long
lastrowsubtasks1 = subtaskws.Range("A" & Rows.Count).End(xlUp).row
newdeliverablerow = (lastrowsubtasks1 + 1)
.Range("A" & (newdeliverablerow)).EntireRow.Insert
newrow = newdeliverablerow
.Range("A4").EntireRow.Copy
.Range("A" & newrow).EntireRow.PasteSpecial xlPasteFormulasAndNumberFormats
.Range("A" & newrow).EntireRow.PasteSpecial xlPasteValidation
.Range("A" & newrow).EntireRow.PasteSpecial xlPasteAllMergingConditionalFormats
.Range("A" & newrow & ":AE" & newrow & "").ClearContents
.Columns("A:BB").Calculate
.Range(IDCol & newrow).Value = subtaskws.Range("A" & newrow).Offset(-1, 0).Value + 0.01
.Cells(newrow, SubTaskCol).Value = cell.Value
.Cells(newrow, FormatCol).Value = cell.Offset(0, 1).Value
.Cells(newrow, AcceptanceCriteriacol).Value = cell.Offset(0, 2).Value
.Cells(newrow, TargetDateCol).Value = cell.Offset(0, 9).Value
.Cells(newrow, BWLCol).Value = cell.Offset(0, 7).Value
.Cells(newrow, TDSNumCol).Value = ImportWs.Range("N9").Value
.Cells(newrow, MileStoneNumCol).Value = DeliverableActivity
.Cells(newrow, AWCol).Value = "=SUM(AF" & newrow & ":" & lastcollet & newrow & ")"
.Cells(newrow, PCol).Value = "=(" & WCol & newrow & "*" & ICol & newrow & "*" & ECol & newrow & ")"
.Cells(newrow, LevelCol).Value = "=IF(" & PCol & newrow & " >11,1,IF(" & PCol & newrow & ">3,2,""N/A""))"
.Range("A" & newrow).EntireRow.Hidden = False
End With
Exit Sub
Else
With subtaskws
lastrowsubtasks1 = subtaskws.Range("A" & Rows.Count).End(xlUp).row
activityrow = finddeliverableactivity.row
ActivtiyforDeliverable = ImportWs.Range("B" & activityrow).Value
Set findactivityintasks = .Range("B3:B" & lastrowsubtasks1).Find(What:=(ActivtiyforDeliverable), Lookat:=xlWhole)
newdeliverablerow = (findactivityintasks.row)
.Range("A" & (newdeliverablerow)).EntireRow.Insert
newrow = newdeliverablerow
.Range("A4").EntireRow.Copy
.Range("A" & newrow).EntireRow.PasteSpecial xlPasteFormulasAndNumberFormats
.Range("A" & newrow).EntireRow.PasteSpecial xlPasteValidation
.Range("A" & newrow).EntireRow.PasteSpecial xlPasteAllMergingConditionalFormats
.Range("A" & newrow & ":AE" & newrow & "").ClearContents
.Columns("A:BB").Calculate
.Range(IDCol & newrow).Value = subtaskws.Range("A" & newrow).Offset(-1, 0).Value + 0.01
.Cells(newrow, SubTaskCol).Value = cell.Value
.Cells(newrow, FormatCol).Value = cell.Offset(0, 1).Value
.Cells(newrow, AcceptanceCriteriacol).Value = cell.Offset(0, 2).Value
.Cells(newrow, TargetDateCol).Value = cell.Offset(0, 9).Value
.Cells(newrow, BWLCol).Value = cell.Offset(0, 7).Value
.Cells(newrow, TDSNumCol).Value = ImportWs.Range("N9").Value
.Cells(newrow, MileStoneNumCol).Value = DeliverableActivity
.Cells(newrow, AWCol).Value = "=SUM(AF" & newrow & ":" & lastcollet & newrow & ")"
.Cells(newrow, PCol).Value = "=(" & WCol & newrow & "*" & ICol & newrow & "*" & ECol & newrow & ")"
.Cells(newrow, LevelCol).Value = "=IF(" & PCol & newrow & " >11,1,IF(" & PCol & newrow & ">3,2,""N/A""))"
.Range("A" & newrow).EntireRow.Hidden = False
End With
End If
Next cell
Call CompactView
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
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!
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.