Consulting

Results 1 to 4 of 4

Thread: Put comment with IF

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    40
    Location

    Put comment with IF

    Hello everyone,

    I would like to include in my code that it puts comment only if cells in columns "N" of workbooks ("Stocks") = "Y"

    Thank you very much for your help.



    Sub position()
    
    Dim ws As Worksheet, rng As Range, x As Long, temp
    
    Set ws = Workbooks("Wk49production").Sheets("packing production schedule")
    Set rng = Workbooks("Stocks").Worksheets("wms_browse").Range("D2:Q2000")
    
    For x = 1 To 1000
            With ws.Cells(x, 5)
            temp = Application.VLookup(ws.Cells(x, 35), rng, 14, False)
            
    If .Comment Is Nothing Then
            .AddComment CStr(temp)
            
    Else
            .Comment.Text CStr(temp)
            
            
    End If
    End With
    Next
    End Sub

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i think you mean 'corresponding cell' in col N

    perhaps:
    Sub vbax_54455_AddComment_On_Condition()
         
        Dim ws As Worksheet, rng As Range, x As Long, temp
         
        Set ws = Workbooks("Wk49production").Sheets("packing production schedule")
        Set rng = Workbooks("Stocks").Worksheets("wms_browse").Range("D2:Q2000")
         
        For x = 1 To 1000
            With ws.Cells(x, 5)
                temp = Application.VLookup(ws.Cells(x, 35), rng, 14, False)
                If Application.VLookup(ws.Cells(x, 35), rng, 11, False) = "Y" Then
                    If .Comment Is Nothing Then
                        .AddComment CStr(temp)
                    Else
                        .Comment.Text CStr(temp)
                    End If
                End If
            End With
        Next
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Posts
    40
    Location
    Hi Mancubus,

    Thank you very much for your help. It is close but not working.
    I have several times Cells(x, 35) in my workbooks stocks, which can match either with an "N" or "Y".
    What I get is the value for the first value it meets (even if it's an N).
    Not sure it makes sense

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome.

    perhaps seeing the workbooks may help.

    you can upload a (multiple) workbook(s) via Go Advanced / Manage Attachments.

    you may wish to alter your sensitive / confidential data. just provide some fake data in order to understand the table structure and values.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •