How to pass selected cells into a delimited text string?

P

PcolaITGuy

I have a spreadsheet of server information with a column called HOSTNAME
where the values are unique and I have other columns such as ENVIRONMENT,
CATEGORY, etc that are used to identify the servers function, location, etc.
Typically I only look at a specific group of servers based on their
ENVIRONMENT or CATEGORY by using an AUTOFILTER.

Having said that, after I have filtered my data showing only the rows I want
I will often select a group of cells under the HOSTNAME column to be used in
other documents, etc. Once I make my selection of cells,Is there a way to
pass the values of these cells into a delimited text string or text file?

Example of selected sells in HOSTNAME column:

HOSTNAME
server1
server2
server6
server9
server33

Pass the values into a string or text file as such:
server1, server2, server6, server9, server33

Thanks in advance for your advice,

Scott
 
J

Jim May

After Filtering - Highlight your Hostname Data Only Cells and run:

Sub extractdata()
Dim mystring As String
ct = Selection.Count
With Selection
For j = 1 To ct
If Selection(1) = Selection(j) Then
mystring = Selection(j).Value
Else
mystring = mystring & "," + Selection(j).Value
End If
Next j
End With
Range("G2").Value = mystring ' Or someother cell <<< chg accordingly,,,
End

That should get you there
 
B

bta

Const CF_TEXT As Long = 1



Private Function HostNameList() As String

Dim HostName As Range


For Each HostName In ActiveWindow.RangeSelection
HostNameList = HostNameList & "," & HostName.Value
Next

HostNameList = Mid(HostNameList, 2)
Call PutCFTEXTStringOnClipboard(HostNameList)
End Function



' Requires the reference "Microsoft Forms 2.0 Object Library"
' Just add a UserForm object and delete it again immediately.
Private Sub PutCFTEXTStringOnClipboard(ByRef CF_TEXT_string As String)

Dim ClipboardText As New DataObject


Call ClipboardText.SetText(CF_TEXT_string, CF_TEXT)
Call ClipboardText.PutInClipboard
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top