Little Tweek needed in VBA Code

GaryG9595

Board Regular
Joined
Jun 13, 2014
Messages
74
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have a code (Below) that works ok. This code goes into the file that I name on my "CG" sheet from A1.
Pulls the data I need perfectly, except if the file has two sheets on it, it pulls from the sheet that was open during it's last save.

For Example, the workbook has two sheets, one named "CG" and the other "4-13 report". If they made changes to the "4-13 Report", and hit save, when I run this code, I get the "4-13 Report" data and not the "CG" I need.

Currently I am going into each Workbook, selecting the "CG" sheet, clearing the data, unhiding the data, and then clicking save. Once I do that, this Code works great.
I would like this code to go into the workbook and always pull from the "CG" Sheet. Also Unhide and clear the data before the pull just in case they have some filtered or hidden.

Does anyone know how to add to this code to make the above red sentence happen?

Thank you in advance,
Gary

Sub simpleXlsMerger1()
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")

Set dirObj = mergeObj.Getfolder(Sheets("CG").Range("A1").Value)
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
Range("A8:AC" & Range("A65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(1).Activate
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
What do you mean by "clearing the data"?
 
Upvote 0
What do you mean by "clearing the data"?
Hi Fluff,
If they have anything filtered, I go in and clear the filter and sort state for the current range of data. On the DATA Tab next to the filter button, I just hit clear.
Hope that helps.
Thansk,
Gary
 
Upvote 0
Ok, how about
VBA Code:
Sub simpleXlsMerger1()
   Dim bookList As Workbook
   Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
   Application.ScreenUpdating = False
   Set mergeObj = CreateObject("Scripting.FileSystemObject")
   
   Set dirObj = mergeObj.Getfolder(Sheets("CG").Range("A1").Value)
   Set filesObj = dirObj.Files
   For Each everyObj In filesObj
      Set bookList = Workbooks.Open(everyObj)
      With bookList.Sheets("CG")
         .AutoFilterMode = False
         .Range("A8:AC" & .Range("A" & .Rows.Count).End(xlUp).Row).Copy
         ThisWorkbook.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
      End With
      Application.CutCopyMode = False
      bookList.Close
   Next
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub simpleXlsMerger1()
   Dim bookList As Workbook
   Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
   Application.ScreenUpdating = False
   Set mergeObj = CreateObject("Scripting.FileSystemObject")
  
   Set dirObj = mergeObj.Getfolder(Sheets("CG").Range("A1").Value)
   Set filesObj = dirObj.Files
   For Each everyObj In filesObj
      Set bookList = Workbooks.Open(everyObj)
      With bookList.Sheets("CG")
         .AutoFilterMode = False
         .Range("A8:AC" & .Range("A" & .Rows.Count).End(xlUp).Row).Copy
         ThisWorkbook.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
      End With
      Application.CutCopyMode = False
      bookList.Close
   Next
End Sub
That worked. Thanks you so much. This is also helping me understand coding as well. Thank you again for your help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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