我的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

相关文章

更多

Don’t work. Be hated. Love someone.

http://halfhalf.posterous.com/dont-work-be-hated-lo ...

gui求jtapi高手T_T

小弟今年刚毕业,上了一个星期班,老大要我用jtapi写一个监控程序,弄了两天了,头很大啊。 有一部座 ...

RabbitMQ Work模式消息队列

一个生产者、多个消费者。 一个消息只能被一个消费者获取。 生产者发布消息 private ...

Working on Free Software

This was written in December, 1999 Lots of programm ...

Object Oriented Programming

Some might also contend that inheritance should be ...

webservice可不可以实现动态的返回类型:<T> List<T> getList(T a)

webservice可不可以实现动态的返回类型 例如: &lt;T&gt; List&lt;T&g ...

javax.imageio.IIOException: Can't create output stream!的解决方案

ImageIO.write(image, "jpeg", response.getOutputStre ...

不会sql语句....谁帮我优化下

select * from dbo.tbGoodsInfo where (id in (select ...

Java泛型父类取得子类的泛型参数T的Class类型

import java.lang.reflect.ParameterizedType;import j ...

最新问答

更多

您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)

将diff文件复制到存储库的根目录,然后执行以下操作: git apply yourcoworkers.diff 有关apply命令的更多信息, apply 见其手册页 。 顺便说一下:一个更好的方法是通过文件交换整个提交文件是发送者上的命令git format-patch ,然后在接收器上加上git am ,因为它也传送作者信息和提交信息。 如果修补程序应用程序失败,并且生成diff的提交实际上在您的备份中,则可以使用尝试在更改中合并的apply程序的-3选项。 它还适用于Unix管道,如下

将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)

尝试将第二行更改为snprintf(buf1, sizeof buf1, "%.2f", balance1); 。 另外,为什么要声明用该特定表达式分配缓冲区的存储量? EDIT @LưuVĩnhPhúc在下面的评论中提到我的原始答案中的格式说明符将舍入而不是截断,因此根据如何在不使用C舍入的情况下截断小数,您可以执行以下操作: float balance = 200.56866; int tmp = balance1 * 100; float balance1 = tmp / 100.0; c

OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)

这是简单的解决方案 在你需要写的控制器中 BackendMenu::setContext('Archetypics.Team', 'website', 'team'); 请参阅https://octobercms.com/docs/backend/controllers-views-ajax#navigation-context BackendMenu::setContext('Author.Plugin name', 'Menu code', 'Sub menu code'); 你需要在r

页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)

每当发出请求时ASP都会创建一个新的Page对象,并且一旦它将响应发送回用户就不会保留对该Page对象的引用,因此只要你找不到某种方法来保持生命自己引用该Page对象后,一旦发送响应, Page和只能通过该页面访问的所有对象才有资格进行垃圾回收。 ASP creates a new Page object whenever a request is made, and it does not hold onto the reference to that Page object once it

codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)

要在生产服务器中调试这个,你可以临时放 error_reporting(E_ALL); 并查看有哪些其他错误阻止正确的重定向。 您还应该检查生产服务器发送的响应标头。 它是否具有“缓存”,是否需要重新验证标头等 to debug this in production server, you can temporary put error_reporting(E_ALL); and see what other errors are there that prevents the proper

在计算机拍照在哪里进入

打开娥的电脑.在下面找到视频设备点击进去就可以了...

使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)

你是对的。 第一次输入后,换行符将保留在输入缓冲区中。 第一次读取后尝试插入: cin.ignore(); // to ignore the newline character 或者更好的是: //discards all input in the standard input stream up to and including the first newline. cin.ignore(numeric_limits::max(), '\n'); 您必须为#inc

No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)

for (int k = 0; k > 10; k++) { System.out.println(k); } k不大于10,所以循环将永远不会执行。 我想要什么是k<10 ,不是吗? for (int k = 0; k < 10; k++) { System.out.println(k); } for (int k = 0; k > 10; k++) { System.out.println(k); } k is not greater than 10, so loop

单页应用程序:页面重新加载(Single Page Application: page reload)

优点是不注销会避免惹恼用户,以至于他们会想要杀死你:-)。 说真的,如果每次刷新页面时应用程序都会将我注销(或者在新选项卡中打开一个链接),我再也不会使用该应用程序了。 好吧,不要这样做。 确保身份验证令牌存储在刷新后的某个位置,即不在某些JS变量中,而是存储在cookie或本地存储中。 The advantage is that not logging off will avoid pissing off your users so much that they'll want to kill

在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)

EXECUTE IMMEDIATE 'SELECT '||field_val_temp ||' FROM tableb WHERE function_id = :func_val AND rec_key = :rec_key' INTO field_val USING 'STDCUSAC' , yu.rec_key; 和, EXECUTE IMMEDIATE 'UPDATE tablec SET field_val_'||i||' = :field_val' USI