I have macro that performs a reformat of a collection of sheets that are contained in a single workbook.
Option Explicit
Sub ConsolidateReformat2()
Dim sh As Worksheet
Dim LastRow As Long
Dim FirstSht As String
For Each sh In ActiveWorkbook.Worksheets
FirstSht = Sheets(1).Name
'Piecing together a macro
sh.Sort.SortFields.Clear
sh.Sort.SortFields.Add Key:=Range("R:R"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With sh.Sort
.SetRange sh.Range("A:V")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
sh.Range("C:C,E:E,F:F").Insert Shift:=xlToRight
LastRow = sh.Cells.Find("*", Cells(Rows.Count, Columns.Count), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
sh.Range("C2:C" & LastRow).FormulaR1C1 = "=RC23 & ""-"" & RC1 & ""-"" & RC4"
sh.Range("F2:F" & LastRow).FormulaR1C1 = "=IF('" & FirstSht & "'!RC5>=1,""Yes"",""No"")"
sh.Range("H2:H" & LastRow).FormulaR1C1 = "=RC2 &"" ""&RC7"
sh.Range("C1").FormulaR1C1 = "PRODUCT_CODE"
sh.Range("F1").FormulaR1C1 = "AVAILABLE"
sh.Range("H1").FormulaR1C1 = "PRODUCT_NAME"
sh.Range("L1").FormulaR1C1 = "PRODUCT_COST"
sh.Range("P1").FormulaR1C1 = "WEIGHT"
sh.Range("J1").FormulaR1C1 = "PRODUCT_DESC"
Next sh
End Sub
however the problem is with column F and this code
FirstSht = Sheets(1).Name
sh.Range("F2:F" & LastRow).FormulaR1C1 = "=IF('" & FirstSht & "'!RC5>=1,""Yes"",""No"")"
instead of applying to each individual sheet the FirstSht is referring to the very first sheet in the workbook instead of carrying out the operation in each worksheet of the formula. How would I change this so that the first sheet index is not being used in all the subsequent sheets but rather the columns that are specified in the formula?
Bookmarks