Excel 2003 VBA. How do I Tweak this code.

madmiddle

New Member
Joined
Mar 8, 2012
Messages
45
Good Afternoon people.

I have been using this site for various macros over the last 3-4 years and have never needed to register as I found most of the answers. But I have now cause I never seem to find just what i'm looking for this time.

I do have very basic VB knowledge, so please excuse me with the more advance parts of VB.

I would like some help in tweaking another code if possible. The macro I found is this:
Code:
Option Explicit

Sub ConsolidateSheets()
'Author:    Jerry Beaucaire
'Date:      6/26/2009
'Updated:   6/23/2010
'Merge all sheets in a workbook into one summary sheet (stacked)
'Data is sorted by a specific column name
Dim cs As Worksheet, WS As Worksheet
Dim LR As Long, NR As Long, sCol As Long
Dim sName As Boolean, SortStr As String
Application.ScreenUpdating = False

'From the headers in data sheets, enter the column title to sort by when finished
SortStr = "Equipment Type"

'Add consolidation sheet if needed
If Not Evaluate("ISREF(Consolidate!A1)") Then _
    Worksheets.Add(After:=Worksheets(Worksheets.count)).Name = "All FCAS IT"

'Option to add sheet names to consolidation report
sName = MsgBox("Add sheet names to consolidation report?", vbYesNo + vbQuestion) = vbYes

'Setup
Set cs = ActiveWorkbook.Sheets("Consolidate")
cs.Cells.ClearContents
NR = 1

'Process each data sheet
    For Each WS In Worksheets
        If WS.Name <> "Consolidate" Then
            LR = WS.Range("A" & WS.Rows.count).End(xlUp).Row
            'customize this section to copy what you need
            If NR = 1 Then      'copy titles and data to start the consolidation
                WS.Range("A1", WS.Cells(1, Columns.count).End(xlToLeft)).Copy
                If sName Then
                    cs.Range("B1").PasteSpecial xlPasteAll
                Else
                    cs.Range("A1").PasteSpecial xlPasteAll
                End If
                NR = 2
            End If
            
            WS.Range("A2:BB" & LR).Copy     'copy data

            If sName Then       'paste and add sheet names if required
                cs.Range("B" & NR).PasteSpecial xlPasteValues
                cs.Range("A" & NR, cs.Range("B" & cs.Rows.count).End(xlUp).Offset(0, -1)) = WS.Name
            Else
                cs.Range("A" & NR).PasteSpecial xlPasteValues
            End If
            
            NR = cs.Range("A" & cs.Rows.count).End(xlUp).Row + 1
        End If
    Next WS

'Sort
    LR = cs.Range("A" & cs.Rows.count).End(xlUp).Row
    On Error Resume Next
    sCol = cs.Cells.Find(SortStr, After:=cs.Range("A1"), LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
    cs.Range("A1:BB" & LR).Sort Key1:=cs.Cells(2, sCol + (IIf(sName, 1, 0))), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

'Cleanup
    If sName Then cs.[A1] = "Sheet"
    cs.Rows(1).Font.Bold = True
    cs.Cells.Columns.AutoFit
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    cs.Activate
    Range("A1").Select
    Set cs = Nothing
End Sub

I have tried it within the worksheet i have and it does basically what I want.

I have a list of IT equipment in one spreadsheet with several tabs dividing the equipment up into buildings that they are in. There are 7 tabs all with about 500 rows of: Equipment type/Make/Model/Barcode.

Now I would like to be able to use this code but instead of adding a new sheet, add the resultant merge into a new workbook and then save it with a generic title of "Warton FCAS IT" & today's date.

Now the next part I would need to add:

My name into Cell J1
Date and time of the save in J2
and folder location hyperlink in J3 (if that's possible)


any pointers would be very much appreciated.

TYIA

Madmiddle
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
Option Explicit

Sub ConsolidateSheets()
'Author:    Jerry Beaucaire
'Date:      6/26/2009
'Updated:   6/23/2010
'Merge all sheets in a workbook into one summary sheet (stacked)
'Data is sorted by a specific column name
Dim cs As Worksheet, WS As Worksheet
Dim LR As Long, NR As Long, sCol As Long
Dim sName As Boolean, SortStr As String
Application.ScreenUpdating = False

'From the headers in data sheets, enter the column title to sort by when finished
SortStr = "Equipment Type"

'Add consolidation sheet if needed
If Not Evaluate("ISREF(All FCAS IT!A1)") Then _
    Worksheets.Add(After:=Worksheets(Worksheets.count)).Name = "All FCAS IT"

'Option to add sheet names to consolidation report
sName = MsgBox("Add Building Number to the consolidation report?", vbYesNo + vbQuestion) = vbYes

'Setup
Set cs = ActiveWorkbook.Sheets("All FCAS IT")
cs.Cells.ClearContents
NR = 1

'Process each data sheet
    For Each WS In Worksheets
        If WS.Name <> "All FCAS IT" Then
            LR = WS.Range("A" & WS.Rows.count).End(xlUp).Row
            'customize this section to copy what you need
            If NR = 1 Then      'copy titles and data to start the consolidation
                WS.Range("A1", WS.Cells(1, Columns.count).End(xlToLeft)).Copy
                If sName Then
                    cs.Range("B1").PasteSpecial xlPasteAll
                Else
                    cs.Range("A1").PasteSpecial xlPasteAll
                End If
                NR = 2
            End If
            
            WS.Range("A2:BB" & LR).Copy     'copy data

            If sName Then       'paste and add sheet names if required
                cs.Range("B" & NR).PasteSpecial xlPasteValues
                cs.Range("A" & NR, cs.Range("B" & cs.Rows.count).End(xlUp).Offset(0, -1)) = WS.Name
            Else
                cs.Range("A" & NR).PasteSpecial xlPasteValues
            End If
            
            NR = cs.Range("A" & cs.Rows.count).End(xlUp).Row + 1
        End If
    Next WS

'Sort
    LR = cs.Range("A" & cs.Rows.count).End(xlUp).Row
    On Error Resume Next
    sCol = cs.Cells.Find(SortStr, After:=cs.Range("A1"), LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
    cs.Range("A1:BB" & LR).Sort Key1:=cs.Cells(2, sCol + (IIf(sName, 1, 0))), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

'Cleanup
    If sName Then cs.[A1] = "Sheet"
    cs.Rows(1).Font.Bold = True
    cs.Cells.Columns.AutoFit
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    cs.Activate
    Range("A1").Select
    Set cs = Nothing
End Sub

tweaked code to suit the file new and tab i would like.
 
Upvote 0
Now I would like to be able to use this code but instead of adding a new sheet, add the resultant merge into a new workbook and then save it with a generic title of "Warton FCAS IT" & today's date.

Now the next part I would need to add:

My name into Cell J1
Date and time of the save in J2
and folder location hyperlink in J3 (if that's possible)

What date format do you want in the file name e.g. "Warton FCAS IT mm-dd-yyyy"
What path location do you want to save the new workbook?
What sheet gets the name, save date, link (J1:J3)?
 
Upvote 0
What date format do you want in the file name e.g. "Warton FCAS IT mm-dd-yyyy"
What path location do you want to save the new workbook?
What sheet gets the name, save date, link (J1:J3)?

I am currently using Excel 2003 (work is going to 2010 in September)

That date format would be perfect.

The path that I would normally save it in is a Network drive but it's mapped to P:
so could I have P:\FCAS\IT Admin (though if the code for this is plan to see then anything will do, I could tweak it later)

The newly created spreadsheet needs to have the save date and time on it. so we know that we are talking about the same version of spreadsheet.

Thank you
 
Upvote 0
Replace the "Cleanup" code in your macro with the code below.

Code:
    [color=green]'Cleanup[/color]
    [color=darkblue]Dim[/color] strSavePath [color=darkblue]As[/color] [color=darkblue]String[/color]
    [COLOR="Red"]strSavePath = "C:\Test\"[/COLOR]
    
    cs.Range("J1").Value = "My Name"
    cs.Range("J2").NumberFormat = "mm-dd-yyyy h:mm AM/PM"
    cs.Range("J2").Value = Now
    cs.Range("J3").Formula = "=HYPERLINK(""" & strSavePath & """)"
    [color=darkblue]If[/color] sName [color=darkblue]Then[/color] cs.[A1] = "Sheet"
    cs.Rows(1).Font.Bold = [color=darkblue]True[/color]
    cs.Cells.Columns.AutoFit
    
    [color=green]'Copy Sheets("Consolidate") to a new workbook and save[/color]
    cs.Copy
    [color=darkblue]With[/color] ActiveWorkbook
        [color=green]' Save as Excel 97-2003 (.xls)[/color]
        .SaveAs Filename:=strSavePath & "Warton FCAS IT " & Format(Date, "mm-dd-yyyy"".xls""")
[color=green]'        ' Save as Excel 2007... (.xlsx)[/color]
[color=green]'        .SaveAs Filename:=strSavePath & "Warton FCAS IT " & Format(Date, "mm-dd-yyyy"), FileFormat:=51[/color]
        .Close [color=darkblue]False[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=darkblue]With[/color] Application
        .DisplayAlerts = [color=darkblue]False[/color]
            cs.Delete [color=green]'Delete Sheets("Consolidate") from this workbook[/color]
        .DisplayAlerts = [color=darkblue]True[/color]
        .CutCopyMode = [color=darkblue]False[/color]
        .ScreenUpdating = [color=darkblue]True[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]Set[/color] cs = [color=darkblue]Nothing[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Replace the "Cleanup" code in your macro with the code below.

Absolute Quality thank you very much. It does 99% of what I have wanted. There is a couple of questions if you don't mind.

1. in the original code i have the section
Code:
LR = WS.Range("A" & WS.Rows.Count).End(xlUp).Row
            'customize this section to copy what you need
            If NR = 1 Then      'copy titles and data to start the consolidation
            WS.Range("A1", WS.Cells(1, Columns.Count).End(xlToLeft)).Copy

which i'm assuming copies that data across the top of the first sheet for the title of the new sheet. how do i tweak this to only include A1:F1 ? (I have some columns further on that i don't want to include).

I see the line
Code:
set cs = ActiveWorkbook.sheets ("All FCAS IT")
I'm assuming that once this is set then cs will always refer back to that sheet regardless of what sheet/book you are currently in. (just trying to work it out as the WS is not set so it will process every sheet that is open.


I am trying to customise it for another project as well, and wanted to format a column with the currency format so is this line right:
Code:
cs.Range("D:D").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
 
Last edited:
Upvote 0
Also noticed that this:
Code:
LR = cs.Range("A" & cs.Rows.Count).End(xlUp).Row
    On Error Resume Next
    sCol = cs.Cells.Find(SortStr, After:=cs.Range("A1"), LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
    cs.Range("A1:BB" & LR).Sort Key1:=cs.Cells(2, sCol + (IIf(sName, 1, 0))), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
doesn't sort column B out when I answer yes to:
Code:
sName = MsgBox("Add Got or want to the sheet ?", vbYesNo + vbQuestion) = vbYes
 
Upvote 0
How do i tweak this to only include A1:F1 ? (I have some columns further on that i don't want to include).
Code:
[COLOR="Green"]'Change these...[/COLOR]
WS.Range("A1", WS.Cells(1, Columns.count).End(xlToLeft)).Copy

WS.Range("A2:BB" & LR).Copy     [COLOR="Green"]'copy data[/COLOR]

cs.Range("A1:BB" & LR).Sort Key1:=...

[COLOR="Green"]'To this...[/COLOR]
WS.Range("A1:[COLOR="Red"]F1[/COLOR]").Copy

WS.Range("A2:[COLOR="Red"]F[/COLOR]" & LR).Copy     [COLOR="Green"]'copy data[/COLOR]

cs.Range("A1:[COLOR="Red"]F[/COLOR]" & LR).Sort Key1:=...
-------------------------
I see the line
Rich (BB code):
set cs = ActiveWorkbook.sheets ("All FCAS IT")
I'm assuming that once this is set then cs will always refer back to that sheet regardless of what sheet/book you are currently in. (just trying to work it out as the WS is not set so it will process every sheet that is open.
Yes. cs is a Worksheet object. Once set, it references a specific worksheet in a specific workbook.
FYI; cs.Parent will reference the workbook the cs worksheet is within.

------------------------
I am trying to customise it for another project as well, and wanted to format a column with the currency format so is this line right:
Rich (BB code):
cs.Range("D:D").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
The syntax looks correct. Was there a problem when you tried it?

------------------------
Also noticed that this:
Rich (BB code):
LR = cs.Range("A" & cs.Rows.Count).End(xlUp).Row
    On Error Resume Next
    sCol = cs.Cells.Find(SortStr, After:=cs.Range("A1"), LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
    cs.Range("A1:BB" & LR).Sort Key1:=cs.Cells(2, sCol + (IIf(sName, 1, 0))), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
doesn't sort column B out when I answer yes to:
Rich (BB code):
sName = MsgBox("Add Got or want to the sheet ?", vbYesNo + vbQuestion) = vbYes

Hard to say without seeing your data config. As best I can tell, if you answer Yes, the code is intended to sort on one column to the right of "Equipment Type"(SortStr). I don't know if that's suppose to be column B.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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