How to insert entire row using VBA?

jovie

Board Regular
Joined
Nov 13, 2015
Messages
68
Hi, I am New to VBA, when I write a code I meet some question, and I hope someone can help me.
I was trying to use userform to insert a row when condition meets that textbox2 not equal to Range("B") in Master Data worksheet and when range("G") in Master Data worksheet is not equal to zero
But I have problems that how can I insert when condition meets I state above and it can key in the data to the new row.
Private Sub CommandButton1_Click()
Set mcsh = Sheets("Master Data")
Set msish = Sheets("Stock In")

mclr = mcsh.Range("a65535").End(xlUp).Row
msilr = msish.Range("a65535").End(xlUp).Row


msish.Cells(msilr + 1, "A") = TextBox1
msish.Cells(msilr + 1, "B") = TextBox2
msish.Cells(msilr + 1, "E") = TextBox5


For i = 7 To mclr
If mcsh.Cells(i, "B") & "" = TextBox1 And mcsh.Cells(i, "I") & "" = TextBox2 Then
mcsh.Cells(i, "E") = TextBox5
Set rg = mcsh.Range("C" & i & ":D" & i)
rg.Copy msish.Range("B" & (msilr + 1))
Exit For
End If
If mcsh.Cells(i, "B") & "" = TextBox1 And mcsh.Cells(i, "I") & "" <> TextBox2 And mcsh.Cells(i, "G") = 0 Then
mcsh.Cells(i, "C") = TextBox3
mcsh.Cells(i, "D") = TextBox4
mcsh.Cells(i, "G") = TextBox5
msish.Cells(msilr + 1, "C") = TextBox3
msish.Cells(msilr + 1, "D") = TextBox4
End If
If mcsh.Cells(i, "B") & "" = TextBox1 And mcsh.Cells(i, "I") & "" <> TextBox2 And mcsh.Cells(i, "G") <> 0 Then
mcsh.ActiveCell.EntireRow.Insert
mcsh.Cells(i, "C").Offset(1, 0) = TextBox3
mcsh.Cells(i, "D").Offset(1, 0) = TextBox4
mcsh.Cells(i, "G").Offset(1, 0) = TextBox5
msish.Cells(msilr + 1, "C") = TextBox3
msish.Cells(msilr + 1, "D") = TextBox4
End If
Next

Unload UserForm2
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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