Home » excel » excel – How to record cell position (not empty) and run macro for each of them

excel – How to record cell position (not empty) and run macro for each of them

Posted by: admin May 14, 2020 Leave a comment

Questions:

enter image description here enter image description here

Hi everyone, I’ve not been able to write up anything as described in title after researching around the internet for abit. But, is there a way to record the cell position (not sure if I’m describing it correctly) of those cells which are not empty/blank, then to run a macro ONLY for each of those cell; regardless whether they have been shifted or not? For example (as per screenshot), if A1, A3 and A5 is not blank, then run marco only for these cells EVEN if some of them are shifted to A4 or A8 later on. So in short, I really need the macro to run only on these cell IF it is not blank/empty.

How to&Answers:

Here is the raw structure of the code you are looking for. Adapt it to your project as you see fit and expand its functionality as you require.

Sub SkipBlanks()

    Dim Rl As Long                      ' last row
    Dim Tmp As Variant
    Dim R As Long                       ' row counter

    With Worksheets("Sheet1")           ' modify to suit
        Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
        ' work on column A
        For R = 2 To Rl                 ' start the loop in row 2
            Tmp = .Cells(R, "A").Value
            If Len(Tmp) Then
                MsgBox "Cell " & Cells(R, "A").Address(0, 0) & _
                       " has the value """ & Tmp & """.", _
                       vbInformation, "Test report"
            End If
        Next R
    End With
End Sub

For a quick test, add two or 3 values in random places of column A and run the code.