dimanche 29 mars 2015

Running Macro On Seperate Workbook

I wrote the following macro to help with some of my work. I want to modify it so that I can open a workbook that contains this macro, run it so that it asks you what workbook (or string of workbooks) you want to run this macro on, opens that/those files, runs the macro on those workbooks, save and close the that/those file.


I tried using a combination of "Workbooks.Open("C:\MyFolder\MyBook.xls")" and "Application.Run "MacroBook!MacroName"" but every way I tried it I always ran into an error which I think came down to the syntax I used in my version of the macro not recognizing the active sheet on the opened workbook.


Sorry if I am leaving any important info out, I am still a newbie but will be happy to provide any other information!



Sub FormatingBlankORMulV()


Dim LastCol As Integer
Dim LastRow As Integer
Dim WS As Worksheet
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim o As Integer

Application.ScreenUpdating = False

For Each WS In Worksheets(Array("FI", "FI - Bkgd", "%CV")) 'Do on these worksheets'

WS.Activate

Columns("D:D").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'Insert Dilutions'
Columns("D:D").Value = Worksheets("Dilution").Columns("D:D").Value

ActiveSheet.Range("D10").Clear

LastCol = Cells(11, Columns.Count).End(xlToLeft).Column 'Last Column # determined on row 11'

LastRow = Cells(Rows.Count, 4).End(xlUp).Row 'Last Row # determined on column 4'

Rows("10").WrapText = True 'Wraps text on row 10'

Range(Columns(4), Columns(LastCol)).ColumnWidth = 10 'Sets Column width'

Range(Cells(10, 1), Cells(LastRow, LastCol)).HorizontalAlignment = xlCenter 'Centers Cell contents'

Range(Cells(12, 5), Cells(LastRow, LastCol)).NumberFormat = "0" 'Sets Significant figures'

With Range(Cells(11, 1), Cells(11, LastCol)).Borders(xlEdgeBottom) 'bottem border row 11'
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With

With Range(Cells(10, 1), Cells(10, LastCol)).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With

With Range(Cells(LastRow, 1), Cells(LastRow, LastCol)).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With

Next WS

Worksheets("%CV").Activate

For i = 12 To LastRow 'Loop through data range'
For j = 7 To LastCol
If Cells(i, j).Value > 20 Then Cells(i, j).Font.Color = vbRed 'Change font color to red if > 20'
Next j
Next i

Range(Cells(12, 5), Cells(LastRow, LastCol)).Copy

Sheets("FI - Bkgd").Select

Range("E12").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False 'Copys font color to FI - Bkgd'

For k = 12 To LastRow
If Cells(k, 5).Value > 1000 Then Cells(k, 5).Font.Color = vbBlue 'Highlights blank/mulV values >1000'
Next k

For o = 5 To LastCol
If Cells(12, o).Value > 100 Then Cells(12, o).Font.Color = vbBlue 'Highlights blank well values >100'
Next o

ActiveSheet.Range("A1").Select

Application.CutCopyMode = False

Application.ScreenUpdating = True

End Sub

Aucun commentaire:

Enregistrer un commentaire