VBA - Display cell value if conditions met

mmuu

New member
Local time
Today, 09:15
Joined
Mar 21, 2012
Messages
2
Hello!

I'm looking to make the leap from formula proficiency to VBA use and would appreciate some guidance.

I'll try to explain my task in text, but have also attached an image:

"Titles" (user entered text) in column A2:A4
"Timing" (user entered dates) in column B2:B4
Another "Timing" (fixed dates) in C1:G1

I would like to create some code to get corresponding "Titles" to display in the C2:G4 area whenever the two timing fields align.

(I can't use formulas for this because the "Title" values are pretty long, and spill over into neighboring cells, and having the whole plotting field filled with formulas ends up preventing the entire "Titles" from being displayed)

I've dabbled in VBA before, but haven't tried anything this complicated before, so any pointers on how to begin constructing the code would be much appreciated! Thanks in advance!
 

Attachments

  • Layout.PNG
    Layout.PNG
    4 KB · Views: 209
For reference, this is the formula I wanted to use:

=IF(ISNONTEXT($H5),IF(AND(YEAR(J$2)=YEAR($H5),WEEKNUM($H5)=WEEKNUM(J$2)),$D5,""),"")

Screenshot of how it looks when I run it is attached. As you can see, it's "close" but the formulas end up blocking the text from appearing.

The formula above is in J5
 

Attachments

  • Capture.PNG
    Capture.PNG
    40.2 KB · Views: 218
Hey mmuu,

I made something really quick that should get you started. I tried to explain things with comments since I know what it's like to just be starting in VBA. If this doesn't do it, or you can't modify it to work, upload a sample file and that'll give a better idea of what is going on.

Code:
Sub update()

Range("A65536").End(xlUp).Select
Titles = Selection.Row 'The number of Titles (including the header) present (assuming these are in column A)
Row = 2 'Start iteration after the header
Do While Row < Titles + 1 'Loop until the end of the data is reached
    
    RowTitle = Cells(Row, "A") 'This takes in the title for the current row
    Timing = Cells(Row, "B") 'This takes in the week number (assuming the data is in column B)
    
    If IsNumeric(Timing) Then
        Cells(Row, Timing + 2) = RowTitle
    End If

    Row = Row + 1
Loop
MsgBox "Done! :D" 'Completely neccesary
End Sub
 

Users who are viewing this thread

Back
Top Bottom