【发布时间】:2020-10-16 01:51:33
【问题描述】:
我试图从同一个工作簿中的另一个工作表中获取一个值,但我似乎无法弄清楚。
我有三张床单;它们被命名为 X、Y 和 Z。我目前的代码查看工作表 X 上的几个不同值,以确定要从工作表 Y 或 Z 上提取哪个单元格。
我觉得我的代码写得很好,但是我想要的值只是 0.00 而不是我想要的 1625.00。
Sub test()
Dim value1 As String
Dim value2 As String
Dim value3 As String
Dim value4 As Integer
value1 = ActiveSheet.Cells(8, 4).Value '(the letter Y is written in this cell)
value2 = ActiveSheet.Cells(10, 4).Value '(the word yes is written in this cell)
value3 = ActiveSheet.Cells(12, 4).Value '(the word yes is written in this cell)
if value1 = "" Then
Exit Sub
End If
If value2 = "yes" And value3 = "yes" Then
value4 = Worksheets(value1).Range("H4")
End If
Cells(9, 9) = value4
End Sub
工作表 Y 上的单元格 H4 包含我想要拉过来的数字。 我正在尝试使用 value1 字符串作为工作表的名称,因为我可能需要从工作表 Y 或 Z 中提取
【问题讨论】:
-
Cells(9, 9).Value = Worksheets(value1).Range("H4").Value? -
很可能
value2和value3实际上并非都是“是”。 -
检查单词前后的空格。如果有,要么删除它们,要么用
trim()包装分配的变量 -
使用调试器并逐步执行您的代码(使用 F8)。检查变量的内容,这应该会告诉您代码失败的原因。