Excel VBA: how to cut multiple rows and paste in a new file and save

albertxu

New Member
Joined
Jan 30, 2015
Messages
3
I have daily repetive tasks that need a VBA to help me out.
I have a file which is updated daily, the excel file contains like:
First Name|Last Name|Email| Password
John| Lee | Johnlee#hotmail.com | blabla
......

So here are my tasks, when a client ask for like 100 accounts, I cut 100 accounts starting ROW 2 and open a new excel file and paste those accounts (100 rows) from ROW 2 in that new file, and then I COPY the first row of the original file and paste that in the first row of the new file.
And then save the new file into the same folder.
And then save the original file.
I am thinking of putting a form in the file, one button and two text controls. One text control for inputting the number of rows which need to be cut and pasted each time, other text control for inputting username of the client, which will be used in the file name of the new file. I need the new file save in the name of "Number_username.xls".
I am very new to VBA and need your help. Thank you.
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
.
.


Use this as a starting point:


Code:
Sub MoveRows()

  Dim varRowsToCut    As Variant
  Dim wbkDestination  As Workbook
  Dim strFileName     As String
  
GetRows:
  varRowsToCut = Application.InputBox( _
    Prompt:="Enter number of rows to cut:", Type:=1)
  
  If varRowsToCut = False Then
    Exit Sub
  ElseIf varRowsToCut <> Int(varRowsToCut) Or varRowsToCut < 1 Then
    MsgBox "Number is not valid.", vbExclamation
    GoTo GetRows
  End If
  
  Set wbkDestination = Workbooks.Add
  strFileName = ThisWorkbook.Path & "\" _
      & Format$(Now, "yyyymmddhhmmss") & ".xlsx"
  
  With ThisWorkbook.Worksheets(1)
    .Rows(1).Copy wbkDestination.Sheets(1).Range("A1")
    .Rows("2:" & 1 + varRowsToCut).Copy
  End With
  
  With wbkDestination
    .Sheets(1).Rows(2).Insert
    .SaveAs strFileName
    .Close
  End With
  
  ThisWorkbook.Worksheets(1).Rows("2:" & 1 + varRowsToCut).Delete
  ThisWorkbook.Save
  MsgBox "New file saved:" & vbCrLf & strFileName, vbInformation

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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