Code Export an area to a new file located on the Desktop

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hi everybody . I want to export an area on the working file to a new file on the dekstop, what should I do?

I would suggest the following procedure:

1. Check if there is a file named "New.XLSX" on the Desktop. If not, create a new file "New.XLSX" . If yes, then Export overwrite it
2. Copy the area to be exported For example: Sheets("data").range("C7:E16").copy to the file "New.XLSX" created on the Desktop and paste the Value at position A1 in Sheet1
3. Close File "New.XLSX"
Sincerely thank everyone
1631793896198.png

Sincerely thank everyone
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi Excelpromax123,
try this code, if the new file is overwritten anyway it's not necessary to check if it exists, just set DisplayAlerts = False
VBA Code:
Sub test()
    
    Dim MyRange As Range
    Dim MyNewFile As String
    Dim Wbk As Workbook
    
    Set MyRange = ThisWorkbook.Sheets("data").Range("C7:E16")
    
    MyNewFile = Environ("USERPROFILE") & "\Desktop\New.xlsx"
          
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    
    Set Wbk = Workbooks.Add
    MyRange.Copy
    
    With Wbk
        .Sheets(1).Range("A1").PasteSpecial (xlPasteValues)
        .SaveAs MyNewFile
        .Close
    End With
    
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
    
End Sub
 
Upvote 0
Solution
Xin chào Excelpromax123!
hãy thử mã này, nếu tệp mới bị ghi đè thì không cần thiết phải kiểm tra xem nó có tồn tại hay không, chỉ cần đặt DisplayAlerts = False
[CODE = vba] Kiểm tra phụ ()

Dim MyRange As Range
Dim MyNewFile As String
Dim Wbk As Workbook

Đặt MyRange = ThisWorkbook.Sheets ("data"). Range ("C7: E16")

MyNewFile = Môi trường ("USERPROFILE") & "\ Desktop \ New.xlsx"

Với ứng dụng
.ScreenUpdating = Sai
.DisplayAlerts = Sai
Kết thúc với

Đặt Wbk = Workbooks.Add
MyRange.Copy

Với Wbk
.Sheets (1) .Range ("A1"). PasteSpecial (xlPasteValues)
.SaveAs MyNewFile
.Gần
Kết thúc với

Với ứng dụng
.ScreenUpdating = True
.DisplayAlerts = True
Kết thúc với

Kết thúc Sub [/ CODE]


The code is exactly what I meant. I thank you very much. you're so good
 
Upvote 0
HI,
thanks for the feedback, you can mark the thread as solved.
 
Upvote 0

Forum statistics

Threads
1,214,405
Messages
6,119,315
Members
448,886
Latest member
GBCTeacher

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