+ Reply to Thread
Results 1 to 6 of 6

VB script help to find columns and change...

Hybrid View

  1. #1
    RompStar
    Guest

    VB script help to find columns and change...

    Ok, I have 4 columns:

    Department, Date, Employee Name, Daily HR attendance code

    so

    A, B, C, D

    Headers are on row 1, everything below that is data...

    So what I wanted to be able to do, is change the HR code with a script,
    incase managers make a mistake.

    So, first first the Date from B, then in Offset find the right name to
    match the date, and then if those two match change the HR code using
    offset on the same row.

    starts here -------------

    Sub changedhrcode()

    Dim lastrow As Long
    Dim cell As Range
    Dim txtDate As Date
    Dim txtName As String
    Dim txtHR As String

    lastrow = Range("B65536").End(xlUp).Row
    txtDate = InputBox("Enter Date You want the records searched:", "Enter
    Date")
    txtName = InputBox("Enter Employee Name to search for:", "Enter Name")
    txtHR = InputBox("Enter HR Code to replace old:", "Enter HR Code")

    For Each cell In Range("B1:B" & lastrow)
    If cell.Value = txtDate & cell.Offset(0, 1).Value = txtName Then
    cell.Offset(0, 2).Value = txtHR
    Next cell

    End Sub

    So I highlighted a row that I wanted to test this script on, I enter
    the Date, then the Name, and then the new HR code that was supposed to
    replace it, but it doesn't replace it, so I am not sure where the
    script is not right.

    Can any one help ?


  2. #2
    Jim Cone
    Guest

    Re: VB script help to find columns and change...

    RS,

    Change "&" to "and" in the following line and see what happens...
    If cell.Value = txtDate & cell.Offset(0, 1).Value = txtName Then

    Jim Cone
    San Francisco, USA

    "RompStar" <rmiecznik@comcast.net> wrote in message
    news:1123801114.198488.43810@f14g2000cwb.googlegroups.com...
    Ok, I have 4 columns:
    Department, Date, Employee Name, Daily HR attendance code
    so
    A, B, C, D
    Headers are on row 1, everything below that is data...
    So what I wanted to be able to do, is change the HR code with a script,
    incase managers make a mistake.
    So, first first the Date from B, then in Offset find the right name to
    match the date, and then if those two match change the HR code using
    offset on the same row.
    starts here -------------

    Sub changedhrcode()

    Dim lastrow As Long
    Dim cell As Range
    Dim txtDate As Date
    Dim txtName As String
    Dim txtHR As String

    lastrow = Range("B65536").End(xlUp).Row
    txtDate = InputBox("Enter Date You want the records searched:", "Enter
    Date")
    txtName = InputBox("Enter Employee Name to search for:", "Enter Name")
    txtHR = InputBox("Enter HR Code to replace old:", "Enter HR Code")

    For Each cell In Range("B1:B" & lastrow)
    If cell.Value = txtDate & cell.Offset(0, 1).Value = txtName Then
    cell.Offset(0, 2).Value = txtHR
    Next cell

    End Sub

    So I highlighted a row that I wanted to test this script on, I enter
    the Date, then the Name, and then the new HR code that was supposed to
    replace it, but it doesn't replace it, so I am not sure where the
    script is not right.
    Can any one help ?


  3. #3
    RompStar
    Guest

    Re: VB script help to find columns and change...

    yep, that's what the problem was, I aldo Dimed txtDate as Date, it
    works now, thanks.


  4. #4
    RompStar
    Guest

    Re: VB script help to find columns and change...

    One more question:

    The script works great, I am intergrating it with a network script
    which I will post in due time for feedback :- )

    The one last thing that I can think of is this, I want the user that
    runs this script to get some kind of a msg box at the end...

    So if the requests records were found spit out a msg box "Record found,
    transaction completed", and if the records weren't found msg box "
    something else "info not found" or whatever, what would be the best way
    to do that ?


  5. #5
    Jim Cone
    Guest

    Re: VB script help to find columns and change...

    RS,

    I added a Boolean variable and reused two of the existing
    variables to add a final MsgBox to the code.
    Also, I added a check after all the input boxes are shown to
    make sure at least something was entered in the boxes.
    '-----------------------
    Sub changedhrcode()
    Dim lastRow As Long
    Dim cell As Excel.Range
    Dim txtDate As String
    Dim txtName As String
    Dim txtHR As String
    Dim blnFound As Boolean

    lastRow = Range("B65536").End(xlUp).Row
    txtDate = InputBox("Enter Date You want the records searched:", "Enter Date ")
    txtName = InputBox("Enter Employee Name to search for:", "Enter Name")
    txtHR = InputBox("Enter HR Code to replace old:", "Enter HR Code")

    If Len(txtDate) * Len(txtName) * Len(txtHR) = 0 Then
    MsgBox "Not all required data was entered. ", vbExclamation, "RS did this"
    Exit Sub
    End If

    For Each cell In Range("B1:B" & lastRow)
    If cell.Value = txtDate And cell.Offset(0, 1).Value = txtName Then
    cell.Offset(0, 2).Value = txtHR
    blnFound = True
    End If
    Next cell

    If blnFound Then
    lastRow = vbInformation
    txtName = "Records found, transaction completed. "
    Else
    lastRow = vbExclamation
    txtName = "No records found. "
    End If
    MsgBox txtName, lastRow, "RS did this"
    End Sub
    '---------------------------

    Jim Cone
    San Francisco, USA



    "RompStar" <rmiecznik@comcast.net> wrote in message
    news:1124144885.860096.271850@g43g2000cwa.googlegroups.com...
    One more question:
    The script works great, I am intergrating it with a network script
    which I will post in due time for feedback :- )
    The one last thing that I can think of is this, I want the user that
    runs this script to get some kind of a msg box at the end...
    So if the requests records were found spit out a msg box "Record found,
    transaction completed", and if the records weren't found msg box "
    something else "info not found" or whatever, what would be the best way
    to do that ?

  6. #6
    RompStar
    Guest

    Re: VB script help to find columns and change...

    Thanks a lot, this helped me, I got it like this:


    Sub changedhrcode()

    Dim lastrow, cell, cnt
    Dim txtDate As Date
    Dim txtName As String, txtDept As String


    cnt = 0
    lastrow = Range("B65536").End(xlUp).Row
    txtDate = InputBox("Enter Date You want the records searched for:",
    "Enter Date")
    txtName = InputBox("Enter Employee Name to search for:", "Enter Name")
    txtDept = InputBox("Enter HR Code to replace old:", "Enter Dept")
    For Each cell In Range("B1:B" & lastrow)
    If cell.Value = txtDate And cell.Offset(0, 1).Value = txtName Then
    cell.Offset(0, 2).Value = txtDept
    cnt = cnt + 1
    End If
    Next cell
    If cnt > 0 Then
    MsgBox "Number of records updated: " & cnt
    Else
    MsgBox "Info Not Found"
    End If

    End Sub

    Also what I wanted to do is this:

    Basically that Upper Case didn't Matter, as long as the name matches,
    and also the order in which it was
    entered didnt' matter...

    For example:

    Raymond Stone

    or

    Stone Raymond

    and it wouldn matter if it was Raymond or raymond ? I know LCase I
    think is the function to use, just never used it before.


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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