Can someone explain why this code is writing yes in column 27 instead of column 14?

supermom28115

New Member
Joined
May 9, 2022
Messages
43
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Public Sub birthLoops()
'this code checks for record and birth location notes and copies if needed

Dim x As Range, y As Range, record As Variant, birth As Variant
Dim startrow As Integer, i As Integer, endrow As Integer

    endrow = Cells(Rows.Count, 14).End(xlUp).Row
 
    For i = 3 To endrow
    Set y = Cells(i, 14)
    Set x = Cells(i, 13)
    'for true answers it should evaluate the next line? for false it finds else or end and then the next line?
            If IsEmpty(y(i, 14).Value2) Then    'this is supposed to look at the cell and say true to empty?
                         If LCase(x(i, 13).Value2) = "yes" Then  'this is supposed to look at the value of the cell and if they match say true?
                              Set birth = Range(y(i, 14).Offset(0, 2), y(i, 14).Offset(0, 4))
                              Set record = Range(y(i, 14).Offset(0, -4), y(i, 14).Offset(0, -2))
                              birth.Value = record.Value
                        End If
                        If LCase(y(i, 14).Value2) = "yes" Then
                        Else
                        y(i, 14).Value2 = "yes"  'this is the line, i thought it was not doing this step even though when i stepped through it was doing what i expected
                        End If                             'and was evaluating and, i thought, processing the code. It was after about the 5th time of stepping through and the 
                                                             '2nd time of running the code that I found yes printed in several rows on column AA. 
         End If
    Next i
            
End Sub

I know it has other problems, specifically me having it start at the beginning of the column every time it runs but I just got so frustrated with trying to get the count rows code to work right. I still don't quite understand how the code process works for that particular issue. I don't mind it running repeatedly right now since I only have 10 rows of data right now. I think my row count is also adding a row every time it counts as well. I guess I should also say that I have this set as a table. I thought it would help with coding to be able to name ranges and have them automatically adjust as new records were added, however I ended up throwing the book across my house yesterday when I tried to use named ranges in the code. This is the dropbox link to the file as I am working on it now.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
This y(i, 14) is looking at column 27 not column 14, it should just be y If IsEmpty(y.Value2) Then
 
Upvote 0
Solution
Re: "throwing the book across my house yesterday"

This is not the way a Supermom should behave. Bad example for the kids.
 
Upvote 0
This y(i, 14) is looking at column 27 not column 14, it should just be y If IsEmpty(y.Value2) Then
I had all of my code lines with only the variable but the system did not seem to be acknowledging the value in the variable so I did it this way. I have seen other examples of code with it written this way so I must be missing some rule. Are they all wrong? Should I be using this combination of variable and cell address only for specific situations? And this may be the dumb question but why is it looking at 27? 28 I would have maybe pretended to understand but have I created some weird math expression because I was trying to use the immediate window and locals window to try to figure out why it was not putting yes into that cell.
 
Upvote 0
Re: "throwing the book across my house yesterday"

This is not the way a Supermom should behave. Bad example for the kids.
yes, and my 14 year old son was quick to tell me that I shouldn't behave that way. But he isn't trying to learn code at ?? age either. I was just a little upset with Bill J last night.
 
Upvote 0
y(i,1) is looking at col 14 so you are offsetting that 13 columns which takes you to 27
I thought you had to use the .offset for that. I use offset a lot and did not know you could do an offset without specifying.
 
Upvote 0
Re: "why is it looking at 27? 28 I would have maybe pretended to understand"
Difference between Offset and Resize.
Offset(, 1) is the next cell to the right while Resize(, 1) is the same cell in the reference.
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,172
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top