Solved

Excel / Unique ID

Posted on 2010-08-14
8
334 Views
Last Modified: 2012-05-10
Hi Experts,

I have tried to obtain code off the net and make changes to it, but nothing is working. I have a userform
that works great for entering data into sheet 1. I was using a basic formula in column L to concatenate fields together to make a unique ID. When I copy this formula down it then makes the userform write to the wrong row in sheet 1 when data is entered.
I'm wondering is it possible to automatically concatenate column (E, C, and K) together using VBA according to when the rows of data are entered. Is there some kind of change event in VBA that can achieve this in column L? Is it possible to have it automatically do this without any user interference or button etc.

Many Thanks in advance, I have attached a sample sheet in excel.
SampleUniqueID.xlsx
0
Comment
Question by:JKCC
  • 3
  • 3
  • 2
8 Comments
 
LVL 45

Expert Comment

by:patrickab
ID: 33438649
JKCC,

The file does not contain a UserForm nor does it contain any VBA code. Please upload your file with both the UserForm and the code you are currently using - albeit with errors.

Patrick
0
 

Author Comment

by:JKCC
ID: 33438693
I can't because it is confidential information. That is why I created a simple dummy attached with a very basic sample. If it is not possible than I will have to look for other ways of achieving this. I'm just really after column L concatenating the 3 fields together as the rows are populated. E.g. as row one is populated than, column L should populate with the concatenation and so on through VBA. Is it that this has to be in the userform or can it be a separate module specificually for sheet1? I currently do the basica formula in sheet1 and not on the userform.

I'm new to all of this and have had lots of help getting this done so I'm still learning ALOT!!!!

Thanks heaps!

0
 

Author Comment

by:JKCC
ID: 33438702
Can it be based on according to column A. So when column A is populated, then column L would generate the concatenation via VBA in the corresponding row etc? I dunno, just a thought. I'm not specifically after a unique ID being generated just the count down and the concatenatin via vba. Hope this helps a little. Anythig to get me started would be great.

Cheers,
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 81

Expert Comment

by:byundt
ID: 33438711
You can use a worksheet event sub that will populate column L without a formula. This sub must be placed in the code pane for the worksheet, and won't work at all anywhere else.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range, rw As Range, targ As Range
Dim n As Long
n = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1 'Number of last row with data
Set targ = Union([C2].Resize(n - 1, 1), [E2].Resize(n - 1, 1), [K2].Resize(n - 1, 1))
Set targ = Intersect(targ, Target)
If targ Is Nothing Then Exit Sub

Application.EnableEvents = False
For Each rw In targ.EntireRow.Rows
    With rw
        If Application.CountA(.Cells(3), .Cells(5), .Cells(11)) = 3 Then
            .Cells(12) = Left(.Cells(5), 2) & .Cells(3) & .Cells(11)
        Else
            .Cells(12) = ""
        End If
    End With
Next
Application.EnableEvents = True
End Sub

Open in new window

SampleUniqueIDQ26403446.xlsm
0
 
LVL 45

Accepted Solution

by:
patrickab earned 250 total points
ID: 33438735
JKCC,

The code below is in the attached file. When a change is made in column 1 of Sheet1, the formula you have specified is placed automatically on the same row in column L.

Patrick
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim celle As Range

With Sheets("Sheet1")
    Set rng = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))
    If Not Intersect(rng, Target) Is Nothing Then
        .Cells(Target.Row, 12).FormulaR1C1 = "=CONCATENATE(LEFT(RC[-7],3),"""",RC[-9],RC[-1])"
    End If
End With

End Sub

Open in new window

SampleUniqueID-01.xls
0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 250 total points
ID: 33438751
Although it didn't make any difference with your sample data, I had the wrong number of characters from column E. It should have been 3 in statement 13.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range, rw As Range, targ As Range
Dim n As Long
n = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1 'Number of last row with data
Set targ = Union([C2].Resize(n - 1, 1), [E2].Resize(n - 1, 1), [K2].Resize(n - 1, 1))
Set targ = Intersect(targ, Target)
If targ Is Nothing Then Exit Sub

Application.EnableEvents = False
For Each rw In targ.EntireRow.Rows
    With rw
        If Application.CountA(.Cells(3), .Cells(5), .Cells(11)) = 3 Then
            .Cells(12) = Left(.Cells(5), 3) & .Cells(3) & .Cells(11)
        Else
            .Cells(12) = ""
        End If
    End With
Next
Application.EnableEvents = True
End Sub

Open in new window

SampleUniqueIDQ26403446.xlsm
0
 

Author Comment

by:JKCC
ID: 33439410
Thanks experts...both solutions work great.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 33439754
JKCC - Thanks for the grade and points - Patrick
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question