I need to format a specific set of cells (which will be in different locations depending on what I am doing). I need these cells to be center aligned.
So, I perform the task manually once while recording a macro. The macro codes out as:At the time I need this to run, I have two excel files open. I'm copying data from certain locations on one sheet to the other sheet. The copy part works great. Now I need to do the format part, so I took the macro code and inserted it into my VBA as such.Code:Range("C5:E6").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End WithwkbSummary is declared As WorkBook and iRows is declared As Integer.Code:wkbSummary.Sheets("Sheet1").Range("C" & iRows - 1 & ":E" & iRows).Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With
When I run it, I get[quote=Run Time Error]Run Time error 1004
Select method of Range class failed[/code]The wkbSummary.Sheets("Sheet1").Range("C" & iRows - 1 & ":E" & iRows).Select is highlighted.
I even hardcoded the cell locations in to be C5:E6 and still got the error.
I'm sure there is yet another mysterious part of Excel I've yet to come to grips with.



Reply With Quote



Bookmarks