Home » excel » vba – Excel If statements for UsedRange only?

vba – Excel If statements for UsedRange only?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a macro set up in Excel which takes some pasted data and parses it out into a new sheet. From there, I’d like it to read if a value exists In any cell in column K, and if so, places a value on my first sheet into the same row but in column Q.

A bit confusing, I’m sorry. I’ve tried

If Range("K:K") <> "" Then
   Range("Q:Q") = ("Input!R2C2")
End If

But like it reads, it fills data for the entirety of column Q. I want it only to fill in data where a value exists in column K.

How to&Answers:

“I want it only to fill in data where a value exists in column K.”
For thes you need to loop through the rows.

Option Explicit

Public Sub FillDataInQWhereValueInK()

    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "K").End(xlUp).Row 'find last used row

    Dim iRow As Long
    For iRow = 1 To LastRow 'loop through all rows
        If Cells(iRow, "K") <> vbNullString Then 'check if K has a value
            Cells(iRow, "Q") = "Input!R2C2" 'write in Q if K has a value
            'here you can access any other cell in the row where K has a value
            'Cells(iRow, "A").Font.Color = vbRed
            'or access the complete row by
            'Rows(iRow)
        End If
    Next iRow
End Sub

Answer:

Public Sub TestMe()
    Debug.Print WorksheetFunction.CountA(Range("K:K"))
End Sub

This would print 0, if it is not used and anything above 0, if it is used. So you can use it for something like this:

Public Sub TestMe()
    If WorksheetFunction.CountA(Range("K:K")) Then 'if any value is present
        Range("Q:Q") = 1
    Else
        Range("Q:Q") = 0
    End If
End Sub

However, as far as you have mentioned “I want it only to fill in data where a value exists in column K.”, you can loop only through the cells with values in them and write “Something” 6 columns to the right:

Public Sub TestMe()

    Dim myCell As Range

    For Each myCell In Range("K:K").SpecialCells(xlCellTypeConstants)
        myCell.Offset(0, 6) = "Something"
    Next myCell

End Sub

The above solution would work only if you have non-formula values in the Range("K:K"). If you have formulas, it will not work for them, and if you do not have anything it would return an error. Thus, probably it is a better idea to loop from the last cell to the first one and check whether the cell is empty or not like in this one – https://stackoverflow.com/a/50395958/5448626

Answer:

I would use the counta function which simply counts all cells that are populated in a range. If it is equal to zero, no cells are populated. It will be greater than zero if one or more cells are populated.

If Application.WorksheetFunction.CountA(Range("K:K")) > 0 Then
   Range("Q:Q") = ("Input!R2C2")
End If

Answer:

This will find your last used row, and then run a loop down K and add a value to the same row on Col Q. You should be able to modify this to accomplish your goal. Note you will have to make changes to reference different sheets (change sheet names that I have). Also, if you have headers, this will need to be changed to start at

 i = 2 to Lrow 

Option Explicit

Sub Add()


Dim LRow As Long
Dim WB As Workbook
Set WB = ThisWorkbook
Dim i As Integer

LRow = WB.Sheets("Sheet1").UsedRange.Rows.Count

For i = 1 To LRow
If WB.Sheets("Sheet1").Range("K" & i) <> "" Then
    WB.Sheets("Sheet1").Range("Q" & i) = ("Add Input Here")
        End If
Next i

End Sub