R1C1 Formula

Sherifa

New Member
Joined
Oct 23, 2017
Messages
45
RUN-TIME ERROR '91'
Object variable or With block variable not set

Code:
[COLOR=#333333]Sub SummaryRow()Dim Wkb As Excel.Workbook[/COLOR]Dim ws As Worksheets
Dim ws_count As Integer
Dim i As Integer
Dim LastRow As Long




    Sheet4.Range("a183:M183").Copy
    
        Set Wkb = ThisWorkbook
        ws_count = Wkb.Worksheets.Count
                
                     
        'Begin loop
        For i = 4 To ws_count
              
       
       Set TargetRow = Wkb.Worksheets(i).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        
        LastRow = Wkb.Worksheets(i).Cells(Rows.Count, 1).End(xlUp).Row


         'Paste Formulas
        TargetRow.PasteSpecial xlPasteFormulas
        [B]ws(i).Cells(LastRow + 1, 7).FormulaR1C1 = "=SUM(R4C7:R" & LastRow & "C7)"
        ws(i).Cells(LastRow + 1, 8).FormulaR1C1 = "=COUNT(R4C8:R" & LastRow & "C8)"
        ws(i).Cells(LastRow + 1, 9).FormulaR1C1 = "=SUM(R4C9:R" & LastRow & "C9)"[/B]
        TargetRow.PasteSpecial xlPasteFormats
        
        Next i
         [COLOR=#333333]End Sub[/COLOR]
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I presume you meant to use:

Code:
Wkb.Worksheets(i)

rather than

Code:
ws(i)
 
Upvote 0
I defined
Code:
Dim ws As Worksheets
so it shouldn't make a difference.
I applied the change you suggested, but it gave me the same run time error.
 
Last edited:
Upvote 0
Stop the code after you have dimensioned your variables. What does ws say in the locals window? Anyway paste the code you now have.
 
Upvote 0
: Wkb : : Workbook/ThisWorkbook
: ws : Nothing : Worksheets
: ws_count : 44 : Integer
: i : 4 : Integer
: LastRow : 0 : Long
: TargetRow : : Variant/Object/Range
 
Upvote 0
So ws is nothing. So ws(i) cant be used. Anyway paste the code you now have once you applied what i said in the first post. I think there must be a mistake.
 
Upvote 0
Code:
Sub SummaryRow()Dim Wkb As Excel.Workbook
Dim ws As Worksheet
Dim ws_count As Integer
Dim i As Integer
Dim LastRow As Long




    Sheet4.Range("a183:M183").Copy
    
        Set Wkb = ThisWorkbook
        ws_count = Wkb.Worksheets.Count
                
                     
        'Begin loop
        For i = 4 To ws_count
              
       
       Set TargetRow = Wkb.Worksheets(i).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        
        LastRow = Wkb.Worksheets(i).Cells(Rows.Count, 1).End(xlUp).Row


         'Paste Formulas
        TargetRow.PasteSpecial xlPasteFormulas
        Wkb.Worksheets(i).Cells(LastRow + 1, 7).FormulaR1C1 = "=SUM(R4C7:R" & LastRow & "C7)"
        Wkb.Worksheets(i).Cells(LastRow + 1, 8).FormulaR1C1 = "=COUNTA(R4C8:R" & LastRow & "C8)"
        Wkb.Worksheets(i).Cells(LastRow + 1, 9).FormulaR1C1 = "=SUM(R4C9:R" & LastRow & "C9)"
        TargetRow.PasteSpecial xlPasteFormats
        
        Next i
        
End Sub

Thank you, why doesn't the Dim ws As Worksheet work?
The code It appears to be working now
 
Upvote 0
Why doesn't the Dim ws As Worksheet work?

I guess you mean Dim ws As Worksheets from post #1 .

The Worksheets property of the Workbook object returns a Sheets object.

If you want to use it, this is an example:

Code:
Sub Test()
Dim Wkb As Workbook
Dim ws As Sheets

        
        Set Wkb = ThisWorkbook
        Set ws = ThisWorkbook.Worksheets
        
        MsgBox ws(1).Name

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top