macro with If and Else to copy if Yes and leave the same if NO

Jeffreyxx01

Board Regular
Joined
Oct 23, 2017
Messages
156
Hi guys,

I have my macro here and I would like it to copy the range if the Cell W5 is Yes and if NO copy just the selected range

I have the macro but the yes/no thing is quite hard.

Thanks for the help.

Code:
Sub OpenFile_PW()   
   Dim Fname As String
   Dim Wbk As Workbook
   Dim Sht As Worksheet
   
   Set Sht = ActiveWorkbook.Sheets("PW")
   ChDrive "W:"
   ChDir "W:\Insights Team\ALL ACADEMIC\Reporting\Weekly RAM\Pathways"
   Fname = Application.GetOpenFilename(FileFilter:="xls Files (*.xls*), *.xls*", Title:="Select a file", MultiSelect:=False)
   If Fname = "False" Then
      MsgBox "no file selected"
      Exit Sub
   Else
      Set Wbk = Workbooks.Open(Fname)
      With Wbk.Sheets("Summary for BP")
         Sht.Range("s6:V27").ClearContents
         .Range("i7:L28").Copy
         Sht.Range("s6").PasteSpecial xlPasteValues
      If Range("W5")= Yes
      Sht.Range("W6:W27").ClearContents
         .Range("M7:M28").Copy
         Sht.Range("W6").PasteSpecial xlPasteValues
      Else: Range("W5") = No
         Sht.Range("s6:V27").ClearContents
         .Range("i7:L28").Copy
         Sht.Range("s6").PasteSpecial xlPasteValues
         
Application.CutCopyMode = False
      End With
 Application.DisplayAlerts = False
      Wbk.Close , False
   End If


End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this.
Code:
Sub OpenFile_PW()
   Dim Fname As String
   Dim Wbk As Workbook
   Dim Sht As Worksheet
   
   Set Sht = ActiveWorkbook.Sheets("PW")
   
   ChDrive "W:"
   ChDir "W:\Insights Team\ALL ACADEMIC\Reporting\Weekly RAM\Pathways"
   Fname = Application.GetOpenFilename(FileFilter:="xls Files (*.xls*), *.xls*", Title:="Select a file", MultiSelect:=False)
   
   If Fname = "False" Then
      MsgBox "no file selected"
      Exit Sub
   Else
      Set Wbk = Workbooks.Open(Fname)
      
      With Wbk.Sheets("Summary for BP")
         Sht.Range("s6:V27").ClearContents
         .Range("i7:L28").Copy
         Sht.Range("s6").PasteSpecial xlPasteValues
         
        If Range("W5") = "Yes" Then
            Sht.Range("W6:W27").ClearContents
            .Range("M7:M28").Copy
            Sht.Range("W6").PasteSpecial xlPasteValues
        ElseIf Range("W5") = "No" Then
            Sht.Range("s6:V27").ClearContents
            .Range("i7:L28").Copy
            Sht.Range("s6").PasteSpecial xlPasteValues
        End If
         
      End With
      
    End If
      
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    Wbk.Close False
   
End Sub
 
Upvote 0
Hi Norie,

That seems to work but unfortunately when I use the Yes in W5 it does not overwrite the data from it.

In the Cell W5 I use a data validation Yes No if that changes something?
 
Upvote 0
I found the solution to my problem, the code works fine now.
In red was the missing parameters


Code:
Sub OpenFile_PW()   Dim Fname As String
   Dim Wbk As Workbook
   Dim Sht As Worksheet
   
   Set Sht = ActiveWorkbook.Sheets("PW Jan")
   
   ChDrive "W:"
   ChDir "W:\Insights Team\ALL ACADEMIC\Reporting\Weekly RAM\Pathways"
   Fname = Application.GetOpenFilename(FileFilter:="xls Files (*.xls*), *.xls*", Title:="Select a file", MultiSelect:=False)
   
   If Fname = "False" Then
      MsgBox "no file selected"
      Exit Sub
   Else
      Set Wbk = Workbooks.Open(Fname)
      
      With Wbk.Sheets("Summary for BP")
        If [COLOR=#ff0000]Sht.[/COLOR]Range("W5") = "Yes" Then
            Sht.Range("S6:W27").ClearContents
            .Range("I7:M28").Copy
            Sht.Range("S6").PasteSpecial xlPasteValues
        ElseIf [COLOR=#ff0000]Sht[/COLOR].Range("W5") = "No" Then
            Sht.Range("S6:V27").ClearContents
            .Range("I7:L28").Copy
            Sht.Range("s6").PasteSpecial xlPasteValues
        End If
         
      End With
      
    End If
      
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    Wbk.Close False
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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