Thread: Variable Input
View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
ryguy7272 ryguy7272 is offline
external usenet poster
 
Posts: 2,836
Default Variable Input

I have to use, what I thought was, a simple variable input, but it's turning
out to be not so simple. Basically I loop through all sheets in a book, as
long as the sheets < "Sheet1". Then do some basic formatting and some
simple math. Everything works fine except for this part:
Chng = Range("F2")

and this part:
c.Offset(, 19).Value =
"=(IF(RC[-20]="&Chng&",RC[-18],0)+IF(RC[-15]="&Chng&",RC[-14],0))*RC[-3]"

I have names in column F of each sheet, which are all the same on each
sheet, but different names are on different sheets. All I need to do is
figure out the value in F2 on each sheet and use that in my IF function. I
thought I could just assign the value in F2 to the variable Chng and then
just use that variable in my IF function. Excel has other thoughts and it is
telling me that I can't do that. Why?

All code below:
Sub Math()
Dim lastRow As Long
Dim c As Variant
Dim sh As Worksheet
Dim myA As Range
Dim lastYRow As Long
Dim sumRng As Range
Dim Tot As Double
Dim Chng As Variant


For Each sh In Worksheets
If LCase(sh.Name) < LCase("Sheet1") Then
sh.Activate

With sh
.Rows(1).Font.Bold = True

Chng = Range("F2")

lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
For Each c In .Range("F2:F" & lastRow)
If c.Value < "" Then
c.Offset(, 19).Value =
"=(IF(RC[-20]="&Chng&",RC[-18],0)+IF(RC[-15]="&Chng&",RC[-14],0))*RC[-3]"
End If
Next c

lastYRow = .Cells(.Rows.Count, "Y").End(xlUp).Row
Set sumRng = .Range("Y2:Y" & lastYRow)
Tot = Application.WorksheetFunction.Sum(sumRng)
'MsgBox Tot
.Cells(lastRow + 1, "Y").Value = Tot

'.Rows(2).Delete
End With

End If

Next sh

End Sub

Any help is appreciated.

Regards,
Ryan--

--
RyGuy