简体   繁体   中英

Excel macro help - If statement with a variable range

I am creating a macro to help organize a data dump (sheet 1) into an invoice (sheet 2). I have coded most of the macro, but am stuck on the following.

I want the macro to read column Y on sheet 1, which is a variable range (can be 2 rows to 50) and check if it says "CB". If this is true, then E11 on sheet 2 is Yes, otherwise No, and so on until it reaches the end of column Y on sheet 1.

I have the following:

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

The while range seems to work and it displays the number of cells with text in column Y, but I can't seem to wrap my head around how to get it to move from Y1 to Y2 and so on and then paste the response into E11 then E12 and so on.

I hope I understood your code goal as follows

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 

The problem that you are having is that your code doesn't loop to try to compare. The While loop that you have only looks to see if there is something in the next cell. In fact, it actually skips the first row, but maybe that was intentional.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM