Offset values not being entered into spreadsheet

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I not sure why the below code is not adding my userform entries to the spreadsheet. I did a CompileVBAProject test and there were no issues. I stepped through the program and no error messages appeared. What this program is suppose to do is it will find the final row with a value in it and add one to find the first empty row. Then it will make it the Selected ActiveCell if it is empty.

Starting with that cell, it will start adding values to the spreadsheet from the userform or it supposed to add values but it doesn't. Thank You

Code:
Private Sub cmdbtnAddItem_Click()    
    Dim FinalRow As Integer, x As Integer


    Worksheets("IMA").Select


    FinalRow = Cells(Rows.Count, 2).End(xlUp).Row
    
        x = FinalRow + 1
        Cells(x, 2).Select
        If Cells(x, 2).Value = "" Then
            ActiveCell.Select
        Else
            Selection.End(xlUp).Offset(1, 0).Row
        End If
        
        ActiveCell.Offset(x, 2).Value = frmAddProduct.txtbxPrdctCde.Value
        ActiveCell.Offset(x, 3).Value = frmAddProduct.txtbxDescription.Value
        ActiveCell.Offset(x, 4).Value = Me.txtbxDzPrCs.Value
        ActiveCell.Offset(x, 5).Value = Me.txtbxCsPerPal.Value
        ActiveCell.Offset(x, 7).Value = Me.txtbxStckNum.Value
       

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
.
You really don't need to use the Offset command to accomplish your goal. Review this macro :

Code:
Option Explicit


Private Sub btnOK_Click()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim newRow As Long
    
    'next line checks Col A for first blank cell after last used
    newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1
    
    ws.Cells(newRow, 1).Value = Me.txtbxPrdctCde.Value          'write to Col A
    ws.Cells(newRow, 2).Value = Me.txtbxDescription.Value       'writes to Col B
    ws.Cells(newRow, 3).Value = Me.txtbxDzPrCs.Value            'wrotes to Col C
    ws.Cells(newRow, 4).Value = Me.txtbxCsPerPal.Value          'writes to Col D
    ws.Cells(newRow, 5).Value = Me.txtbxStckNum.Value           'writes to Col E
    
End Sub

As the comments indicate ... the values from the textboxes in your UserForm are written to the first blank row, beginning in Col A.
If you really want the data to shift one column to the right (begin writing to Col B), edit ws.Cells(newRow, 1).Value to ws.Cells(newRow, 2).Value
and edit the remainder to the next higher number as well.
 
Upvote 0
Your script should look something like this.
Never a good plan to use active cell.

My Controls have different names.

Code:
Private Sub cmdbtnAddItem_Click()
'Modified  9/12/2018  10:19:06 PM  EDT
Worksheets("IMA").Select
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row + 1
            
        Cells(Lastrow, 2).Value = TextBox1.Value
        Cells(Lastrow, 3).Value = TextBox2.Value
        Cells(Lastrow, 4).Value = TextBox3.Value
        Cells(Lastrow, 5).Value = TextBox4.Value
        Cells(Lastrow, 7).Value = TextBox5.Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,471
Messages
6,124,996
Members
449,201
Latest member
Lunzwe73

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