VBA IF语句(替换范围中的日期值) [英] VBA IF statement (Replace Date Value in Range)
问题描述
我是VBA新手。
我想用 范围"B1"替换上一个日期,使用 今天(),当"NH"时;在"A1"范围内被起诉
I want to replace a previous date in a range "B1", with Today(), when "NH" is indicted in a range "A1"
新的日期值将保持在范围内,直到条件"NH"为止。被指示 在 新的日期
NEW Date value will remain in range until condition "NH" is indicated on a new date
               &NBSP ;   A1                    B1                 
A1 B1
新高 Previous High
New High Previous High
NH                   3/1/14 NBSP;            
(" B1")上一个日期被 取代今天()  IF  (QUOT; A1")=" NH" 
NH 3/1/14 ("B1") Previous Date replaced by Today() IF ("A1") ="NH"
推荐答案
使用Excel中的简单IF公式无法做到这一点。您需要VBA代码,以下内容将放入模块中,
You would not be able to do this with simple IF formula in Excel. You need VBA Code, the following would go in a Module,
Sub replaceDate()
'********************
'Code Courtesy of
' Paul Eugin
'********************
Dim totRows As Long, iCtr As Long
'Get the total number of Rows
totRows = Cells(Rows.Count, 2).End(xlUp).Row
'Loop through the list of all Unique values
For iCtr = 1 To totRows
'Here is where Grouping will occur
If Cells(iCtr, 1) = "NH" Then _
Cells(iCtr, 2) = Date
Next
End Sub
检查:
我的第一个VBA ,如果您不熟悉使用VBA进行编码。然后在即时窗口中调用Sub replaceDate
Check :
My First VBA, if you are new to coding using VBA. Then in the immediate window call the Sub replaceDate
这篇关于VBA IF语句(替换范围中的日期值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!