vba if语句在excel-vba中访问另一个工作簿中的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13095470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:16:17  来源:igfitidea点击:

If statement accessing data in another workbook in excel-vba

excelvbaexcel-vba

提问by John

I am currently trying to program a macro.

我目前正在尝试编写一个宏。

  • I am writing the macro in a new black workbook (this is part of the requirement) and I have a lot of data in a different workbook.
  • I need to use the macro to read the data in that different workbook and spit out errors encountered for each column and write it in the blank workbook (that i am programming macro in).
  • So for each column it's suppose to have list of row numbers an error was encountered (and nothing else). I'm trying to write an If statement to do this and i keep getting an error message stating "expected then or go to".
  • 我正在一个新的黑色工作簿中编写宏(这是要求的一部分),我在不同的工作簿中有很多数据。
  • 我需要使用宏来读取不同工作簿中的数据并吐出每列遇到的错误并将其写入空白工作簿(我正在编写宏)。
  • 所以对于每一列,它假设有一个行号列表,但遇到了错误(没有别的)。我正在尝试编写一个 If 语句来执行此操作,但我不断收到一条错误消息,指出“预计然后或转到”。

This is what I have so far:

这是我到目前为止:

If [Dataset1.xls]Data1!R2C1=53 AND [Dataset1.xls]Data1!R2C1=453 AND [Dataset1.xls]Data1!R2C1=953 Then

If [Dataset1.xls]Data1!R2C1=53 AND [Dataset1.xls]Data1!R2C1=453 AND [Dataset1.xls]Data1!R2C1=953 Then

The dataset1.xls is the workbook that holds all the data and Data1 is the sheet in that workbook with all the data. I might be accessing the separate workbook totally wrong. Can you guys please help. Thank you very much in advance.

dataset1.xls 是包含所有数据的工作簿,Data1 是该工作簿中包含所有数据的工作表。我可能完全错误地访问了单独的工作簿。你们可以帮忙吗。非常感谢您提前。

This is most of the code:

这是大部分代码:

Dim numrow As Long
Dim count As Long
count = 2

With Workbooks("Dataset1.xls").Sheets("Data1")
numrow = .Range("A1", .Range("A1").End(xlDown)).Rows.count

End With
Do Until count = numrow
If [Dataset1.xls]Data1!R2C1<>53 AND [Dataset1.xls]Data1!R2C1<>453 AND [Dataset1.xls]Data1!R2C1<>953 Then  

End If
count = count + 1
Loop

采纳答案by BrOSs

You should change

你应该改变

If Workbooks("Dataset1.xls").Sheets("Data1").Cells(2, 1).Value <> 53 AND Workbooks("Dataset1.xls").Sheets("Data1").Cells(2, 1).Value <> 453 AND Workbooks("Dataset1.xls").Sheets("Data1").Cells(2, 1).Value <> 953 Then

Instead of:

代替:

If [Dataset1.xls]Data1!R2C1<>53 AND [Dataset1.xls]Data1!R2C1<>453 AND [Dataset1.xls]Data1!R2C1<>953 Then

Regards

问候

-- full code:

-- 完整代码:

range = Workbooks("Dataset1.xls").Worksheets("Data").Range("A65536").End(xlUp).Row

For i = 2 To range

If Workbooks("Dataset1.xls").Sheets("Data1").Cells(i, 1).Value <> 53 AND Workbooks("Dataset1.xls").Sheets("Data1").Cells(i, 1).Value <> 453 AND Workbooks("Dataset1.xls").Sheets("Data1").Cells(i, 1).Value <> 953 Then

End IF

Next i