Need to copy specific data from active workbook to a new workbook and save the new workbook in a user specified location

Purple_Squirrel

New Member
Joined
May 8, 2019
Messages
7
Hi,
I am trying to write data contained in a specific range from the active workbook to a new workbook and then the new workbook ought to be saved in a user specified location. I am using the following code:

Dim x As Workbook
Dim y As Workbook
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
TryAgain:
Flname = InputBox("Enter File Name :", "Creating New File...")
MsgBox Len(Flname)
If Flname <> "" Then
Set NewWkbk = Workbooks.Add
ThisWorkbook.Sheets(1).Copy Before:=NewWkbk.Sheets(1)
'ThisWorkbook.Sheets(1).Range("A2:B200").Copy = NewWkbk.Sheets(1).Range("A2:B200")

NewWkbk.SaveAs ThisWorkbook.Path & "" & Flname
If Err.Number = 1004 Then
NewWkbk.Close
MsgBox "File Name Not Valid" & vbCrLf & vbCrLf & "Try Again."
GoTo TryAgain
End If
ActiveWorkbook.Close
End If

I would like to modify this code to copy a specific range from the active workbook instead of the entire contents of the workbook to the new workbook. Any suggestions/help on how to do this would be extremely appreciated. Thank you.
Regards,
Purple
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about
Code:
Sub purpleSquirrel()
   Dim Wbk As Workbook
   Dim Fname As String
   Application.ScreenUpdating = False
   
   Set Wbk = Workbooks.Add
   ThisWorkbook.Sheets(1).Range("A2:B200").Copy Wbk.Sheets(1).Range("A2:B200")
   Fname = Application.GetSaveAsFilename
   
   Wbk.SaveAs Fname, 51
   Wbk.Close
End Sub
 
Upvote 0
How about
Code:
Sub purpleSquirrel()
   Dim Wbk As Workbook
   Dim Fname As String
   Application.ScreenUpdating = False
   
   Set Wbk = Workbooks.Add
   ThisWorkbook.Sheets(1).Range("A2:B200").Copy Wbk.Sheets(1).Range("A2:B200")
   Fname = Application.GetSaveAsFilename
   
   Wbk.SaveAs Fname, 51
   Wbk.Close
End Sub

Dear Fluff, Thank you for your prompt response. I tried this and it's working well:

Sub purpleSquirrel()
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
TryAgain:
Flname = InputBox("Enter File Name :", "Creating New File...")
MsgBox Len(Flname)
If Flname <> "" Then
Set NewWkbk = Workbooks.Add


ThisWorkbook.Sheets(1).Range("A2:C200").Copy
ActiveWorkbook.Sheets(1).Range("A2:C200").PasteSpecial



NewWkbk.SaveAs ThisWorkbook.Path & "" & Flname
If Err.Number = 1004 Then
NewWkbk.Close
MsgBox "File Name Not Valid" & vbCrLf & vbCrLf & "Try Again."
GoTo TryAgain
End If
ActiveWorkbook.Close
End If

End Sub

Thank you.
Best regards,
Purple Squirrel
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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