Macro Question - Prepare Files For Merge Macro

JediYodaNT

New Member
Joined
Apr 20, 2007
Messages
38
Hello there my fellow Excel junkies...lol.
I have been staring at some code for the past couple days and I am afraid to say, my VB knowledge is so minimal that I'm not making sense of the macros I use. I have a couple that we use for certain projects on a monthly basis. Their functionality tells me that what I need in a new macro should be possible. So, this is where I turn to the most helpful people I've ever worked with...the users for the Mr Excel forums. For years you guys have been my lifesavers.

So, let me get to the meet of my project. I manage a process each month where I take a master file, review it for values that our offcers need to look into. I then have a macro that was created before I joined the team. This macro hides columns that the officers don't need to see, and then splits the file up into individual files for each officer. From there, I send out the files to each officer, and over the next week, they would return them before their deadline. Normally this kind of trickle made this managable, however this month, we've been asked to change up the process. We uploaded the files into one document library on SharePoint for them to review and maintain with a deadline for the files to be removed and consolidated. This is where I could use some help.

The end goal is to create a macro that can open each of the 26 officer files, unhide any hidden rows and columns, remove any filters and then, copy the rows (minus the header), and paste them all onto one master file. I have a macro that I use for another process that can open a folder of files and pull over the data onto a master file, but it's too specific and I've not been able to translate it enough to reverse engineer it. Any help would be greatly apprecaited.

I installed the RDBMerge Add-In yesterday, and I believe it might work if I could run another macro beforehand that opened each of the files, unhid everything, unfroze the view, removed all filters, then saved the file as a clean and ready file. From there, I think the RDBMerge Add-In might have worked with some tweaking of the results. (All columns needed to have the formats readjusted to Currency, Short Date, etc...)

I'm open for any advice you gurus might have. I trust your years of experience in this.


In case you'd like to see the macro I was trying to reverse engineer:




Code:
Sub Step_1_mcr_PSR_UpdateCollectorWorksheets()
'
' Step_1_mcr_PSR_UpdateCollectorWorksheets Macro
'
Dim strDir As String
Dim strFileName As String
Dim strPath As String
Dim i As Integer
Dim x As Integer
Dim aryFileNames()
ReDim aryFileNames(1 To 100)
Dim intRows As Integer

strDir = ActiveWorkbook.Path ' current path
strFileName = "*.xlsx"   'file type to search for
strPath = strDir & "\" & strFileName  'combined dir path and name

i = 1  'counter
x = 1 'counter
aryFileNames(i) = Dir(strPath) 'sets the first member of the array to the first file in the path

Do While aryFileNames(i) <> Empty  'while the array member value is not empty
i = i + 1 'increase counter by one
If i > UBound(aryFileNames) Then  'if the counter is greater than the number of members in the array
     ReDim Preserve aryFileNames(UBound(aryFileNames) + 50) 'then extend it another 50
End If
aryFileNames(i) = Dir 'sets the next member in the array to the next file name
Loop 'keep looping while array member value is not empty
i = i - 1 'remove last increase, true up counter

While i > 0

    ChDir strDir   'change dir
    Workbooks.Open Filename:= _
        strDir & "\" & aryFileNames(x)  'open the file in the array
    Sheets("My Accounts").Select
    
    If Worksheets("My Accounts").FilterMode = True Then
        Cells.Select
        Selection.AutoFilter
    End If
    
    Selection.EntireRow.Hidden = False
    Selection.EntireColumn.Hidden = False

'Code to copy and paste data
   If UCase(Range("M1").Value) = "PSR COMMENTS" And UCase(Range("B1").Value) = "ACCOUNT NUMBER" Then
      Range("B1048576").Select  'select last row
      Selection.End(xlUp).Select  'find the last record
      intRows = Selection.Row  'set the row number for that last record
      
        Columns("A:A").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("A2").Select
        ActiveCell.FormulaR1C1 = Left(Right(aryFileNames(x), 9), 4)
    
      Rows("2:" & intRows).Select  'select the rows to be copied
      Selection.Copy  'copy
      Windows("PSR_ConsolidateWorksheet_Processor.xlsm").Activate  'switch files
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False  'paste
    
      Application.CutCopyMode = False 'clear clipboard
      ActiveWorkbook.Save
    
      Windows(aryFileNames(x)).Activate  'switch files
      ActiveWindow.Close saveChanges:=False  'exit file
        
      Windows("PSR_ConsolidateWorksheet_Processor.xlsm").Activate  'switch files
      Range("B1048576").Select  'select last row
      Selection.End(xlUp).Select  'find the last record
      intRows = Selection.Row  'set the row number for that last record
      Cells(intRows + 1, 1).Select  'go to first column of the next or new record
      Sheets("WMS Check").Select
      Cells(x, 1) = aryFileNames(x)
      Sheets("Sheet1").Select
    
    Else:
           Application.CutCopyMode = False 'clear clipboard
           ActiveWindow.Close saveChanges:=False  'exit file
           Windows("PSR_ConsolidateWorksheet_Processor.xlsm").Activate  'switch files
           Sheets("WMS Check").Select
           Cells(x, 2) = aryFileNames(x)
           Sheets("Sheet1").Select
    End If
    
    x = x + 1  'increase array counter
    i = i - 1  'decrease while counter

Wend

Range("A1").Select
MsgBox "Consolidation is complete."

End Sub
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
LOL...I was just editing my original post to add notes about trying RDBMerge... Great minds think alike Comfy. Sadly, the add-in didn't quite work with the source data that was provided, as it seems that the officers have different tweaks that they make to their files before they are done. I think a second macro that "prepares the files" might be needed to make RDBMerge work correctly. Additionally, I was kinda disappointed that the new "Master File" that the merge created needed a lot of clean up before I could use it. All the columns reverted to "general" instead of maintaining their formats. Short Dates were just numbers, Currency was turned into numbers...and the scary part...the account numbers...which often have leading zeroes that must be maintained...they were being truncated.
 
Upvote 0
Sadly, working for the company I do, I am prohibited from "Installing" Power Query. Anything that requires an install also requires Admin Rights for my PC, which I do not have. The RDBMerge Add-In, thankfully didn't require a true install...so I was able to add it, although it's not cutting the mustard.

Thank you though.
 
Upvote 0
Ok, not to worry.

1. Is this macro to work on all sheets within the returned workbooks or just a specific sheet?
2. Will the columns be in the same order or is there a chance that they will be moved?
 
Upvote 0

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

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