PC Review


Reply
Thread Tools Rate Thread

Create macro button

 
 
=?Utf-8?B?U2FyYWg=?=
Guest
Posts: n/a
 
      5th Jun 2007
I am looking to create a macro button which when pressed will complete the
following macro. (This will be used by other employess...fyi)

Sub Summary_cells_from_Different_Workbooks_1()
Dim FileNameXls As Variant
Dim SummWks As Worksheet
Dim ColNum As Integer
Dim myCell As Range, Rng As Range
Dim RwNum As Long, FNum As Long, FinalSlash As Long
Dim ShName As String, PathStr As String
Dim SheetCheck As String, JustFileName As String
Dim JustFolder As String

ShName = "Info"
Set Rng = Range("A2:K2")

'Select the files with GetOpenFilename
FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files,
*.xl*", _
MultiSelect:=True)

If IsArray(FileNameXls) = False Then
'do nothing
Else
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

'Add a new workbook with one sheet for the Summary
Set SummWks = Workbooks.Add(1).Worksheets(1)

'The links to the first workbook will start in row 2
RwNum = 1

For FNum = LBound(FileNameXls) To UBound(FileNameXls)
ColNum = 1
RwNum = RwNum + 1
FinalSlash = InStrRev(FileNameXls(FNum), "\")
JustFileName = Mid(FileNameXls(FNum), FinalSlash + 1)
JustFolder = Left(FileNameXls(FNum), FinalSlash - 1)

'copy the workbook name in column A
SummWks.Cells(RwNum, 1).Value = JustFileName

'build the formula string
JustFileName = WorksheetFunction.Substitute(JustFileName, "'",
"''")
PathStr = "'" & JustFolder & "\[" & JustFileName & "]" & ShName
& "'!"

On Error Resume Next
SheetCheck = ExecuteExcel4Macro(PathStr & Range("A1").Address(,
, xlR1C1))
If Err.Number <> 0 Then
'If the sheet not exist in the workbook the row color will
be Yellow.
SummWks.Cells(RwNum, 1).Resize(1, Rng.Cells.Count + 1) _
.Interior.Color = vbYellow
Else
For Each myCell In Rng.Cells
ColNum = ColNum + 1
SummWks.Cells(RwNum, ColNum).Formula = _
"=" & PathStr & myCell.Address
Next myCell
End If
On Error GoTo 0
Next FNum

' Use AutoFit to set the column width in the new workbook
SummWks.UsedRange.Columns.AutoFit

MsgBox "The Summary is ready, save the file if you want to keep it"

With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End If
End Sub
 
Reply With Quote
 
 
 
 
Susan
Guest
Posts: n/a
 
      6th Jun 2007
one of the easiest, & most visually interesting (i think) ways is to
insert an autoshape into the spreadsheet. format it any color you
want, add text saying like "Click me to run summary", and then (most
important) format it & in the properties tab uncheck the "print"
box...... this way it's there, visible to your users, but doesn't
print.
then right click the shape & choose "assign macro" & choose this one.
now everytime they click it, your macro will run.
you can use a Forms or ControlToolbox command button, but they are
kind of dull & boring-looking. ha ha

hth!
susan


On Jun 5, 5:41 pm, Sarah <S...@discussions.microsoft.com> wrote:
> I am looking to create a macro button which when pressed will complete the
> following macro. (This will be used by other employess...fyi)
>
> Sub Summary_cells_from_Different_Workbooks_1()
> Dim FileNameXls As Variant
> Dim SummWks As Worksheet
> Dim ColNum As Integer
> Dim myCell As Range, Rng As Range
> Dim RwNum As Long, FNum As Long, FinalSlash As Long
> Dim ShName As String, PathStr As String
> Dim SheetCheck As String, JustFileName As String
> Dim JustFolder As String
>
> ShName = "Info"
> Set Rng = Range("A2:K2")
>
> 'Select the files with GetOpenFilename
> FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files,
> *.xl*", _
> MultiSelect:=True)
>
> If IsArray(FileNameXls) = False Then
> 'do nothing
> Else
> With Application
> .Calculation = xlCalculationManual
> .ScreenUpdating = False
> End With
>
> 'Add a new workbook with one sheet for the Summary
> Set SummWks = Workbooks.Add(1).Worksheets(1)
>
> 'The links to the first workbook will start in row 2
> RwNum = 1
>
> For FNum = LBound(FileNameXls) To UBound(FileNameXls)
> ColNum = 1
> RwNum = RwNum + 1
> FinalSlash = InStrRev(FileNameXls(FNum), "\")
> JustFileName = Mid(FileNameXls(FNum), FinalSlash + 1)
> JustFolder = Left(FileNameXls(FNum), FinalSlash - 1)
>
> 'copy the workbook name in column A
> SummWks.Cells(RwNum, 1).Value = JustFileName
>
> 'build the formula string
> JustFileName = WorksheetFunction.Substitute(JustFileName, "'",
> "''")
> PathStr = "'" & JustFolder & "\[" & JustFileName & "]" & ShName
> & "'!"
>
> On Error Resume Next
> SheetCheck = ExecuteExcel4Macro(PathStr & Range("A1").Address(,
> , xlR1C1))
> If Err.Number <> 0 Then
> 'If the sheet not exist in the workbook the row color will
> be Yellow.
> SummWks.Cells(RwNum, 1).Resize(1, Rng.Cells.Count + 1) _
> .Interior.Color = vbYellow
> Else
> For Each myCell In Rng.Cells
> ColNum = ColNum + 1
> SummWks.Cells(RwNum, ColNum).Formula = _
> "=" & PathStr & myCell.Address
> Next myCell
> End If
> On Error GoTo 0
> Next FNum
>
> ' Use AutoFit to set the column width in the new workbook
> SummWks.UsedRange.Columns.AutoFit
>
> MsgBox "The Summary is ready, save the file if you want to keep it"
>
> With Application
> .Calculation = xlCalculationAutomatic
> .ScreenUpdating = True
> End With
> End If
> End Sub



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a button with a macro vba franco monte Microsoft Excel New Users 0 7th Mar 2010 07:53 PM
Create button to run Macro? Joe M. Microsoft Excel Misc 3 3rd Jun 2008 02:17 PM
How to create button and associate a macro with it. Nils Titley Microsoft Excel Programming 4 20th Mar 2008 02:50 PM
Re: create a button and assign a macro Dean Microsoft Excel Programming 2 22nd Sep 2005 05:16 PM
Create cmd button on spreadsheet to run Macro HappyDevil24 Microsoft Excel Programming 5 24th Mar 2004 06:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:28 AM.