posting my data from userform to sheet with the same format table

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
i have userform contains 3 textbox ans sheet1 contains data 3 columns and have table in sheet1
here i have code transfer data from userform to sheet1 but my problem every time when i fill data in userform and transfer data not the same format the next row what filled
HTML:
Private Sub CommandButton1_Click()On Error Resume Next
Dim My_sh As Worksheet
Set My_sh = Worksheets("sheet1")
Dim lastrow As Integer
Dim i%
With My_sh
lastrow = .Cells(Rows.Count, 1).End(3).Row + 1
For i = 1 To 3
.Cells(lastrow, i).Value = Me.Controls("TextBox" & i)
Me.Controls("TextBox" & i) = ""
Next
MsgBox "done"
End With
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You must put the type of data when you pass it to the sheet, for example:


Code:
Private Sub CommandButton1_Click()
  On Error Resume Next
  Dim My_sh As Worksheet
  Set My_sh = Worksheets("sheet1")
  Dim lastrow As Integer
  Dim i%
  With My_sh
    lastrow = .Cells(Rows.Count, 1).End(3).Row + 1
    .Cells(lastrow, "A").Value = TextBox1.Value           'text value
    .Cells(lastrow, "B").Value = CDate(TextBox2.Value)    'date value
    .Cells(lastrow, "C").Value = CDbl(TextBox3.Value)     'number value
    MsgBox "done"
  End With
End Sub
 
Upvote 0
You must put the type of data when you pass it to the sheet, for example:


Code:
Private Sub CommandButton1_Click()
  On Error Resume Next
  Dim My_sh As Worksheet
  Set My_sh = Worksheets("sheet1")
  Dim lastrow As Integer
  Dim i%
  With My_sh
    lastrow = .Cells(Rows.Count, 1).End(3).Row + 1
    .Cells(lastrow, "A").Value = TextBox1.Value           'text value
    .Cells(lastrow, "B").Value = CDate(TextBox2.Value)    'date value
    .Cells(lastrow, "C").Value = CDbl(TextBox3.Value)     'number value
    MsgBox "done"
  End With
End Sub


thanks for your replying but you don't understand me your code and mine it works but the problem the format the table in sheet for instance i have color blue in my table in sheet the data begin from row3 i would when i fill data in userform and transfer data automatically insert the colored row with the data like the row what before
 
Upvote 0
Then before putting the data you should copy the format of the previous row and paste in the new row.
For example:

Code:
Private Sub CommandButton1_Click()
  On Error Resume Next
  Dim My_sh As Worksheet
  Set My_sh = Worksheets("sheet1")
  Dim lastrow As Integer
  Dim i%
  Application.ScreenUpdating = False
  With My_sh
    lastrow = .Cells(Rows.Count, 1).End(3).Row + 1
[COLOR=#0000ff]    .Range(.Cells(lastrow - 1, 1), .Cells(lastrow - 1, 3)).Copy[/COLOR]
[COLOR=#0000ff]    .Cells(lastrow, 1).PasteSpecial Paste:=xlPasteFormats[/COLOR]
    For i = 1 To 3
      .Cells(lastrow, i).Value = Me.Controls("TextBox" & i)
      Me.Controls("TextBox" & i) = ""
    Next
  End With
  Application.ScreenUpdating = True
  Application.CutCopyMode = False
  MsgBox "done"
End Sub
 
Upvote 0
Then before putting the data you should copy the format of the previous row and paste in the new row.
For example:

Code:
Private Sub CommandButton1_Click()
  On Error Resume Next
  Dim My_sh As Worksheet
  Set My_sh = Worksheets("sheet1")
  Dim lastrow As Integer
  Dim i%
  Application.ScreenUpdating = False
  With My_sh
    lastrow = .Cells(Rows.Count, 1).End(3).Row + 1
[COLOR=#0000ff]    .Range(.Cells(lastrow - 1, 1), .Cells(lastrow - 1, 3)).Copy[/COLOR]
[COLOR=#0000ff]    .Cells(lastrow, 1).PasteSpecial Paste:=xlPasteFormats[/COLOR]
    For i = 1 To 3
      .Cells(lastrow, i).Value = Me.Controls("TextBox" & i)
      Me.Controls("TextBox" & i) = ""
    Next
  End With
  Application.ScreenUpdating = True
  Application.CutCopyMode = False
  MsgBox "done"
End Sub


thanks so much the code is worked
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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