Results 1 to 3 of 3

Referencing the correct object in a formula

Threaded View

  1. #1
    Registered User
    Join Date
    06-04-2007
    MS-Off Ver
    Excel 2007
    Posts
    94

    Referencing the correct object in a formula

    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?
    Last edited by capnhud; 06-21-2010 at 06:10 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1