0

I have a Excel workbook with 35 tabs (1 tab for each day of a 31 day month and 4 extras).

I wanted to create two macros:

  1. The first to "complete current line",(3 columns A B & C) clearing the content of column B, inserting "Completed" in column C, Highlighting the row (the three columns are in a table) yellow, then finally sorting the table by color with "No Fill".
  2. A button to insert a new row above the current position.

I developed the macro on the 20th tab, only because that's where I happened to be.

Tested it and thought that I had it setup to move to each of the tabs and function the same on all of them. I thought I had it nailed

Then when I transferred it to the machine it needed to run on I discovered that for some reason the highlight function of the 'complete' macro was bleeding over into the insert macro, so every new line inserted had a yellow highlight.

Went back to my system and started testing and the 'Complete' macro is also failing.

The Completed macro is failing at the points where I'm doing the following lines using ActiveWorkbook.

Since the insert doesn't fail just randomly highlights yellow I am not sure where that is blowing up.

Sub Completed()
Dim SCellRow As String
SCellRow = ActiveCell.Row
Dim SCellCol As String
SCellCol = "A"
Dim CellColB As String
CellColB = "B" & SCellRow
Dim ECellCol As String
ECellCol = "C"
Dim SCell As String
SCell = SCellCol & SCellRow
Dim ECell As String
ECell = ECellCol & SCellRow
Dim TargetRange As String
TargetRange = SCell & ":" & ECell

Range(TargetRange).Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
Range(CellColB).Select
Selection.ClearContents
Range(ECell).Select
ActiveCell.FormulaR1C1 = "COMPLETED"
Selection.End(xlToLeft).Select
Range(TargetRange).Select
ActiveWorkbook.Worksheets(ActiveSheet.Name).ListObjects("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets(ActiveSheet.Name).ListObjects("Table1").Sort.SortFields.Add _
    Key:=Range("Table1[[#All],[PRD NUM]]"), SortOn:=xlSortOnCellColor, Order _
    :=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(ActiveSheet.Name).ListObjects("Table1").Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
End Sub


Sub Insertrow()

' Insert Row Macro

Dim SCellRow As String
SCellRow = ActiveCell.Row
Dim SCellCol As String
SCellCol = "A"
Dim ECellCol As String
ECellCol = "C"
Dim SCell As String
SCell = SCellCol & SCellRow
Dim ECell As String
ECell = ECellCol & SCellRow
Dim TargetRange As String
TargetRange = SCell & ":" & ECell

Range(TargetRange).Insert
End Sub

I know my variable assignment blocks are over blown but I haven't had time weed out what isn't needed to get the jobs done as of yet. My apologies.

Post Script: I resolved that the sorting by color was simple enough for the user to do on their own and hacked off the end of the macro to eliminate that part of the procedure. Not a real fix but it took it off my plate.

Thanks for the insight on 'Select' issue... good reading.

Ken Carter
  • 355
  • 1
  • 16
  • 3
    Highly suggest you give [this question](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) a read. – BigBen Nov 21 '19 at 21:17
  • This was insightful information. Thanks. I ended up hacking off the sort routine just to save grief, eliminating the issue. – Ken Carter Nov 22 '19 at 15:35

0 Answers0