Im writing a code but i get the Invalid Next control variable reference error, and i don't know why. this is my code:
Your i and j variables are the wrong way round, you need to make sure the correct variable is with the correct indentation. Hope this helps!
Sub Network()
Dim Net(1 To 8) As Double
Dim Total(1 To 8) As Double
Dim i As Integer
For i = 1 To 8
For j = 1 To 5
Net(j) = Worksheets("Summary").Cells((i * 7) + 12, 9).Value
Next j
Next i
For j = 1 To 5
Total(j) = WorksheetFunction.Sum(Net(j))
Next j
For j = 1 To 5
Worksheets("Summary").Cells(73, 8 + j) = Total(j)
Next j
End Sub
Related
I am having problems with the definition of my variables I think but I cannot see where or why. It's quite a simple code to count the amount of lessons teachers have allocated. The information is in the worksheet 'Subects and Teachers 2018' and has to be printed in the worksheet 'Teachers'. The quantities always appear on the left of the name.
Here's the code. If anyone could give me a hint on what I'm defining incorrectly I would be very thankful! Debugging suggests that the problem is in the line which has ***** at the end (not part of the code).
Sub Counter2018()
Dim Var1 As String
Dim CVar1 As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
For k = 2 To 50
Var1 = Worksheets("Teachers").Cells(k, 3)
CVar1 = 0
For i = 2 To 45
For j = 2 To 45
If Worksheets("2018 Subjects and Teachers").Cells(i, j) = Var1 Then
CVar1 = CVar1 + Worksheets("2018 Subjects and Teachers").Cells(i, j - 1) *****
End If
Next j
Next i
Worksheets("Teachers").Cells(k, 5) = CVar1
Next k
End Sub
Try this version (untested)
Option Explicit
Public Sub Counter2018()
Dim wsTeachers As Worksheet, wsSubjects As Worksheet
Set wsTeachers = Worksheets("Teachers")
Set wsSubjects = Worksheets("2018 Subjects and Teachers")
Dim teacher As String, counter As Long
Dim i As Long, j As Long, k As Long
For k = 2 To 50
teacher = wsTeachers.Cells(k, 3)
counter = 0
For i = 2 To 45
For j = 2 To 45
If wsSubjects.Cells(i, j).Value2 = teacher Then
If Not IsError(wsSubjects.Cells(i, j - 1)) Then
counter = counter + Val(wsSubjects.Cells(i, j - 1).Value2)
End If
End If
Next
Next
wsTeachers.Cells(k, 5) = counter
Next
End Sub
I think the Type Mismatch error is caused by some of the cells in wsSubjects.Cells(i, j - 1)
That counter expects numbers in that column, but there might be some strings or errors in there
Sub Off_Hours_Set_TEST()
Dim x As String
Dim found As Boolean
Dim i As Integer, j As Integer
' Select first line of data.
Range("A1").Select
' Set search variable value.
x = "Off"
For i = 0 To 2
For j = 0 To 10
If ActiveCell.Value = x Then
found = True
ActiveCell.Offset(i, j + 2) = "0"
Else
ActiveCell.Offset(i, j + 2).Value = ActiveCell.Offset(i, j + 1).Value - ActiveCell.Offset(i, j).Value
End If
j = j + 2
Next j
Next i
End Sub
Trying to make a little code that tallies up hours worked for day. It works until it encounters the cell with the word 'Off' in it, then it RTE 13's on me. I'm not quiet sure why it does this, or where the mismatch is coming from, as all it is doing is checking to see if the cell = Off, if it does, it inputs a 0 in the offset hours worked column. Ideas?
I'm stuck in an assignment for school, this is what i have to do:
This is the current code I have:
You need to loop in the array you've loaded and :
Sub Ratio()
Dim OperatingRatio() As Double
Dim j As Long
With Sheets("Summary")
OperatingRatio = .Cells("I80:M80").Value
For j = LBound(OperatingRatio, 2) To UBound(OperatingRatio, 2)
If OperatingRatio(1, j) > 100 Then
MsgBox .Cells(14, 9 + j)
.Cells(80, 9 + j).Interior.Color = vbRed
Else
End If
Next j
End With
End Sub
It is better if you loop through all cells individually, like this:
Dim i As Integer
'9 means column I and 13 is column M
For i = 9 To 13
' Getting the percent values
Debug.Print Worksheets("Summary").Cells(80, i).Value
Next
This way you can continue by yourself.
I want to copy some columns of an excel sheet to another sheet.
I have written the code, which doesnt work. It gets into an infinite loop, and exits with an error. The code is:
Sub customCopy()
Dim i As Integer
Dim j As Integer
j = 1
For i = 1 To 700
If i Mod 5 = 2 Then
Columns(i).Copy Destination:=Sheets(2).Rows(j)
j = j + 1
End If
If i Mod 5 = 3 Then
Columns(i).Copy Destination:=Sheets(2).Rows(j)
j = j + 1
End If
If i Mod 5 = 4 Then
Columns(i).Copy Destination:=Sheets(2).Rows(j)
j = j + 1
End If
Next i
End Sub
Please help..
Copying a column into a row won't work.............a row is too small!
I got the compile error message - End If without If Block. Can anyone tell what is wrong with my code:
Sub Find()
Dim i As Integer
Dim j As Integer
Dim k As Integer
For i = 2 To 107
For j = 4 To 6
For k = 2 To 573
If InStr(Sheet2.Cells(k, 7), Sheet1.Cells(i, j)) <> 0 Then Sheet2.Cells(k, 12) = Sheet2.Cells(k, 9)
End If
Next k
Next j
Next i
End Sub
You don't need to use End If for inline If statements. Just remove it and you'll be ok, or move the bit after then to the next line.
You need to use End If when you have a multi-line if statement