VBA if i cancel the update it clears the sheet

malveiro

New Member
Joined
May 13, 2015
Messages
32
Hello

I've have a userform , and when i click on button to update a sheet , execute the module above and it's working ,it opens a file explorer to choose the file i select the file , it clears the sheet , and it updates the values.
The problem is , if i cancel the update in the file explorer , it gives me the MsgBox " Actualização cancelada" but also clears the sheet
How can i prevent , if i cancel , to clear the sheet ?
I want is , if i cancel , to not update e sheet1 remains the same before clicking the button
Thank you in advance

VBA Code:
Sub copy_worksheet()

On Error GoTo Cancel

Application.ScreenUpdating = False

ThisWorkbook.Sheets("Sheet1").Range("A2:D5000").Clear

Set Source_workbook = Workbooks.Open(Application.GetOpenFilename(FileFilter:="Excel Files,*.xls*"))

Source_workbook.Sheets("Sheet1").Range("A2:D5000").Copy _
ThisWorkbook.Sheets("Sheet1").Range("A2:D5000")

Source_workbook.Close SaveChanges:=False

Application.ScreenUpdating = True

MsgBox "Lista de Impressoras actualizada "
Exit Sub
If Err.Number > 0 Then GoTo Cancel
Cancel:

MsgBox " Actualização cancelada"

End Sub
 
You can put it in memory but you can also put the name in a cell. Then refer to that cell in your userform_initialize.
The name will be put in the cell right to the imported data.

VBA Code:
Sub jec()
 Dim ar, fn
 Application.ScreenUpdating = False
 With Application.FileDialog(3)
   .Filters.Clear
   .Filters.Add "Excel Files", "*.xlsx; *.xlsm"
   If .Show Then
      fn = Split(Dir(.SelectedItems(1)), ".")(0)
      With Workbooks.Open(.SelectedItems(1))
         ar = .Sheets("Sheet1").Range("A2:D5000")
         With ThisWorkbook.Sheets("Sheet1").Range("A2")
            .Resize(UBound(ar), UBound(ar, 2)) = ar
            .Offset(-1, UBound(ar, 2) + 1) = fn
         End With
        .Close 0
      End With
   End If
 End With
End Sub
Thank you so much , last nigth i made a workaround with a textbox instead of a label and it woked .
I' ll try with your code
Best regards
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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