Referencing two types of sheets - I get Error HELP!!

Y

Yossy

I am referencing two types of named sheet(s) in a work book. One named with
_Bal and the others named _Trial but I get error. What am I doing wrong. All
help totally appreciated. THanks

Sub T_Bal()
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In Worksheets
If InStr(1, ws.Name, "_Bal") Then
With ws
.Range("g35").Value = .Range("g40").Value
With .Range("g23")
formStr = .Formula
.Formula = formStr & "+" & ws.Range("g36").Value
End With
With .Range("g30")
formStr = .Formula
.Formula = formStr & "+" & ws.Range("g57").Value
End With
.Range("g57").Value = 0
End With
ElseIf InStr(1, ws.Name, "_Trial") Then
With ws
.Range ("g23")
formStr = .Formula
.Formula = formStr & "+" & ws.Range("g36").Value
End With
End If
Next
Application.ScreenUpdating = True
End Sub
 
D

Dave Peterson

Notice that you have "with ws" and "with .range("G23")" in the top portion of
the code.

You don't have that in the second portion:

With ws
.Range ("g23")
formStr = .Formula
.Formula = formStr & "+" & ws.Range("g36").Value
End With

So maybe:

With ws
with .Range("g23")
formStr = .Formula
.Formula = formStr & "+" & ws.Range("g36").Value
end with
End With
 
Y

Yossy

it highlights the . Range within this code and gave error as invalid use of
property.

ElseIf InStr(1, ws.Name, "_Trial") Then
With ws
.Range ("g23") ' it highlights the .Range here
 
R

Rick Rothstein

I think Dave has given you what the problem is. Right after the 'With ws'
you have .Range("g23") on the line all by itself... you aren't doing
anything with it. You probably meant the code to look like this (mirroring
how you handled the code structure in the "Then" section)...

With ws
With .Range("g23")
.....

instead of how you posted it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top