Deleting a Particular Column with IF Condition before Pasting

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello Folks, Please go through the code i mentioned. The code is intended to copy all the sheets from source workbook and paste to another workbook's single sheet. My requirement is before pasting all the sheets it should look for E1 cell in all the sheets, IF the E1 cell is blank it should delete the entire column and then paste ELSE just copy paste all the sheet. Also if possible please add the the file open default location should be in desktop.



Sub Import()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range

Set wb1 = ActiveWorkbook
Set PasteStart = [Sheet1!C2]

Sheets("Sheet1").Select
Cells.Select
Selection.ClearContents

FileToOpen = Application.GetOpenFilename _
(Title:="Please Select Your File", _
FileFilter:="Report Files *.xlsx (*.xlsx),")

If FileToOpen = False Then
MsgBox "No XML Selected.", vbExclamation, "ERROR"
Exit Sub
Else
Set wb2 = Workbooks.Open(Filename:=FileToOpen)

For Each Sheet In wb2.Sheets
With Sheet.UsedRange
.Copy PasteStart
Set PasteStart = PasteStart.Offset(.Rows.Count)
End With
Next Sheet

End If

wb2.Close
End Sub
 
How about
Code:
Sub Import()

   Dim wb1 As Workbook
   Dim wb2 As Workbook
   Dim Ws As Worksheet
   Dim PasteStart As Range
   Dim FileToOpen As String
   
Application.ScreenUpdating = False

   Set wb1 = ActiveWorkbook
   Set PasteStart = wb1.Sheets("Sheet1").Range("C2")
   
   Sheets("Sheet1").Cells.ClearContents
   
   ChDrive "C:"
   ChDir "[COLOR=#ff0000]C:\Users\Fluff\Desktop\[/COLOR]"
   
   FileToOpen = Application.GetOpenFilename _
      (Title:="Please Select Your File", _
      FileFilter:="Report Files *.xlsx (*.xlsx),")
   
   If FileToOpen = False Then
      MsgBox "No XML Selected.", vbExclamation, "ERROR"
      Exit Sub
   End If
   Set wb2 = Workbooks.Open(FileName:=FileToOpen)
   
   For Each Ws In wb2.Worksheets
      If Ws.Range("E1") = "" Then Ws.Columns("E").Delete
      With Ws.UsedRange
         .copy PasteStart
         Set PasteStart = PasteStart.Offset(.Rows.Count)
      End With
   Next Ws
   
   wb2.Close
End Sub
Change the line in red, to match your default path

Hello Fluff, adding to the above code, Is it possible to delete the content before the last space of A2 cell in all sheets before pasting? for Example if the cell contains 81 A 1 i want to delete 81 A and the result would be 1. please help. Thank you.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try
Code:
Sub Import()

   Dim wb1 As Workbook
   Dim wb2 As Workbook
   Dim Ws As Worksheet
   Dim PasteStart As Range
   Dim FileToOpen As String
   
Application.ScreenUpdating = False

   Set wb1 = ActiveWorkbook
   Set PasteStart = wb1.Sheets("Sheet1").Range("C2")
   
   Sheets("Sheet1").Cells.ClearContents
   
   ChDrive "C:"
   ChDir "C:\Users\" & Environ("Username") & "\Desktop\"
   
   FileToOpen = Application.GetOpenFilename _
      (Title:="Please Select Your File", _
      FileFilter:="Report Files *.xlsx (*.xlsx),")
   
   If FileToOpen = "False" Then
      MsgBox "No XML Selected.", vbExclamation, "ERROR"
      Exit Sub
   End If
   Set wb2 = Workbooks.Open(FileName:=FileToOpen)
   
   For Each Ws In wb2.Worksheets
      With Ws.Range("A2")
         .Value = Right(.Value, Len(.Value) - InStrRev(.Value, " "))
      End With
      If Ws.Range("E1") = "" Then Ws.Columns("E").Delete
      With Ws.UsedRange
         .Copy PasteStart
         Set PasteStart = PasteStart.Offset(.Rows.Count)
      End With
   Next Ws
   
   wb2.Close True
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,225
Members
453,025
Latest member
Hannah_Pham93

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