VBA If语句中的For Each用于仿真 [英] VBA If Statement within For Each for Emulation
问题描述
我需要在BlueZone中更新数千个X坐标。我正在使用VBA在BlueZone VT中输入关键命令。不幸的是,没有设置光标位置命令,因此我仅限于使用制表符将光标放在需要移动的位置。
I need to update several thousand X coordinates in BlueZone. I'm using VBA to enter key commands in BlueZone VT. Unfortunately, there is no "set cursor position" command, so I am limited to using "tabs" to put the cursor where it needs to go.
在坐标更新时屏幕上有两种可能:6个或7个选项卡,取决于屏幕上是否存在0。
At the coordinate updating screen there are two possibilities: 6 or 7 tabs depending on the presence of a 0 or not on the screen.
例如:位置241054有一个1,需要7个标签,位置241051有一个0,需要6个标签,以获取X位置字段,以便我将其转储到我的位置
For example: location 241054 has a 1 and needs 7 tabs, location 241051 has a 0 and needs 6 tabs to get to the X location field for me to dump in my variable.
这是我的代码:
Sub FiXCoord_Loop()
'Must start at IMLOA screen
Dim bzhao As Object
Set bzhao = CreateObject("BZWhll.WhllObj")
bzhao.Connect ""
Dim myX As Integer
Dim res_check As Integer
Dim myLoc As Variant
'Dim res_check As Variant
myRange = ActiveSheet.Range("A2:A1000")
'myResRange = ActiveSheet.Range("D3")
myX = ActiveSheet.Range("E1").Value
res_check = ActiveSheet.Range("D3").Value
For Each myLoc In myRange
'end loop at blank cell
If myLoc = "" Then
Exit For
End If
'Query location
bzhao.SendKey "Q"
bzhao.Wait 0.2
bzhao.SendKey myLoc
bzhao.Wait 0.2
bzhao.SendKey "<enter>"
bzhao.Wait 0.2
'Copy screen to get res#
bzhao.Wait 1
bzhao.Copy 32
bzhao.Wait 1
'Paste info to sheet for res_check
Range("J1").Select
ActiveSheet.Paste
bzhao.SendKey "M"
bzhao.Wait 0.2
bzhao.SendKey "<tab>"
bzhao.Wait 0.2
bzhao.SendKey "<tab>"
bzhao.Wait 0.2
bzhao.SendKey "<tab>"
bzhao.Wait 0.2
bzhao.SendKey "<tab>"
bzhao.Wait 0.2
bzhao.SendKey "<tab>"
'if value > 0 extra tab
If res_check > 0 Then
bzhao.SendKey "<tab>"
bzhao.Wait 1
bzhao.SendKey "<tab>"
bzhao.Wait 1
bzhao.SendKey myX
bzhao.Wait 0.2
bzhao.SendKey "<enter>"
bzhao.Wait 0.2
bzhao.SendKey "E"
bzhao.Wait 0.5
Else
bzhao.Wait 0.2
bzhao.SendKey "<tab>"
bzhao.Wait 1
bzhao.SendKey myX
bzhao.Wait 0.2
bzhao.SendKey "<enter>"
bzhao.Wait 0.2
bzhao.SendKey "E"
bzhao.Wait 0.5
End If
Next myLoc
End Sub
因为我仅限于步行通过键盘命令输入屏幕,我正在复制屏幕并将其粘贴到工作表上以检查是否为0。但是,我无法获得if-then语句。
As I'm limited to walking the screen by key commands, I am copying the screen and pasting it onto my sheet to check for the 0 or not. I cannot get the if - then statement to work, though.
代码仅使用6个选项卡运行,这告诉我res_check值(这是将值拉出屏幕的mid()语句)识别,或者变量不会随循环更新。但是,如果我先运行res_check> 0位置,它仍然会显示6个标签,因此我可以消除这种可能性。
The code just keeps running with 6 tabs which tells me that either the res_check value (which is a mid() statement to pull the value off the screen) is not being recognized, or the the variable doesn't update with the loop. If I run the res_check > 0 location first, though, it still does 6 tabs so I can eliminate that possibility.
作为进一步检查,我对TRUE进行了FALSE检查res_check值并将其调暗为整数时通过。但是,在工作表上,ISNUMBER()失败。
As a further check I ran a TRUE FALSE check on the res_check value and it passed when dimmed as an integer. On the sheet, though, ISNUMBER() fails.
TLDR:如果那么将不会产生期望的结果-代码将默认保持为其他条件。
TLDR: If Then will not produce desired result - code keeps defaulting to the Else condition.
推荐答案
以下代码有效。
单元格D3 = mid(J7,63,1)和单元格D8 = D3 * 1
Cell D3 =mid(J7, 63, 1) and cell D8 =D3*1
Sub FiXCoord_Loop()
'Must start at IMLOA screen
Dim bzhao As Object
Set bzhao = CreateObject("BZWhll.WhllObj")
bzhao.Connect ""
Dim myX As Integer
Dim res_check As Integer
Dim myLoc As Variant
myRange = ActiveSheet.Range("A2:A1000")
myX = ActiveSheet.Range("E1").Value
res_check = ActiveSheet.Range("D8").Value
For Each myLoc In myRange
'end loop at blank cell
If myLoc = "" Then
Exit For
End If
'Query location
bzhao.SendKey "Q"
bzhao.Wait 0.2
bzhao.SendKey myLoc
bzhao.Wait 0.2
bzhao.SendKey "<enter>"
bzhao.Wait 0.2
'Copy screen to get res#
bzhao.Wait 1
bzhao.Copy 32
bzhao.Wait 1
'Paste info to sheet for res_check
Range("J1").Select
ActiveSheet.Paste
'Modify data
bzhao.SendKey "M"
bzhao.Wait 0.2
bzhao.SendKey "<tab><tab><tab><tab><tab>" 'tab to Res
'Decide 6 or 7 tabs
If ActiveSheet.Range("D8").Value > 0 Then
bzhao.Wait 0.1
bzhao.SendKey "<tab>"
Else
End If
bzhao.Wait 0.1
bzhao.SendKey "<tab>"
bzhao.Wait 0.1
bzhao.SendKey myX
bzhao.Wait 0.1
bzhao.SendKey "<enter>"
bzhao.Wait 0.1
bzhao.SendKey "E"
bzhao.Wait 0.5
Next myLoc
End Sub
这篇关于VBA If语句中的For Each用于仿真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!