2016-05-11 36 views
1

我正在创建一个宏来帮助将数据转储(表单1)组织到发票(表单2)中。我编写了大部分的宏,但我坚持以下。Excel宏帮助 - 如果语句的变量范围为

我希望宏读取表1中的Y列,这是一个可变范围(可以是2行到50),并检查它是否显示“CB”。如果这是真的,那么E11上纸2是Yes,否则不,依此类推,直到其到达柱Y对片的端1.

我有以下:

Sheets("Data_Dump").Select 
intCounter = 1 
While Range("Y" & (intCounter + 1)) <> "" 
    intCounter = intCounter + 1 
Wend 
intCardSize = intCounter 
MsgBox (intCardSize) 

Sheets("Data_Dump").Select 

If Range("Y" & intCardSize) = "CB" Then 
    Sheets("Reconciliation").Select 
    Range("E11:E" & intCardSize).Select 
    Range("E11") = "Yes" 
End If 

的范围而似乎工作,它显示列Y中的文本的单元格的数量,但我似乎无法包装我的头周围如何让它从Y1移动到Y2等等,然后将响应粘贴到E11然后E12和等等。

+0

移动“蜿蜒”语句从那里后“结束如果” – OpiesDad

+0

另外,看到这一点:http://stackoverflow.com/questions/10714251/how-to-avoid-using-select- in-excel-vba-macros – OpiesDad

+0

还将“Range(”Y“&intCardSize)”更改为“Range(”Y“&intCounter)” – OpiesDad

回答

0

,您有是你的代码不循环,试图进行比较的问题。 While循环只会查看下一个单元格中是否存在某些内容。事实上,它实际上跳过了第一行,但也许这是故意的。

Dim dataSheet As WorkSheet 
Dim recSheet As Worksheet 
Dim lngCounter As Long 'Use long because an integer may not be big enough for large dataset. 
Dim intCardSize As Long 

Set dataSheet = ThisWorkbook.Sheets("Data_Dump") 
Set recSheet = ThisWorkbook.Sheets("Reconciliation") 
'You want to set the sheets to a variable instead of referring to the whole path each time 
'Also, Note the usage of "ThisWorkbook" which guarantees the worksheet 
'is coming from the one with code in it. 
lngCounter = 2 'If you want to start looking at row 2, start at row 2 with 
       'the variable instead of starting the variable and checking var+1 
While dataSheet.Range("Y" & (lngCounter)) <> "" 
    'While there is a value in the column 

    'intCardSize = intCounter 'Not sure what this is supposed to do 
    'MsgBox (intCardSize) 'This looks like debugging. Commenting out. 


    If dataSheet.Range("Y" & lngCounter) = "CB" Then 
      'Check each row as you go through the loop. 
      'Sheets("Reconciliation").Select 
      'Avoid selecting sheet/range. Unneccessary work for computer. 

      recSheet.Range("E" & (9 + lngCounter)) = "Yes" 
      'Set reconciliation sheet value to "Yes" if data sheet has "CB" 
      'The reconciliation sheet starts on row 11, whereas the datasheet 
      'starts at row 2 ,a difference of 9 
    Else 
      recSheet.Range("E" & (9 + lngCounter)) = "No" 
      'Otherwise set to no. 
    End If 
    lngCounter = lngCounter + 1 
Wend 
intCardSize = lngCounter - 1 'It's been increased to one past the last item. 
MsgBox intCardSize 'Display the last row checked. 
+0

嘿,非常感谢! Wend在开始时没有做任何事情,因为你的评论最重要。我做了一些改变,我似乎无法正常工作......我希望它开始粘贴到列E11(在recSheet上)并从那里下去。因此,如果Y1中有15行,在dataSheet上,recSheet将从E11粘贴到E26,是或否。我将“E”改为“E1”,它开始在E11粘贴,但在跳到第110行之前只进行了9次迭代。你知道为什么会这样吗?柜台上有东西吗? – Kamal

+0

我误解了问题....所提供的代码将从recSheet的第1行开始粘贴。通过将“E”改为“E1”,您最初在单元格“E11”中粘贴,但是,一旦lngCounter等于“10”,您开始粘贴单元格“E110”,因为“E1”与“10” ”。一种方法是“E”和(9 + lngCounter)。我会更新答案来反映这一点。代码中的评论中描述了9。 – OpiesDad

+0

太棒了,再次感谢您的帮助! – Kamal

0

我希望我理解你的代码的目标如下

With Sheets("Data_Dump") 
    With Sheets("Reconciliation").Range("E11").Resize(.Cells(.Rows.Count,1).Row) 
     .Formula="=IF('Data_Dump'!Y1="CB", "Yes","")" 
     .Value= .Value 
    End With 
End With 
+0

OP对如何对VBA进行编码存在误解。这个答案并不能解释他们遇到的问题,而且,如果不解释VBA,会更加难以理解。 OP将代码粘贴到工作簿中学到的东西并不多。为了改善这一点,请评论他们遇到的问题以及如何解决问题,然后可能包含您的附加解决方案以及解释。 – OpiesDad

+0

@OpiesDad你知道,我学到了更多的东西,然后研究了不同的解决方案。尽管如此,我从来没有downvoted谁有不同的想法 – user3598756

+0

我提供不同的解决方案没有问题。这个解决方案没有解释任何东西可以公平地说,我太过分地建议您的解决方案需要在所要求的方法中包含解决方案,为此,我表示歉意。然而,至少应该解释代码,尽管该代码说明为什么这种方法比他们尝试的方法更可取。 – OpiesDad