Home

Applying a macro to multiple cells.

%3CLINGO-SUB%20id%3D%22lingo-sub-781763%22%20slang%3D%22en-US%22%3EApplying%20a%20macro%20to%20multiple%20cells.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-781763%22%20slang%3D%22en-US%22%3E%3CP%3EI%20need%20help%20with%20a%20macro%20issue.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22x-hidden-focus%22%3ESo%2C%20I%20currently%20have%20a%20large%20Excel%20workbook%20with%20over%201000%2B%20cells%20with%20content%20in%20them.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20issue%3A%20I%20have%20a%20column%2C%20column%20A%20I%20will%20call%20it%2C%20that%20has%20different%20text%20in%20each%20cell.%20For%20instance%2C%20using%20first%20names%2C%20A1%20is%20Mark%2C%20A2%20is%20Sam%2C%20A3%20is%20Grace%2C%20and%20so%20on.%20Now%2C%20in%20each%20cell%20I%20need%20to%20add%2Fpaste%20text%2C%20say%20a%20last%20name.%20This%20text%2C%20which%20is%20in%20my%20clipboard%2C%20will%20be%3CEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ethe%20same%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FEM%3Efor%20every%20cell%20in%20column%20A.%20So%20I%20need%20to%20paste%20%22Appleseed%22%20into%20A1%2C%20A2%2C%20A3%2C%20and%20so%20on%20resulting%20in%20Mark%20Appleseed%2C%20Sam%20Appleseed%2C%20and%20Grace%20Appleseed-%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3E%3CSPAN%3E%3CSTRONG%3Ewithout%3C%2FSTRONG%3E%3C%2FSPAN%3E%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eoverwriting%20the%20first%20names.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EWhat%20I%20need%3A%3C%2FSPAN%3E%3C%2FP%3E%3COL%3E%3CLI%3EA%20macro%20that%20takes%20the%20text%20from%20my%20clipboard%20and%20pastes%20it%20into%20the%20cells.%3C%2FLI%3E%3CLI%3EThe%20above%20macro%20to%20apply%20itself%20to%20all%20necessary%20cells%20in%20one%20or%20only%20a%20few%20clicks-%20column%20A%20has%201517%20%22first%20names%22%20so%20can%20not%20do%20this%20manually.%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20makes%20sense.%26nbsp%3B%20Thank%20you%20in%20advance.%3C%2FP%3E%3CP%3E-%20Heidi%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-781763%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETraining%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-782262%22%20slang%3D%22en-US%22%3ERe%3A%20Applying%20a%20macro%20to%20multiple%20cells.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-782262%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F385078%22%20target%3D%22_blank%22%3E%40HeidiC1%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20not%20wildly%20enthusiastic%20about%20the%20use%20of%20the%20clipboard%3B%20more%20often%20than%20not%20is%20contains%20the%20leftovers%20from%20some%20past%20operation.%26nbsp%3B%20It%20also%20requires%20references%20be%20set%20to%20the%20%22Microsoft%20Forms%202.0%20Object%20Library%22.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20said%2C%20the%20following%20code%20appears%20to%20do%20what%20you%20require%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%3ESub%20extendName()%3CBR%20%2F%3EDim%20DataObj%20As%20New%20MSForms.DataObject%3CBR%20%2F%3EDim%20rng%20As%20Range%3CBR%20%2F%3EDim%20v()%3CBR%20%2F%3EDim%20i%20As%20Long%3CBR%20%2F%3EDim%20S%20As%20String%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20DataObj.GetFromClipboard%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20S%20%3D%20DataObj.GetText%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Set%20rng%20%3D%20Range(%22firstName%22)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20v%20%3D%20rng.Value%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20For%20i%20%3D%201%20To%20UBound(v)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20v(i%2C%201)%20%3D%20v(i%2C%201)%20%26amp%3B%20%22%20%22%20%26amp%3B%20S%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Next%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20rng.Value%20%3D%20v%3CBR%20%2F%3EEnd%20Sub%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
HeidiC1
Occasional Visitor

I need help with a macro issue.

 

So, I currently have a large Excel workbook with over 1000+ cells with content in them.

 

My issue: I have a column, column A I will call it, that has different text in each cell. For instance, using first names, A1 is Mark, A2 is Sam, A3 is Grace, and so on. Now, in each cell I need to add/paste text, say a last name. This text, which is in my clipboard, will be the same for every cell in column A. So I need to paste "Appleseed" into A1, A2, A3, and so on resulting in Mark Appleseed, Sam Appleseed, and Grace Appleseed- without overwriting the first names.

 

What I need:

  1. A macro that takes the text from my clipboard and pastes it into the cells.
  2. The above macro to apply itself to all necessary cells in one or only a few clicks- column A has 1517 "first names" so can not do this manually.

 

Hope this makes sense.  Thank you in advance.

- Heidi 

1 Reply

@HeidiC1 

I am not wildly enthusiastic about the use of the clipboard; more often than not is contains the leftovers from some past operation.  It also requires references be set to the "Microsoft Forms 2.0 Object Library".

 

That said, the following code appears to do what you require

 

Sub extendName()
Dim DataObj As New MSForms.DataObject
Dim rng As Range
Dim v()
Dim i As Long
Dim S As String
    DataObj.GetFromClipboard
    S = DataObj.GetText
    Set rng = Range("firstName")
    v = rng.Value
    For i = 1 To UBound(v)
        v(i, 1) = v(i, 1) & " " & S
    Next
    rng.Value = v
End Sub

 

Related Conversations
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
2 Replies
*Updated 9/3* Syncing in Microsoft Edge Preview Channels
Elliot Kirk in Articles on
202 Replies
Early preview of Microsoft Edge group policies
Sean Lyndersay in Discussions on
65 Replies