Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Macro 1

Status
Not open for further replies.

chester27

IS-IT--Management
Apr 29, 2002
208
0
0
GB
Looking for some help or advice. Trying to create a Macro that will concatenate 4 columns A-D with a ~ separator in Column F. It needs to start in E2 and continue till empty cell in column D. I have tried the below so far but fails to continue past E3.

Sub ConCat()
'
' ConCat Macro
'

'
Dim oWS As Worksheet, lLastRow As Long, r As Long

Set oWS = ActiveSheet
lLastRow = oWS.Cells.SpecialCells(xlLastCell).Row
For r = 1 To lLastRow
' Combine A to D
If Len(oWS.Cells(r, 4)) > 0 And Len(oWS.Cells(r, 5)) > 0 Then
oWS.Cells(r, 5).Value = oWS.Cells(r, 1).Value & "~" & oWS.Cells(r, 2).Value & "~" & oWS.Cells(r, 3).Value & "~" & oWS.Cells(r, 4).Value
End If
Next
End Sub
 
I think your problem is with this line: lLastRow = oWS.Cells.SpecialCells(xlLastCell).Row
Try changing it to the following : lLastRow = oWS.ActiveCell.SpecialCells(xlLastCell).Row

I also don't understand why you say you want to start in E2, but end when the data is empty in col D. Is there a reason not to use a Do Loop

Code:
Sub ConCat()
 '
 ' ConCat Macro
 '

 '
 Dim oWS As Worksheet, lLastRow As Long, r As Long

 Set oWS = ActiveSheet
[s] lLastRow = oWS.Cells.SpecialCells(xlLastCell).Row
 For r = 1 To lLastRow
[/s][COLOR=#EF2929]r = 1    ' Or change this to the first row of data
Do Until IsEmpty(Cells(oWS.Cells(r, 4))[/color] ' Combine A to D
[indent]If Len(oWS.Cells(r, 4)) > 0 And Len(oWS.Cells(r, 5)) > 0 Then
oWS.Cells(r, 5).Value = oWS.Cells(r, 1).Value & "~" & oWS.Cells(r, 2).Value & "~" & oWS.Cells(r, 3).Value & "~" & oWS.Cells(r, 4).Value
[s]End If[/s]
[COLOR=#EF2929]r = r + 1
[/indent]Loop[/color]
 Next
 End Sub

Additionally, unless you are using the code for multiple spreadsheets, you can drop the oWS. throughout the macro
 
Hi,

So your If statement starts off... If the length of the value in D1 And the lengthy of the values in E1 are greater than ZERO then do the concatenation. Same for the next and so on for each row.

We can't see your data, so ????

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Skip
Yep my bad, "If Len(oWS.Cells(r, 4)) > 0 And Len(oWS.Cells(r, 5)) > 0 Then"..... should have read "If Len(oWS.Cells(r, 1)) > 0 And Len(oWS.Cells(r, 4)) > 0 Then...."

All works a treat now

Zelgar
Do Until IsEmpty(Cells(oWS.Cells(r, 4)) ' Combine A to D keeps throwing a debug error but not too fussed as it now works as I want it, and going forward we will be using multiple spreadsheets, but thanks
 
It would be nice if you would post your solution to your problem so others - who may have the same/similar issue - can benefit from this thread.

"now works" doesn't really help anybody since nobody knows what you have done...

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Andy

I thought I had, I only changed one line as it was more an oversight on my part, but just in case it is misread here it is.

Sub ConCat()
'
' ConCat Macro
'

'
Dim oWS As Worksheet, lLastRow As Long, r As Long

Set oWS = ActiveSheet
lLastRow = oWS.Cells.SpecialCells(xlLastCell).Row
For r = 1 To lLastRow
' Combine A to D
If Len(oWS.Cells(r, 1)) > 0 And Len(oWS.Cells(r, 4)) > 0 Then
oWS.Cells(r, 5).Value = oWS.Cells(r, 1).Value & "~" & oWS.Cells(r, 2).Value & "~" & oWS.Cells(r, 3).Value & "~" & oWS.Cells(r, 4).Value
End If
Next
End Sub

Hope this helps.

Cheers

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top