How do i improve my existing code for GetOpenfile function

Zakky

Please can someone improve my current code - opens a box for the user to select a file and when the correct file is selected it pastes the data to correct tab. However, I am not sure how to improve it as follows:

1) Can i specify the name of the target file (costings) in the code so it only works on that file? At the moment the user can select any file and it looks for the sheet name "SAP" and if it can't find it doesn't work - goes to debug mode - i would like it to give the message "Macro will end now because you failed to select the correct file".

2) Same as 1 above but the message now is "Macro will end now because there is no SAP sheet in the file". This message will come up after selecting the correct file but there's no SAP sheet in it.

2) Clear the existing data in the target "SAP" sheet in the given range (do i have the correct code at the moment?)

3) When the user clicks cancel (because they don't want to proceed) a message appears and sub ends - "The macro will end now because you did not select the file"

Thank you!

'Set source workbook
 Set wb = ActiveWorkbook
'Open the target workbook
 vFile = Application.GetOpenFilename("Excel-files,*.xlsm", 1, "Please select file", , False)
 Set wb2 = ActiveWorkbook
'clear existing values form target book
 wb2.Worksheets("SAP").Range("A:N").ClearContents
'Copy/paste the visible data to the new workbook
 wb.Worksheets("sheet1").Cells.SpecialCells(xlCellTypeVisible).Copy
 wb2.Worksheets("SAP").Range("A1").PasteSpecial Paste:=xlPasteValues
 ActiveSheet.Range("A1").Select
 Application.CutCopyMode = False
 Windows("Format MCP6 Final.xlsm").Close
Doktor OSwaldo

I think you could have solved most of the questions for your self by searching the specific Problem, but i gonna try to help you anyway.

1 If you know the Name of the file, why do you let the user choose it ? Anyway you can check the Name with "workbook.name" and test whatever you want

2 That's easy done by this:

    If (Worksheets("SAP").Name = "") Then
      MsgBox "Macro will end now because there is no SAP sheet in the file"
      Exit Sub
    End If

2.2? Depends on what you try to delete. ClearContents will not delete any formatings.

3 You have to define vFile as Variant and check it for false:

vFile = Application.GetOpenFilename("Excel-files,*.xlsm", 1, "Please select file", , False)

if vFile = false then 
  MsgBox "The macro will end now because you did not select the file"
  Exit Sub
End If

But for the future, split the 4 question in 4 Posts, and try do describe what you have tried, and where you have Problems. And Questions like: "Is it correct what I have done?" are not suitable for SO. Try it and if you have Problems ask why These Problems occur.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I improve my Python code about heap sort?

From Dev

One of the method from my python code fails for some unittests. How do I improve it?

From Dev

How do i improve my code to load images from remote server more effiecintly without UI lag?

From Dev

how do i define a php function with my code

From Dev

How do I correctly use the mouseleave function in my code?

From Dev

How can I improve my code to reduce the synthesis time?

From Dev

How can I improve my webpage code to be scalable for smaller sizes?

From Dev

How can I improve my SQL code for correct results?

From Dev

How can I improve my code to reduce the synthesis time?

From Dev

How can I improve my SQL code for correct results?

From Dev

How can i improve my python code regarding while loops

From Dev

How can I improve my code to handle large numbers?

From Dev

How can I improve my "if and else if" VBA? code

From Java

How can I improve my spiral index function?

From Dev

PL\SQL How can I improve the execution speed of my function?

From Dev

How can i improve my search function ? PHP + MySQL

From Dev

How do I improve my Neural Network output?

From Dev

How do I improve the performance of my VirtualBox guest?

From Dev

How do I improve the quality of my FFsplit Twitch stream?

From Dev

How do I improve the performance of my VirtualBox guest?

From Dev

How do I call a function in C# that adds text to your existing code?

From Dev

How do I reconnect my project to my existing GitHub repository

From Dev

How do I improve the performance of the following code in python

From Dev

Why is this error occurring, and how do I improve this function

From Dev

How can I create a method using my existing code

From Dev

How do I use the ActionEvent function with my code to do math equations

From Dev

How do i check/validate existing file with my requirement in python

From Dev

How do I find my shiny app's existing css?

From Dev

How do I make an existing project on my machine into a GitHub project?

Related Related

  1. 1

    How do I improve my Python code about heap sort?

  2. 2

    One of the method from my python code fails for some unittests. How do I improve it?

  3. 3

    How do i improve my code to load images from remote server more effiecintly without UI lag?

  4. 4

    how do i define a php function with my code

  5. 5

    How do I correctly use the mouseleave function in my code?

  6. 6

    How can I improve my code to reduce the synthesis time?

  7. 7

    How can I improve my webpage code to be scalable for smaller sizes?

  8. 8

    How can I improve my SQL code for correct results?

  9. 9

    How can I improve my code to reduce the synthesis time?

  10. 10

    How can I improve my SQL code for correct results?

  11. 11

    How can i improve my python code regarding while loops

  12. 12

    How can I improve my code to handle large numbers?

  13. 13

    How can I improve my "if and else if" VBA? code

  14. 14

    How can I improve my spiral index function?

  15. 15

    PL\SQL How can I improve the execution speed of my function?

  16. 16

    How can i improve my search function ? PHP + MySQL

  17. 17

    How do I improve my Neural Network output?

  18. 18

    How do I improve the performance of my VirtualBox guest?

  19. 19

    How do I improve the quality of my FFsplit Twitch stream?

  20. 20

    How do I improve the performance of my VirtualBox guest?

  21. 21

    How do I call a function in C# that adds text to your existing code?

  22. 22

    How do I reconnect my project to my existing GitHub repository

  23. 23

    How do I improve the performance of the following code in python

  24. 24

    Why is this error occurring, and how do I improve this function

  25. 25

    How can I create a method using my existing code

  26. 26

    How do I use the ActionEvent function with my code to do math equations

  27. 27

    How do i check/validate existing file with my requirement in python

  28. 28

    How do I find my shiny app's existing css?

  29. 29

    How do I make an existing project on my machine into a GitHub project?

HotTag

Archive