我的if语句似乎不起作用。可能是另一种方式?

时间:2018-04-09 12:18:58

标签: excel vba if-statement

所以,这个的基本要点是我每小时运行一次报告,我需要它根据一天的时间将结果填充到特定的单元格中。现在我使用if语句告诉它要填写哪些字段,但我不确定我是否正确行事。我肯定每小时运行一次,所以代码不必基于它的时间,只要它不会覆盖或删除已存在的内容,并且每次都会移动到下一行它跑了。我刚刚包含了代码的顶部以及if语句的一个实例。在第一个之后,我复制并粘贴其余部分,根据需要更改变量。如果我做错了或者有更好的方法可以告诉我,请告诉我!

Sub Update()
Dim sht As Worksheet
Dim lastRow As Long
lastRow = ActiveSheet.UsedRange.Rows.Count
Set sht = ThisWorkbook.Worksheets("Sheet1")
Dim path As String
path = "C:\Users\Redacted\Desktop\Booking Window Avai -working copy.xlsm"
Dim currentWb As Workbook
Set currentWb = ThisWorkbook

Dim openWb As Workbook
Set openWb = Workbooks.Open(path)

Dim openWs As Worksheet
Set openWs = openWb.Sheets("Mail Format")
Dim rng_data As Range


Set rng_data = openWs.Range("B17")

If ("C2") = "" And Now() > ("09:00") And Now() < ("10:00") Then
rng_data.Copy [currentWb.Sheets("sht").Range("C2").PasteSpecial          xlPasteValues]
ElseIf ("C3") = "" And Now() > ("10:00") And Now() < ("11:00") Then
rng_data.Copy [currentWb.Sheets("sht").Range("C3").PasteSpecial xlPasteValues]
ElseIf ("C4") = "" And Now() > ("11:00") And Now() < ("12:00") Then
rng_data.Copy [currentWb.Sheets("sht").Range("C4").PasteSpecial xlPasteValues]
ElseIf ("C5") = "" And Now() > ("12:00") And Now() < ("13:00") Then
rng_data.Copy [currentWb.Sheets("sht").Range("C5").PasteSpecial xlPasteValues]
ElseIf ("C6") = "" And Now() > ("13:00") And Now() < ("14:00") Then
rng_data.Copy [currentWb.Sheets("sht").Range("C6").PasteSpecial xlPasteValues]
ElseIf ("C7") = "" And Now() > ("14:00") And Now() < ("15:00") Then
rng_data.Copy [currentWb.Sheets("sht").Range("C7").PasteSpecial xlPasteValues]
ElseIf ("C8") = "" And Now() > ("15:00") And Now() < ("16:00") Then
rng_data.Copy [currentWb.Sheets("sht").Range("C8").PasteSpecial xlPasteValues]
ElseIf ("C9") = "" And Now() > ("16:00") And Now() < ("17:00") Then
rng_data.Copy [currentWb.Sheets("sht").Range("C9").PasteSpecial xlPasteValues]

End If

2 个答案:

答案 0 :(得分:4)

public override View GetView(int position, View convertView, ViewGroup parent) { DataViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.From(mContext).Inflate(Resource.Layout.TableItems, null, false); holder = new DataViewHolder(); holder.txtDescription = convertView.FindViewById<TextView>(Resource.Id.txtDescription); holder.txtDescription.Click += delegate { // instead of setting the color directly here, just modify the data (holder.txtDescription.Tag as ItemType).ItemColor = Color.Red notifyDataSetChanged(); }; convertView.Tag = holder; } else { holder = convertView.Tag as DataViewHolder; } holder.txtDescription.Text = mitems[position].Description; holder.txtDescription.Tag = mitems[position]; // this so that the click handler knows which item to modify holder.txtDescription.SetBackgroundColor(mitems[position].ItemColor); return convertView; } public class DataViewHolder : Java.Lang.Object { public TextView txtDescription { get; set; } } 没有按照您的代码所期望的那样做。看看下面的代码:

Now() < ("15:00")

如您所见,您必须将已解析为Sub TestMe() Debug.Print Now() '09.04.2018 14:23:56 Debug.Print Now() < ("10:00") 'True Debug.Print Now() < ("something meaningless") 'True Debug.Print Now() < ("") 'False 'This is one way to do it--v Debug.Print TimeSerial(Hour(Now), Minute(Now), Second(Now)) < TimeSerial(10, 10, 0) End Sub 的值与已解析为Date的值进行比较。否则,它会将日期解析为Date并比较字符串。

String只是其中一个选项。 TimeSerial MSDN

答案 1 :(得分:1)

如果您在Sheet1的列中包含时间值,则可以更轻松地移动值。这假定列B包含时间值 - 09:0010:00

Sub Update()

    Dim openWb As Workbook
    Dim rng_data As Range
    Dim sht_Target As Worksheet
    Dim lPasteRow As Long

    Set sht_Target = ThisWorkbook.Worksheets("Sheet1")

    Set openWb = Workbooks.Open("C:\Users\Redacted\Desktop\Booking Window Avai -working copy.xlsm")
    Set rng_data = openWb.Worksheets("Mail Format").Range("B17")

    'This next row replaces your IF statements.
    lPasteRow = Application.Match(CDbl(Time()), sht_Target.Range("B:B"), 1)
    sht_Target.Cells(lPasteRow, 3) = rng_data.Value

End Sub