首页 \ 问答 \ 我的if语句似乎不起作用。(My if statements don't seem to work. Might be an alternate way?)

我的if语句似乎不起作用。(My if statements don't seem to work. Might be an alternate way?)

所以,其基本要点是我每小时运行一次报告,并根据一天中的某个时间将结果填入特定单元格中。 现在我正在使用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

So, the basic gist of this is that I run a report once an hour and I need it to fill the results into specific cells depending on what time of day it is. Right now I'm using if statements to tell it which fields to fill, but I'm not sure I'm doing it right. I definitely run it every hour so the code doesn't have to be based on what time of day it is as long as it doesn't overwrite or delete what is already there and will move to the next row each time it's run. I've just included the top of my code as well as one instance of the if statements. After the first one I copied and pasted throughout the rest changing the variables as needed. Please let me know if I'm doing something wrong or if there is a better way to do this!

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

原文:https://stackoverflow.com/questions/49732831
更新时间:2022-05-20 09:05

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。