You are here:

Excel/VBA - Inputting a value from a formula

Advertisement


Question
QUESTION: Current coding is:

For y = 3 To 1000
If Sheets("Gas Pending").Cells(y, 1) = "" Then Sheets("Gas Pending").Cells(y, 18) = "" _
Else Sheets("Gas Pending").Cells(y, 18).FormulaR1C1 = "=TODAY()-RC[-10]"
Next y
       Range("R3:R1000").Copy
       Range("R3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=False

I would like to not have to paste special and instead just input the value of the R1C1 formula.

ANSWER: Ryan,


For y = 3 To 1000
If Sheets("Gas Pending").Cells(y, 1) = "" Then

 Sheets("Gas Pending").Cells(y, 18) = ""

Else

v = Date - Sheets("Gas Pending").Cells(y,8).Value
Sheets("Gas Pending").Cells(y, 18).Value = v

End if

Next y

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Thanks so much for your previous help. One more quick question.

Here is my current code:

'For b = 3 To 1000
'If Sheets("Gas Pending").Cells(b, 1) = "" Then Sheets("Gas Pending").Cells(b, 26) = "" _
'Else Sheets("Gas Pending").Cells(b, 26).FormulaR1C1 = "=TODAY()-RC[-17]"
'Next b
'       Range("Z3:Z1000").Copy
 '      Range("Z3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  '     :=False, Transpose:=False

'For X = 3 To 1000
'If Sheets("Gas Pending").Cells(X, 9) = "" Then Sheets("Gas Pending").Cells(X, 22) = "" _
'Else Sheets("Gas Pending").Cells(X, 22).FormulaR1C1 = "=If(RC[+4]<90,""0-3"",IF(RC[+4]<180,""3-6"","">6""))"
'Next X
'       Range("V3:v1000").Copy
 '      Range("V3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  '     :=False, Transpose:=False
   
'Application.CutCopyMode = False

I would like to simplify this so it doesnt have to paste special and it just inputs the values into the cells. preferably take it down to one  "If" statement.

ANSWER: Ryan,

Since you are checking two separate conditions, you need two if statements.  You can reduce it down to on pass through the rows (one loop).


For b = 3 To 1000
If Sheets("Gas Pending").Cells(b, 1) = "" Then
  Sheets("Gas Pending").Cells(b, 26) = ""
Else
  Sheets("Gas Pending").Cells(b, 26).FormulaR1C1 = _
     "=TODAY()-RC[-17]"
end if
If Sheets("Gas Pending").Cells(b, 9) = "" Then
  Sheets("Gas Pending").Cells(b, 22) = ""
Else
  Sheets("Gas Pending").Cells(b, 22).FormulaR1C1 = _
  "=If(RC[+4]<90,""0-3"",IF(RC[+4]<180,""3-6"","">6""))"
Next b

With Range("Z3:Z1000")
 .Formula = .Value
End with
With Range("V3:V1000")
 .Formula = .Value
End with
 
Application.CutCopyMode = False

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: I think this should be the last one.

I need to run the below macro across several worksheets. Here is what I have so far:

Sub GasCalc()

Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = "Gas Pend" Or ws.Name = "FP-Pend" Or ws.Name = "ARP" Or ws.Name = "Power-Pend" Or ws.Name = "Other-Pend" Then

For a = 3 To 1000
If Cells(a, 1) = "" Then
Cells(a, 19) = ""

Else

v = Date - Cells(a, 8).Value
Cells(a, 19).Value = v

End If

Next a

For b = 3 To 1000
If Cells(b, 9) = "" Then
Cells(b, 23) = ""
   
Else

v = 90 - (Date - Cells(b, 9).Value)
Cells(b, 23).Value = v

End If
Next b

End If
Next ws

End Sub

The problem is that it is not running across the five named worksheets, mearly running 5 times on the active worksheet. Any solutions?  

Answer
Ryan,

the unqualified Cells(a, 1)  refers to the activesheet (and thus your problem).  
You could either use

ws.Cells(a, 1)

and do this for all cell references

or the easy fix is to activate the sheet you are working on

Sub GasCalc()

Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = "Gas Pend" Or ws.Name = "FP-Pend" Or ws.Name = "ARP" Or ws.Name = "Power-Pend" Or ws.Name = "Other-Pend" Then

ws.Activate   '<=== activate the sheet


For a = 3 To 1000
If Cells(a, 1) = "" Then
Cells(a, 19) = ""

Else

v = Date - Cells(a, 8).Value
Cells(a, 19).Value = v

End If

Next a

For b = 3 To 1000
If Cells(b, 9) = "" Then
Cells(b, 23) = ""
  
Else

v = 90 - (Date - Cells(b, 9).Value)
Cells(b, 23).Value = v

End If
Next b

End If
Next ws

End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2012 About.com, a part of The New York Times Company. All rights reserved.