WHY IS THIS CODE IS RANDOMLY POSTING CHANGES TO THE WRONG CELL ROW IN THE SHEET FROM THE USERFORM ?

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
918
Office Version
  1. 365
Platform
  1. Windows
This code does not work right every time.. Sometimes notes and/or changes in Textbox2 of Userform1 are posted to the correct Sheet row cell ,and sometimes the notes posts at the very top in row 2 when the Save button is clicked. I cannot figure out why its doing this. It should post changes to the correct cell row and column every time all the time.

Does it have to do with the code being able to identify the correct row number in the sheet cell that is being displayed in the userform that the user is making a note in the textbox in.? I did not put this lastrow code line in the code block below because I didn’t think it was necessary – thinking, what does the lastrow have to do with finding the current row?

Code:
lastrow = Sheets("BIBLETEXT").Cells(rows.count, 1).End(xlUp).Row

This is the simple one line of code below that's supposed to save Textbox2 note to the correct cell row in column C
Code:
Private Sub cmdSAVE_Click()
Sheets("BIBLETEXT").Cells(rowno, 3) = TextBox2

I did look for solutions and all that keeps coming up from Stack Overflow and ChatGPT (which gets its solutions from ChatGPT most of the time)
and all I get are long lines of code that use a For Loop . I tend to stay away from a For loop if I can, but I'm not opposed to it if that solves this problem.

I put images in the sheet and where this line of code takes the note randomly. All this is done from a button click.
Any help and guidance on this - which seems very simple to fix - will be very much appreciated.

Thanks, cr
 

Attachments

  • THIS NOTE ON ROW1 OF  COL C BELONGS AT THE BOTTOM OF THE LAST ROW IN COL C.  .png
    THIS NOTE ON ROW1 OF COL C BELONGS AT THE BOTTOM OF THE LAST ROW IN COL C. .png
    21.7 KB · Views: 14
  • NOTE SHOULD POST HERE IN ROW 15 OF COL C - NOT IN ROW 1 OF COL C.  SOMETHING MISSING IN THE CODE.png
    NOTE SHOULD POST HERE IN ROW 15 OF COL C - NOT IN ROW 1 OF COL C. SOMETHING MISSING IN THE CODE.png
    28.1 KB · Views: 13

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
How do you arrive at the value of the variable "rowno"?
 
Upvote 0
Is there more to the code than that single line? You're using a variable that we can't see how/when it is declared or given a value. Please post the entire code.
 
Upvote 0
You haven't shown us much of your code, from what you have posted it would appear that rowno is undefined. How is it defined and where is it set to a value? what is the scope of the variable? you might find it useful to read tthis MS article:
Understanding scope and visibility (VBA)
 
Upvote 0
Sorry everyone - the rowno value comes from the change event in Listbox1. Yes. I have a Listbox on the userform which is populated by
a FIND code block. I should have mentioned this earlier. Here is the code for the Change Event for
Listbox1 with an image of the userform and a Listbox selected item displayed
Listbox1 Change Event:
Code:
Private Sub ListBox1_Change()
Dim n As Long
n = ListBox1.ListIndex
Verse = ListBox1.Value
TextBox1 = ListBox1.List(n, 1)
TextBox2 = ListBox1.List(n, 2)
rowno = n + 1
'currentrow = currentrow + 1  'currentrow is just a global variable.  
End Sub

rowno is assigned the value of 1 in the Initialize event:
Code:
Private Sub UserForm_Initialize()
Dim lastrow, currentrow As Long
ListBox1.ListIndex = 0
lastrow = Sheets("BIBLETEXT").Cells(rows.count, 1).End(xlUp).Row
currentrow = 1
Verse = Sheets("BIBLETEXT").Cells(currentrow, 1)
TextBox1 = Sheets("BIBLETEXT").Cells(currentrow, 2)
TextBox2 = Sheets("BIBLETEXT").Cells(currentrow, 3)
rowno = 1
totrows = lastrow
End Sub
I do see that I did not define rowno in this code block, nor as a global variable.

When the form runs, 1 is in the textbox named rowno.

Image below:

Thanks for all your help.
cr
 

Attachments

  • USERFORM WITH rowno = 1 at bottom.  The textbox is named rowno also..png
    USERFORM WITH rowno = 1 at bottom. The textbox is named rowno also..png
    184 KB · Views: 4
Upvote 0
It is hard to work with pictures. It would be easier to help if you could upload a copy of your file (de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data.
 
Upvote 0
It would appear that my guess as to what your problem is correct, you need to define rowno as global variable since you are using it in two different subs in two different modules.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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