Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Automatically add an index

Posted on 2013-06-22
4
Medium Priority
?
283 Views
Last Modified: 2013-06-23
I'm trying to figure out a way to automatically add an ampersand along with a unique number when I transfer my row of data from one sheet to another.  On the Matrix worksheet the column SHOP has three initials. My cut and paste vba code transfers anything with something in column A & B. When the vba moves my data to Print worksheet I need it to look at the number IDs in column A and add the next corresponding ID following an ampersand. My example is listed below in the attached link.  I would also like to add a conditional format that colors the numbers and ampersand white so it isn't visible
Add-Unique-Identifier.xlsm
0
Comment
Question by:Southern_Gentleman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
als315 earned 1600 total points
ID: 39269005
0
 
LVL 14

Assisted Solution

by:Faustulus
Faustulus earned 400 total points
ID: 39269018
I took the liberty of revising your code in some parts, in particular where the cell referencing wasn't clear. Bear in mind that a naked Cells(1, "A") refers to the Activesheet, even if it is embedded in something like ShData.Range(Cells(1, "A")). If it so happens that the ActiveSheet isn't ShData the result may be perplexing.
I also added "Option Explicit" to your sheet and declared a variable you had left out. And, finally, I moved Application.CutCopyMode = False to after you had used it rather than before. You don't need to Cut because you are deleting the entire row in the next step.
To the such revamped code I added a few rows to do what you asked. Please repalce your existing code with the following:-
Option Explicit

Sub TransferIt()

    Dim shData As Worksheet
    Dim shDetail As Worksheet
    Dim Response As VbMsgBoxResult
    Dim IdxRow As Long
    Dim TgtRow As Long
    Dim Idx As Long
    
    Response = MsgBox("You are about to move items to the Print Sheet?", vbYesNo)
    If Response = vbNo Then Exit Sub
    
    Set shData = Worksheets("Matrix")
    Set shDetail = Worksheets("Print")

'    On Error Resume Next
    With shDetail
        TgtRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Idx = GetIndex(TgtRow, shDetail)
    End With
    
    With shData
        For IdxRow = .Cells(.Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If WorksheetFunction.IsText(.Cells(IdxRow, "A").Value) And _
                                 IsDate(.Cells(IdxRow, "B").Value) Then
                TgtRow = TgtRow + 1
                Idx = Idx + 1
                Range(.Cells(IdxRow, "A"), .Cells(IdxRow, "R")).Copy _
                       Destination:=shDetail.Cells(TgtRow, "A")
                With shDetail.Cells(TgtRow, "A")
                    .Value = .Value & "&" & CStr(Idx)
                    .NumberFormat = ";;;"
                End With
                .Cells(IdxRow, 1).EntireRow.Delete Shift:=xlUp
                Application.CutCopyMode = False
            End If
        Next
    End With
End Sub

Private Function GetIndex(ByVal R As Long, _
                          Ws As Worksheet) As Long
    Dim S() As String
    
    If R > 1 Then
        S = Split(Ws.Cells(R, "A").Value, "&")
        If IsNumeric(S(UBound(S))) Then
            GetIndex = S(UBound(S))
        End If
    End If
End Function

Open in new window

0
 

Author Closing Comment

by:Southern_Gentleman
ID: 39269774
Thanks Faustulus and als315. For Faustulus, I didn't want the items in the 'Print' sheet to be deleted so I kept the CutCopyMode=True and did an accumulation of what was already on the sheet.
0
 
LVL 14

Expert Comment

by:Faustulus
ID: 39270064
Southern_Gentleman,
Thank you for giving me some of the points.
After you cut or copy an area in a worksheet manually you will still see that area marked, indicating that it is still on the clipboard and, should you choose to paste again, another copy will be made whereever you might be pointing the cursor. That is the CutCopyMode indicator which, in VBA, you remove setting Application.CutCopyMode = False. It is created by a preceding Cut or Copy command and is quite unrelated to deletion.
Some programmers will set this mode to False before they cut  or copy. That action is aimed at any manual cut or copy action that might have preceded running of the code. In fact, the mode is set or reset automatically with the Cut or Copy command. Once the cutting or copying has been accomplished a clean slate should be left behind, and that includes setting to CutCopyMode to False.
My code doesn't delete any items in the Print sheet.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

610 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