VBA - Code was working and now doesn't

Chris_86

New Member
Joined
Dec 9, 2012
Messages
18
Office Version
  1. 2013
Platform
  1. Windows
Hi All,

This code was working less than 2 days ago and now it's just suddenly stopped working.
Can anyone work out why it has failed?
Debug stated the following lines are the issue.
rng.Parent.Cells(LastRow + 1, 2).Value = SurnameTextBox.Value
rng.Parent.Cells(LastRow + 1, 3).Value = USITextBox.Value
rng.Parent.Cells(LastRow + 1, 4).Value = EmployerTextBox.Value
rng.Parent.Cells(LastRow + 1, 5).Value = DOBTextBox.Value
rng.Parent.Cells(LastRow + 1, 6).Value = EmailTextBox.Value
rng.Parent.Cells(LastRow + 1, 7).Value = PositionTextBox.Value
Take in mind I haven't changed the coding since it was working either.

VBA Code:
Private Sub RegisterButton_Click()

ActiveSheet.Unprotect "DMTR20"

Dim rng As Range
Set rng = ActiveSheet.ListObjects("Table1").Range
Dim LastRow As Long
LastRow = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row

    rng.Parent.Cells(LastRow + 1, 1).Value = NameTextBox.Value
    rng.Parent.Cells(LastRow + 1, 2).Value = SurnameTextBox.Value
    rng.Parent.Cells(LastRow + 1, 3).Value = USITextBox.Value
    rng.Parent.Cells(LastRow + 1, 4).Value = EmployerTextBox.Value
    rng.Parent.Cells(LastRow + 1, 5).Value = DOBTextBox.Value
    rng.Parent.Cells(LastRow + 1, 6).Value = EmailTextBox.Value
    rng.Parent.Cells(LastRow + 1, 7).Value = PositionTextBox.Value
    
ActiveSheet.Protect "DMTR20"

End Sub

Any help would be appreciated.

Thank you.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Do you get an error ?
Have you stepped through using F8 to see if any line causes an issue ?
Have you renamed any of the text boxes ?
Have you protected the worksheet / workbook with a different password??
 
Upvote 0
Not sure why it's stopped working, but you could save yourself a lot of repetition by doing something like this
VBA Code:
Private Sub RegisterButton_Click()
  ActiveSheet.Unprotect "DMTR20"
  Dim rng As Range, LastRow As Long
  
  Set rng = ActiveSheet.ListObjects("Table1").Range
  LastRow = rng.Rows.Count
  
  With rng.Cells(LastRow, 1).Offset(1)
    .Resize(1, 7).Value = _
      Array( _
        NameTextBox.Value, _
        SurnameTextBox.Value, _
        USITextBox.Value, _
        EmployerTextBox.Value, _
        DOBTextBox.Value, _
        EmailTextBox.Value, _
        PositionTextBox.Value)
  End With
    
  ActiveSheet.Protect "DMTR20"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,553
Members
449,038
Latest member
Guest1337

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