简体   繁体   中英

VBA- Concatenate Cells with If function

I have a code that returns all the data I want from a query. The problem is that the csv format separates the data, even account names with commas in them. For each row, column A is the account name followed by 11 blocks of integer data (making a total of 12 cells used per row). Fortunately, for the accounts that have commas, the result is only one additional cell (making a total of 13 cells used per row).

I need an IF-THEN formula that will concatenate Column A & Column B if there are 13 used cells in that row, otherwise leaving things alone. Being new to VBA concatenate is giving me huge problems.

Any suggestions? Thanks in advance.

Well, just from a term point of view, Excel Formulas and Excel VBA are not the same thing.

In fact, there are a lot of people on here that are formula pros that know nothing about VBA and visa versa.

That being said, you could use something like:

=IF(COUNTBLANK(C1:N1)=0,A1&" "&B1,A1)

Of course, assuming your data looks like this:

图片

It would be easier to use:

=IF(N1="",A1,A1&" "&B1)

Concatenating in Excel formulas and VBA is pretty simple.

Simply separate the strings you wish to concatenate with an amperstand &

If I want to concatenate A1 and B1, I can use =A1&B1 as shown in the formula.

If I want to add a space, or even a word, I can just add it between them as a string:

=A1&" is way cooler than "&B1

Edit:

VBA:

Sub ConcatenateCells()
Application.ScreenUpdating = False
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
    If InStr(c, ",") > 0 Then c = c & c.Offset(0, 1)
    'You could also add a space between the two using c = c & " " & c.Offset(0, 1)
Next c
Application.ScreenUpdating = True
End Sub

I don't know if you want to scoot the data over after you have concatenated the values, but if you do, you can use this:

Sub ConcatenateAndScootCells()
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
    If InStr(c, ",") > 0 Then
        c = c & c.Offset(0, 1)
        c.Offset(0, 1).Delete xlToLeft
    End If
Next c
End Sub

Still assuming I got your data format right, here is what the before and after looks like:

之前

后

Final Edit:

If you absolutely insist on counting the number of used rows instead of simply looking for a coma in column A, then use this:

Sub ConcatenateAndScootCells()
Dim FixRange As Range, c As Range
Set FixRange = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
For Each c In FixRange
    If Cells(c.row, Columns.Count).End(xlToLeft).Column = 13 Then
        c = c & " " & c.Offset(0, 1)
        c.Offset(0, 1).Delete xlToLeft
    End If
Next c
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM