How to run code that runs through two specific worksheets

17 views Asked by At

I have looked and looked and looked and come up empty, apologies in advance if i just couldn't find it! I have a section of code that I want to run on two specific sheets (same workbook). I don't want to reference the sheets by index or by name because both of those present too much risk for accidental change. I want to use the codename so that nobody can mess with it.

I realize the code below is how i set it up to use the index number, but like i said that is setting myself up for failure because if anyone moves the tabs around the code will break.

`

Dim s1() As Variant 'Raw Data Tabs
Dim s2() As Variant 'Converted Data Tabs
s1 = Array(7, 5)
s2 = Array(3, 4)


For i = 0 To 1
    y = 0
    Do While y <> fCount 'Cycle through each date/row of data at a time
        'First identify the first date/row to process from raw data tab
        Set findrange = Sheets(s1(i)).Columns(DDC).Find(What:=CDate(FileDate(y)))
        r1 = findrange.Row
        Set findrange = Sheets(s2(i)).Columns(DDC).Find(What:=CDate(FileDate(y)))
        r3 = findrange.Row
    
        'Delete all existing data to avoid errors
        Sheets(s2(i)).Range(Sheets(s2(i)).Cells(r3, 3), Sheets(s2(i)).Cells(r3, 30)).Clear
    
        s = 0
        Do Until s = iCountRaw + 1 'Loop through each item number and quantity from raw data tab
        
            'Identify first date, item number, and value, then assign value to variable
            c1 = Sheets(s1(i)).Cells.Find(What:=ItemNumRaw(s)).Column
            x = Sheets(s1(i)).Cells(r1, c1) 'quantity of pos item number sold
        
            'identify row on Constants tab that cooresponds to the current item number in question
            r2 = Sheet2.Columns(2).Find(What:=ItemNumRaw(s)).Row
            x1 = x * Sheet2.Cells(r2, 5) 'converted quantity
            x2 = Sheet2.Cells(r2, 4) 'item number to apply to
        
            'apply data to converted tab
            Set findrange = Sheets(s2(i)).Rows(IR).Find(What:=x2)
            c3 = findrange.Column
            Sheets(s2(i)).Cells(r3, c3) = Sheets(s2(i)).Cells(r3, c3) + x1 'Post data on converted sales data tab
        
            If Sheet2.Cells(r2, 6) <> "" Then 'This will apply a second item if needed
                x1 = x * Sheet2.Cells(r2, 7) 'converted quantity
                x2 = Sheet2.Cells(r2, 6) 'item number to apply to
                c3 = Sheets(s2(i)).Cells.Find(What:=x2).Column
                Sheets(s2(i)).Cells(r3, c3) = Sheets(s2(i)).Cells(r3, c3) + x1 'Post data on converted sales data tab
            End If
            s = s + 1
        Loop
        c = 3
        Do While Sheets(s2(i)).Cells(4, c) <> ""
            If Sheets(s2(i)).Cells(r3, c) = "" Then
                Sheets(s2(i)).Cells(r3, c) = "0"
            End If
            c = c + 1
        Loop
        y = y + 1 'this will upcycle the date to be looked at
    Loop
Next i

`

0

There are 0 answers