+ Reply to Thread
Results 1 to 5 of 5

Generating values in number of cells based on adjacent cell values

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-24-2012
    Location
    Hollidaysburg, Pa
    MS-Off Ver
    Excel 2010
    Posts
    398

    Generating values in number of cells based on adjacent cell values

    What I have
    1. In Column B, I have the dates
    2. In Column I, I have engineers name

    What I need
    • I want a macro to generate Serial Nos. (1,2,3……. n) in column A If an only if the date in column B is today’s date and the engineer’s name matches with the PC’s username


    The following is my code
    Sub RequestID()
    
    Dim x As Long
    For x = 3 To Range("a" & Rows.Count).End(xlUp).row
         If CDate(Range("b" & x).Text) = Date And UCase(Range("i" & x).Text) = UCase(UserName) Then   ' checking for date and username
         Dim myrange As Range
    Dim rng As Range
    Dim dblMax As Double
     Set rng = Range("a1", Range("a65536").End(xlUp))   ' setting range
    dblMax = Application.WorksheetFunction.Max(rng) 
    Range("a65536").End(xlUp).Offset(1, 0).Value = dblMax + 1
    End If
    Next
    End Sub
    and

    Public Function UserName()
        UserName = Environ$("UserName")
    End Function
    Problem:
    • Above code runs without errors but does nothing
    Last edited by subbby; 05-24-2014 at 04:34 PM.

  2. #2
    Forum Contributor
    Join Date
    08-24-2012
    Location
    Hollidaysburg, Pa
    MS-Off Ver
    Excel 2010
    Posts
    398

    Re: Generating values in number of cells based on adjacent cell values

    Any help on this ?

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Generating values in number of cells based on adjacent cell values

    Hi subby,

    Try the following code (tested in Excel 2003):
    Option Explicit
    
    Sub RequestID()
    
      Const sSerialNumberCOLUMN = "A"
      Const sDateCOLUMN = "B"
      Const sEngineerCOLUMN = "I"
    
      Dim rng As Range
      
      Dim mySpreadSheetDate As Date
      
      Dim iCount As Long
      Dim iHighestSerialNumber As Long
      Dim iLastRow As Long
      Dim iRow As Long
      
      Dim sEngineer As String
      Dim sExistingSerialNumber As String
      Dim sUserName As String
      Dim sValue As String
      
      'Get the User Name
      sUserName = GetUserNameFromEnvironmentVariable()
      
      'Get the last row used
      iLastRow = Range("a" & Rows.Count).End(xlUp).Row
      
      'Get the last row used (more reliable)
      iLastRow = Range("A1").SpecialCells(xlCellTypeLastCell).Row
      
      'Get the highest existing Serial Number (in Column 'A')
      Set rng = ActiveSheet.UsedRange.Columns(sSerialNumberCOLUMN).SpecialCells(xlConstants)
      iHighestSerialNumber = Application.WorksheetFunction.Max(rng)
      
      For iRow = 3 To iLastRow
           
        'Get the existing Serial Number from the Spreadsheet
        'Get the Date from the Spreadsheet
        'Get the Engineer Name from the spreadsheet
        'NOTE: Trim() function removes leading and trailing spaces
        sExistingSerialNumber = Trim(Cells(iRow, sSerialNumberCOLUMN).Text)
        sEngineer = Trim(Cells(iRow, sEngineerCOLUMN).Text)
      
        sValue = Cells(iRow, sDateCOLUMN).Text
        If IsDate(sValue) Then
          mySpreadSheetDate = CDate(sValue)
        Else
          mySpreadSheetDate = 0  'Set date to sentinel day (a long long time ago)
        End If
      
        'Put in a new serial number if
        'a. There is NO existing 'Serial Number' and
        'b. The date in the spreadsheet is today's date and
        'c. The User Name is the User Name on this computer
        If Len(sExistingSerialNumber) = 0 Then
          If mySpreadSheetDate = Date And UCase(sEngineer) = UCase(sUserName) Then
            iCount = iCount + 1
            iHighestSerialNumber = iHighestSerialNumber + 1
            Cells(iRow, sSerialNumberCOLUMN) = iHighestSerialNumber
          End If
        End If
        
      Next iRow
      
      MsgBox iCount & " New Serial Number(s) were added." & vbCrLf & _
             "The largest Serial Number is now " & iHighestSerialNumber & "."
      
    End Sub
    
    Public Function GetUserNameFromEnvironmentVariable()
        GetUserNameFromEnvironmentVariable = Environ$("UserName")
    End Function
    Lewis

  4. #4
    Forum Contributor
    Join Date
    08-24-2012
    Location
    Hollidaysburg, Pa
    MS-Off Ver
    Excel 2010
    Posts
    398

    Re: Generating values in number of cells based on adjacent cell values

    Lewis, I haven't tried this code yet but i would like to thank you a lot for your dedication. I thought this would be as simple as just 4 lines...but you must have taken a lot of time to write this up and test it

    Appreciate your help

  5. #5
    Forum Contributor
    Join Date
    08-24-2012
    Location
    Hollidaysburg, Pa
    MS-Off Ver
    Excel 2010
    Posts
    398

    Re: Generating values in number of cells based on adjacent cell values

    Worked well... appreciate it....

+ 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] How to delete adjacent cells values based on one cell?
    By meprad in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-31-2013, 09:07 AM
  2. Concatenate multiple cell values based on matching adjacent cells
    By mkrzy in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-10-2013, 02:40 AM
  3. Replies: 2
    Last Post: 03-03-2012, 12:58 PM
  4. [SOLVED] Conditional coloring of Excel cells, based on adjacent cell values?
    By Greg Stuart in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-10-2006, 06:20 PM
  5. Format cell based on adjacent cells values
    By the majestic ferny in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-16-2005, 02:35 PM

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