When I run the program, nothing happens. I think it's because I am not using the right variable types or I am not doing variable and value assignment properly.
This is what I want the code to do:
For every cell from row 80, column 6 to row 90, column 6 , I want j
to be the INTEGER specified in that cell. For every column from 10 to 100, if the DATE in Cells(i,2)
is the same as the DATE in Cells(1,k)
, then I want to set Cells(j, k)
as the INTEGER found in Cells(j, 6)
.
Please help me correct this code.
Sub TestSub()
Dim i As Integer, i2 As Integer, i3 As Integer
Dim j As Integer, j2 As Integer, j3 As Integer
Dim k As Integer, k2 As Integer, k3 As Integer
For i = 81 To 95
j = Cells(i, 6) 'j becomes the row # of the equipment
For k = 8 To k = 115
If Cells(i, 2) = Cells(1, k) Then Cells(j, k) = Cells(j, 6) 'Cells(i,2)->NEXT PM DATE Cells(1,k)->CALENDER DATE (MM/1/YY)
Next k
Next i
For i2 = 97 To 105
j2 = Cells(i2, 6)
For k2 = 8 To k2 = 115
If Cells(i2, 2) = Cells(1, k2) Then Cells(j2 + 1, k2) = Cells(j2 + 1, 6)
Next k2
Next i2
For i3 = 107 To 121
j3 = Cells(i3, 6)
For k3 = 8 To k3 = 115
If Cells(i3, k3) = Cells(j3, 6) Then Cells(j3 + 2, k3) = Cells(j3 + 2, 6)
Next k3
Next i3
End Sub
This is what your code is doing:
It compares the dates for each cell in range B80:B90
(rows 80 to 90) with the dates of cells in range J1:CV1
(columns 10 to 100) If the dates compared are equal then it takes the value in the same row and column 6 ( F
). This value is then used to reference a row number and places that row number in the column that contains the same date.
The program is validating the dates in range B80:B90
with the dates in range J1:CV1
and for those found equal is updating the corresponding cell in the row determined by the corresponding value in the range F80:F90
.
Let's look at the following sample case, where the date in cell B80
equals the date in cell J1
and the value in cell F80
is 97
. Then the program will enter the value of cell F97
in cell J97
Dim i As Integer, j As Integer, k As Integer
For i = 80 To 90
If i = 89 Then Stop
j = Cells(i, 6)
For k = 10 To 100
'as per the sample case
'i = 80 ; j = 97 and k = 10
'date in cell(B80) = date in cell(J1)
'If Cells(i, 2) = Cells(1, k) Then Cells(j, k) = Cells(j, 6)
'then J97 = F97
'Replaced with:
If Cells(i, 2) = Cells(1, k) Then Cells(j, 6) = Cells(i, 6)
Next k: Next
Therefore, if it seems that the program is doing nothing and could not see any result is either because there are no equal dates in the ranges compared or because results are expected to be shown in the range J80:CV90
but the values in range F80:F90
are determining a different output range (ie the values in range F80:F90
are lower than 80 or higher than 90.
I asked for the values in range F80:F90
to be provided to validate the above.
So if the objective is:
If the dates are equal, then it takes the value in the same row and column 6 (F) and inserts that value into cells(j, 6)
Then replaced line
If Cells(i, 2) = Cells(1, k) Then Cells(j, k) = Cells(j, 6)
with line
If Cells(i, 2) = Cells(1, k) Then Cells(j, 6) = Cells(i, 6)
The value of j
is the value in cell(i,6)
as determined by the line: j = Cells(i, 6)
Basically if the date in cell B80
is found in Range J1:CV1
then enters 73
, value in cell F80
or Cell(i,6)
, into cell F73
or Cell(j,6)
.
The problem is you got confused because you used single-letter variable names and made a mistake in your code that is not easily identifiable. To correct the error, change this line:
If Cells(i, 2) = Cells(1, k) Then Cells(j, k) = Cells(j, 6)
To be this instead:
If Cells(i, 2) = Cells(1, k) Then Cells(j, k) = Cells(i, 6)
Notice the only difference is that instead of setting it to Cells(j, 6)
it needs to be set to Cells(i, 6)
. This is a common mistake for new programmers, and is the exact reason that using descriptive variable names is good practice. It will prevent simple errors like this.
Have to post a second answer as the asker changed entirely the code:
These lines will never work
For k = 8 To k = 115
For k2 = 8 To k2 = 115
For k3 = 8 To k3 = 115
Change to the correct format
For k = 8 To 115
For k2 = 8 To 115
For k3 = 8 To 115
Then try
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.