[英]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. 问题是csv格式会分隔数据,即使是其中包含逗号的帐户名也是如此。 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). 对于每一行,列A是帐户名,后跟11个整数数据块(每行总共使用12个单元格)。 Fortunately, for the accounts that have commas, the result is only one additional cell (making a total of 13 cells used per row). 幸运的是,对于具有逗号的帐户,结果仅是一个额外的单元格(每行总共使用了13个单元格)。
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. 我需要一个IF-THEN公式,如果该行中有13个用过的单元格,它将连接A列和B列,否则就不用管它了。 Being new to VBA concatenate is giving me huge problems. 刚接触VBA会给我带来很多问题。
Any suggestions? 有什么建议么? Thanks in advance. 提前致谢。
Well, just from a term point of view, Excel Formulas and Excel VBA are not the same thing. 好吧,从术语的角度来看,Excel公式和Excel VBA并不是同一件事。
In fact, there are a lot of people on here that are formula pros that know nothing about VBA and visa versa. 实际上,这里的很多人都是配方专家,对VBA一无所知,反之亦然。
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. 在Excel公式和VBA中进行串联非常简单。
Simply separate the strings you wish to concatenate with an amperstand &
简单地分开,你希望与amperstand来连接字符串&
If I want to concatenate A1 and B1, I can use =A1&B1
as shown in the formula. 如果要串联A1和B1,可以使用=A1&B1
,如公式所示。
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: 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: 如果您绝对要计数使用的行数,而不是简单地在A列中查找逗号,请使用以下方法:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.