Hello All,
The issue I am having is that I am trying to set a variable (rng) equal to a range. I have six different Select Cases in which I do this, and it works in all but one of them. I'm not quite sure what the reason behind this is. Could someone please look at my code and let me know if they have any ideas? I have highlighted the instance where this has been occurring. I also included other subs that are called, however I do not think these have anything to do with the issue as all the other instances work fine.
Thanks for the help!
The issue I am having is that I am trying to set a variable (rng) equal to a range. I have six different Select Cases in which I do this, and it works in all but one of them. I'm not quite sure what the reason behind this is. Could someone please look at my code and let me know if they have any ideas? I have highlighted the instance where this has been occurring. I also included other subs that are called, however I do not think these have anything to do with the issue as all the other instances work fine.
Code:
Private Sub AddTcTemplate()
Dim rng As Range
Dim lrow As Long
Dim ws As Worksheet
Set ws = Worksheets("R&O")
Dim l As Long
Dim j As Long
Dim k As Long
ws.Select
If Me.cbo_type = "Opportunity" Then
ws.Select
'Find range max boundary
l = Application.WorksheetFunction.Match("RISK", Range("A1:A1500"), 0)
j = l - 2
Select Case Me.cbo_probability
Case "High"
Set rng = Range("B5:F" & j)
'If all rows are filled, add new row
If Application.WorksheetFunction.CountBlank(rng) = 0 Then
Call AddOppRow
End If
'If all rows are not filled, add to next blank row
lrow = GetRow(rng)
If lrow = 0 Then
lrow = 5
Else
lrow = lrow + 1
End If
'Format to blue when new
Range("B" & lrow & ":F" & lrow).Select
With Selection.Font
.Color = -3394765
.TintAndShade = 0
End With
'Add values to block
ws.Cells(lrow, 2).Value = Me.tbo_ID.Value
ws.Cells(lrow, 3).Value = Me.tbo_item.Value
ws.Cells(lrow, 4).Value = Me.tbo_amt.Value
If Me.tbo_investment.Value = "" Or IsNull(Me.tbo_investment.Value) Then
ws.Cells(lrow, 5) = "'"
Else
ws.Cells(lrow, 5) = Me.tbo_investment.Value
End If
If Trim(Me.tbo_ECD.Value) = "" Or IsNull(Me.tbo_ECD.Value) Then
ws.Cells(lrow, 6) = "'"
Else
ws.Cells(lrow, 6) = Me.tbo_ECD.Value
End If
Case "Medium"
Set rng = Range("G5:K" & j)
'If all rows are filled, add new row
If Application.WorksheetFunction.CountBlank(rng) = 0 Then
Call AddOppRow
End If
'If all rows are not filled, add to next blank row
lrow = GetRow(rng)
If lrow = 0 Then
lrow = 5
Else
lrow = lrow + 1
End If
'Format to blue when new
Range("G" & lrow & ":K" & lrow).Select
With Selection.Font
.Color = -3394765
.TintAndShade = 0
End With
ws.Cells(lrow, 7).Value = Me.tbo_ID.Value
ws.Cells(lrow, 8).Value = Me.tbo_item.Value
ws.Cells(lrow, 9).Value = Me.tbo_amt.Value
If Me.tbo_investment.Value = "" Or IsNull(Me.tbo_investment.Value) Then
ws.Cells(lrow, 10) = "'"
Else
ws.Cells(lrow, 10) = Me.tbo_investment.Value
End If
If Trim(Me.tbo_ECD.Value) = "" Or IsNull(Me.tbo_ECD.Value) Then
ws.Cells(lrow, 11) = "'"
Else
ws.Cells(lrow, 11) = Me.tbo_ECD.Value
End If
Case "Low"
Set rng = Range("L5:P" & j)
'If all rows are filled, add new row
If Application.WorksheetFunction.CountBlank(rng) = 0 Then
Call AddOppRow
End If
'If all rows are not filled, add to next blank row
lrow = GetRow(rng)
If lrow = 0 Then
lrow = 5
Else
lrow = lrow + 1
End If
'Format to blue when new
Range("L" & lrow & ":P" & lrow).Select
With Selection.Font
.Color = -3394765
.TintAndShade = 0
End With
ws.Cells(lrow, 12).Value = Me.tbo_ID.Value
ws.Cells(lrow, 13).Value = Me.tbo_item.Value
ws.Cells(lrow, 14).Value = Me.tbo_amt.Value
If Me.tbo_investment.Value = "" Or IsNull(Me.tbo_investment.Value) Then
ws.Cells(lrow, 15) = "'"
Else
ws.Cells(lrow, 15) = Me.tbo_investment.Value
End If
If Trim(Me.tbo_ECD.Value) = "" Or IsNull(Me.tbo_ECD.Value) Then
ws.Cells(lrow, 16) = "'"
Else
ws.Cells(lrow, 16) = Me.tbo_ECD.Value
End If
End Select
'Recalculate grand total
HighTotal = Application.Sum(Range(Cells(5, 4), Cells(j, 4)))
MediumTotal = Application.Sum(Range(Cells(5, 9), Cells(j, 9)))
LowTotal = Application.Sum(Range(Cells(5, 14), Cells(j, 14)))
NewTotal = (HighTotal * 0.9) + (MediumTotal * 0.5) + (LowTotal * 0.1)
NewTotal = -NewTotal
k = l - 1
ws.Cells(k, 14) = NewTotal
ElseIf Me.cbo_type = "Risk" Then
'Find range max boundary
l = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
j = l - 1
Select Case Me.cbo_probability
Case "High"
[COLOR="#0000FF"] [B][SIZE=5]Set rng = Range("B17:F" & j)[/SIZE][/B][/COLOR]
'If all rows are filled, add new row
If Application.WorksheetFunction.CountBlank(rng) = 0 Then
Call AddRiskRow
End If
'If all rows are not filled, add to next blank row
lrow = GetRow(rng)
If lrow = 0 Then
lrow = 17
Else
lrow = lrow + 1
End If
'Format to blue when new
Range("B" & lrow & ":F" & lrow).Select
With Selection.Font
.Color = -3394765
.TintAndShade = 0
End With
ws.Cells(lrow, 2).Value = Me.tbo_ID.Value
ws.Cells(lrow, 3).Value = Me.tbo_item.Value
ws.Cells(lrow, 4).Value = Me.tbo_amt.Value
ws.Cells(lrow, 5).Value = "'"
ws.Cells(lrow, 6).Value = Me.tbo_ECD.Value
Case "Medium"
Set rng = Range("G17:K" & j)
'If all rows are filled, add new row
If Application.WorksheetFunction.CountBlank(rng) = 0 Then
Call AddRiskRow
End If
'If all rows are not filled, add to next blank row
lrow = GetRow(rng)
If lrow = 0 Then
lrow = 17
Else
lrow = lrow + 1
End If
'Format to blue when new
Range("G" & lrow & ":K" & lrow).Select
With Selection.Font
.Color = -3394765
.TintAndShade = 0
End With
ws.Cells(lrow, 7).Value = Me.tbo_ID.Value
ws.Cells(lrow, 8).Value = Me.tbo_item.Value
ws.Cells(lrow, 9).Value = Me.tbo_amt.Value 'K
ws.Cells(lrow, 10).Value = Me.tbo_investment.Value
ws.Cells(lrow, 11).Value = Me.tbo_ECD.Value
Case "Low"
Set rng = Range("L17:P" & j)
'If all rows are filled, add new row
If Application.WorksheetFunction.CountBlank(rng) = 0 Then
Call AddRiskRow
End If
'If all rows are not filled, add to next blank row
lrow = GetRow(rng)
If lrow = 0 Then
lrow = 17
Else
lrow = lrow + 1
End If
'Format to blue when new
Range("L" & lrow & ":P" & lrow).Select
With Selection.Font
.Color = -3394765
.TintAndShade = 0
End With
ws.Cells(lrow, 12).Value = Me.tbo_ID.Value
ws.Cells(lrow, 13).Value = Me.tbo_item.Value
ws.Cells(lrow, 14).Value = Me.tbo_amt.Value
ws.Cells(lrow, 15).Value = Me.tbo_investment.Value
ws.Cells(lrow, 16).Value = Me.tbo_ECD.Value
End Select
'Recalculate grand total
HighTotal = Application.Sum(Range(Cells(17, 4), Cells(j, 4)))
MediumTotal = Application.Sum(Range(Cells(17, 9), Cells(j, 9)))
LowTotal = Application.Sum(Range(Cells(17, 14), Cells(j, 14)))
NewTotal = (HighTotal * 0.9) + (MediumTotal * 0.5) + (LowTotal * 0.1)
ws.Cells(l, 14) = NewTotal
End If
End Sub
Sub AddOppRow()
Dim l As Long
Dim n As Long
l = Application.WorksheetFunction.Match("RISK", Range("A1:A1500"), 0)
n = l - 1
Rows(n).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
Sub AddRiskRow()
Dim lrow As Long
Dim newRow As Long
lrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
newRow = lrow + 1
Range("B" & lrow & ":P" & lrow).Select
Selection.Cut
Range("B" & newRow).Select
ActiveSheet.Paste
End Sub
Function GetRow(rng As Range) As Long
Dim lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("R&O")
If WorksheetFunction.CountA(rng) > 0 Then
'Search for any entry, by searching backwards by Rows.
lastrow = rng.Find(what:="*", After:=rng.Cells(rng.Rows.Count, rng.Columns.Count), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'If Not Lastrow Is Nothing Then Lastrow.Activate
Else
lastrow = 0
End If
GetRow = lastrow
End Function
Thanks for the help!