+ Reply to Thread
Results 1 to 6 of 6

How to run a macro, if the value in a cell matches with a set of values in other sheet?

Hybrid View

  1. #1
    Registered User
    Join Date
    01-12-2014
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    77

    How to run a macro, if the value in a cell matches with a set of values in other sheet?

    hello,

    i have a cell A6 which contains a date and on another sheet there are list of non working days. If the date in the cell A6 matches with the non working days list, i want to perfom some action and if it is a working day i want to perform fifferent action. I have tried the following but it is not functioning.

    Sub textauto()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Sheet1")
    Dim ws2 As Worksheet:   Set ws2 = Sheets("Sheet2")
    Dim ws3 As Worksheet:   Set ws3 = Sheets("Sheet3")
    
    
    If ws1.Range("A6").Value = ws3.Range("D12:D28").Value Then
             If ws2.Range("D1").Value = "" Then
             ws2.Range("D1").Value = "Please assign the documents after three days"
             Else
             ws2.Range("D1").Value = ws2.Range("D1").Value & vbLf & vbLf & "Please assign the documents after three days"
            End If
    Else
            If ws2.Range("D1").Value = "" Then
            ws2.Range("D1").Value = "Please assign the documents immediately"
            Else
           ws2.Range("D1").Value = ws2.Range("D1").Value & vbLf & vbLf & "Please assign the documents immediately"
            Else
           End If
    End If
    End Sub
    Last edited by laxminarayana; 02-08-2014 at 02:43 AM.

  2. #2
    Registered User
    Join Date
    02-18-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Help with a macro

    You are more than likely needing to look in each cell in the range as at the moment it is a collection of ranges and if statements.

    Depending on the amount of data you want to look at, if the data set is small you could use "For Each" in a range ie.

    Sub Date_Checker()
    
    Dim rValue as Range
    Dim rCell as Range
    Dim rDynamic as Range
    Dim ws(1 to 3) as Worksheet
    Dim wb as Workbook
    
    Set wb = ThisWorkbook
    
    With wb
          Set ws(1) = .Sheets("Sheet1")
          Set ws(2) = .Sheets("Sheet2")
          Set ws(3) = .Sheets("Sheet3")
    end with
    strValue = 
          Set rDynamic = ws(3).Range("D12:D28") 
          Set rValue = ws(1).Range("A6")
    For each rCell in rDynamic
            If rValue = rCell Then
                  ws(2).Range("D1").value = "write what you want here"
            End If
    
    next rCell
    
    End Sub

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Help with a macro

    Hi, laxminarayana,

    please take your time for a read of the forum rules:

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)



    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    01-12-2014
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    77

    Re: Help with a macro

    I'm sorry about that.

    I've changed the Title and also used the code tags.

    Thank you.

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: How to run a macro, if the value in a cell matches with a set of values in other sheet

    Hi, laxminarayana,

    thanks. Please check if WorksheetFunction,Countif will work for you:
    Sub textauto()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Sheet1")
    Dim ws2 As Worksheet:   Set ws2 = Sheets("Sheet2")
    Dim ws3 As Worksheet:   Set ws3 = Sheets("Sheet3")
    
    With ws2.Range("D1")
      If WorksheetFunction.CountIf(ws3.Range("D12:D28"), ws1.Range("A6").Value) > 0 Then
        If .Value = "" Then
          .Value = "Please assign the documents after three days"
        Else
          .Value = .Value & vbLf & vbLf & "Please assign the documents after three days"
        End If
      Else
        If .Value = "" Then
          .Value = "Please assign the documents immediately"
        Else
          .Value = .Value & vbLf & vbLf & "Please assign the documents immediately"
        End If
      End If
    End With
    
    Set ws3 = Nothing
    Set ws2 = Nothing
    Set ws1 = Nothing
    
    End Sub
    Ciao,
    Holger

  6. #6
    Registered User
    Join Date
    01-12-2014
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    77

    Re: How to run a macro, if the value in a cell matches with a set of values in other sheet

    Thank you very much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to show Which macro didnt work in a nested macro
    By akhileshgs in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-10-2013, 03:21 AM
  2. Perform macro "on open" specific file- store macro in Personal Macro Workbook?
    By thompssc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-17-2012, 12:38 PM
  3. lookup macro, solver macro, realtime macro
    By xelhelp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2011, 06:14 PM
  4. Cannot find macro error when running a macro from a macro in a diffrent workbook.
    By Acrobatic82 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-05-2010, 09:22 AM
  5. [SOLVED] Macro calling another Macro: "The macro 'Personal.xls!FindChar"
    By William Benson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-07-2005, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1