Alter VBA for Cell Addressing in Excel 2013

kyleno

Board Regular
Joined
Jun 9, 2014
Messages
61
I found a VBA script to convert a column of text to a single cell separated by spaces (see below), BUT it will only select a list of data from column A and only return the data to column B.

I was able to figure out that altering the numbers in rows 7 and 8 (dataRow =# and listRow =#) will change the row numbers.

I would like to be able to specify a cell number for both the row/column that the list is located as well as the output cell for the new single cell list.

Any ideas?

Please and thank you.

~ Ky
----------------------------------
Sub generatecsv()

Dim dataRow As Integer
Dim listRow As Integer
Dim data As String

dataRow = 1: Rem the row that it is being read from column A otherwise known as 1 in vb script
listRow = 1: Rem the row in column B that is getting written

Do Until Cells(dataRow, 1).Value = "" And Cells(dataRow + 1, 1).Value = ""
If (data = "") Then
data = Cells(dataRow, 1).Value
Else
If Cells(dataRow, 1).Value <> "" Then
data = data & " " & " " & Cells(dataRow, 1).Value
Else
Cells(listRow, 2).Value = data
data = ""
listRow = listRow + 1
End If
End If
dataRow = dataRow + 1
Loop

Cells(listRow, 2).Value = data

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Instead of giving us code you found and trying to indicate how you might want to modify it, why don't you just tell us exactly what you need and let us write the code to do exactly that? Just give us enough information for us to know what you know about what you want to do. Some ideas on that... what is your set up, how are you deciding where to start, how are you deciding where to stop, what exactly do you want done with the indicated data, etc., etc.?
 
Upvote 0
I have data in H7:H11 that I will be using to create a query in Access

like "*Test*"
or like "Best*"
or like "Rest*"
or like "*West*"
or like "Crest*".


I would like to be able to adjust this range as need


After running the script I would like the data to appear in cell B3

like "*Test*" or like "Best*" or like "Rest*" or like "*West*" or like "Crest*"


I would like to be able to adjust this specified cell as need

Thanks!
 
Upvote 0
I have data in H7:H11 that I will be using to create a query in Access

like "*Test*"
or like "Best*"
or like "Rest*"
or like "*West*"
or like "Crest*".


I would like to be able to adjust this range as need


After running the script I would like the data to appear in cell B3

like "*Test*" or like "Best*" or like "Rest*" or like "*West*" or like "Crest*"

I would like to be able to adjust this specified cell as need
The best way to handle this is using a UDF (user defined function). Here is the code (place it in a general module... same kind of module that macros go in)...
Code:
Function Combine(Rng As Range) As String
  Combine = Join(Application.Transpose(Rng))
End Function
To use it, just specify the range whose cells you want to combine (H7:H11 from your example) and place the formula in the cell you want that combined text to be in (B3 from your example). So, using your example, put this formula in cell B3...

=Combine(H7:H11)
 
Upvote 0
Is there a way to get this to do comma separated values too?
Do you mean like this...
Code:
Function Combine(Rng As Range) As String
  Combine = Join(Application.Transpose(Rng)[B][COLOR="#FF0000"], ","[/COLOR][/B])
End Function
 
Last edited:
Upvote 0
Hey Rick,

I was wondering if there would be a way to alter this to exclude empty/blank cells? Thanks!
 
Upvote 0
I was wondering if there would be a way to alter this to exclude empty/blank cells?
Sure, but that takes a little more code to do...
Code:
Function Combine(Rng As Range) As String
  Dim vNum As Variant
  Combine = Join(Application.Transpose(Rng), ",")
  For Each vNum In Array(9841, 121, 13, 5, 3, 3, 2)
    Combine = Replace(Combine, String(vNum, ","), ",")
  Next
  If Right(Combine, 1) = "," Then Combine = Left(Combine, Len(Combine) - 1)
End Function
Note: I could do this as a one-liner, but the above is more efficient.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top