在Excel中循环和如果声明

在工作表中,我试图在Excelmacros中创build以下逻辑。

逻辑:

Dub Test() Dim rws as integer Dim counter as integer rws = 6 counter = 2 Do while rws <> 0 IF 'Data Sheet with Space'!J`counter` = "Sample Data Row 1 Col 10" Then WorkSheet!A`counter` = 'Data Sheet with Space'!A`counter` end IF counter = counter + 1 rws = rws - 1 

Excel工作表是:

Data Sheet with Space

 --------------------------------------------- Header 1 Header 2 Header 3 Header 4 Header 5 Header 6 Header 7 Header 8 Header 9 Header 10 Sample Data Row 1 Col 1 Sample Data Row 1 Col 2 Sample Data Row 1 Col 3 Sample Data Row 1 Col 4 Sample Data Row 1 Col 5 Sample Data Row 1 Col 6 Sample Data Row 1 Col 7 Sample Data Row 1 Col 8 Sample Data Row 1 Col 9 Sample Data Row 1 Col 10 Sample Data Row 2 Col 1 Sample Data Row 2 Col 2 Sample Data Row 2 Col 3 Sample Data Row 2 Col 4 Sample Data Row 2 Col 5 Sample Data Row 2 Col 6 Sample Data Row 2 Col 7 Sample Data Row 2 Col 8 Sample Data Row 2 Col 9 Sample Data Row 2 Col 10 Sample Data Row 3 Col 1 Sample Data Row 3 Col 2 Sample Data Row 3 Col 3 Sample Data Row 3 Col 4 Sample Data Row 3 Col 5 Sample Data Row 3 Col 6 Sample Data Row 3 Col 7 Sample Data Row 3 Col 8 Sample Data Row 3 Col 9 Sample Data Row 3 Col 10 Sample Data Row 4 Col 1 Sample Data Row 4 Col 2 Sample Data Row 4 Col 3 Sample Data Row 4 Col 4 Sample Data Row 4 Col 5 Sample Data Row 4 Col 6 Sample Data Row 4 Col 7 Sample Data Row 4 Col 8 Sample Data Row 4 Col 9 Sample Data Row 4 Col 10 Sample Data Row 5 Col 1 Sample Data Row 5 Col 2 Sample Data Row 5 Col 3 Sample Data Row 5 Col 4 Sample Data Row 5 Col 5 Sample Data Row 5 Col 6 Sample Data Row 5 Col 7 Sample Data Row 5 Col 8 Sample Data Row 5 Col 9 Sample Data Row 5 Col 10 --------------------------------------------- 

和另一个名为WorkSheet Excel工作表

当试图运行macros时出现错误, Data Sheet with Space名称会导致错误。

其次,当试图使用countervariablesdynamic地改变input和输出单元号时,我有一个错误。

任何帮助都很感激。

问题在于你处理细胞的方式。

如果要引用第一张纸上的单元格“B6”,基本上有三种方法可以引用纸张:

  1. 通过CodeName(如果您碰巧知道工作表的代号)

     Sheet1.[B6] 
  2. 按名称(这是您在表格的标签上看到的名称)

     Sheets("Data Sheet with Space").[B6] 
  3. 按编号(如果您碰巧知道纸张的顺序)

     Sheets(1).[B6] 

这是你的代码,你要维护它,所以使用哪个最适合你。 你可能更喜欢使用一个variables: wsData = Sheets("Data Sheet with Space") ,所以你可以简单地写wsOther.[A1]=wsData.[B6]


还有几种方法可以引用表单中的单元格。 一些有用的:

  1. 直接使用地址:

     Sheets("Data Sheet with Space").[B6] 
  2. 通过使用.Range (使用地址)

     Sheets("Data Sheet with Space").Range("B6") 
  3. 通过使用.Cell (使用坐标)

     Sheets("Data Sheet with Space").Cells(6, 2) 
  4. 使用.Cell(如YowE3K所提供的)

     Sheets("Data Sheet with Space").Cells(6, "B") 

在你的代码中,你已经在使用坐标了。 所以你可能更喜欢使用.Cells(rws, counter) 。 但这取决于你。

错误来自你的代码中。 (在这里很容易看到灰色)。 如果我正确理解你的代码,那么你应该通过input真正的表单和范围来更正它(如下)(未testing):

 Sub Test2() Dim rws As Integer Dim counter As Integer rws = 6 counter = 2 Do While rws <> 0 If Sheets("Data Sheet with Space").Range("J" & counter).Value = Cells(1, 10).Value Then Sheets("WorkSheet").Range("A" & counter).Value = Sheets("Data Sheet with Space").Range("A" & counter).Value End If counter = counter + 1 rws = rws - 1 Loop End Sub