第1个回答 2011-02-15
添加一个模块,将以下代码写在里面,到时运行该宏
Public Sub AddTextB()
Dim EndRow As Long
Dim i As Long
Dim AddString As String
Dim Flag As Integer
Flag = 0
EndRow = Sheet1.Range("A65536").End(xlUp).Row
AddString = InputBox("输入要添加的文字", "输入", "ABCD")
For i = 1 To EndRow
If Len(Cells(i, "A").Value) <> 6 Then Flag = 1
Cells(i, "B") = CStr(Cells(i, "A")) & "-" & AddString
Next i
If Flag = 0 Then MsgBox "数据整理完毕!", vbOKOnly Else MsgBox "数据可能有误,请手工检查!", vbOKOnly
End Sub
顺便说明一下,您所提到的CONCATENATE()只能在表格里面用,而且它的功能和字符串连接符"&"是一样的,代码用&来连接多个字符串。
还有你补充的假设A列中有空行的情况,上述代码没有考虑,是以数据有误来对待的,即不等于6位字符,如果要考虑数据中间有空行也是正常的话,将第10行,11行改为
If Len(Cells(i, "A").Value) <> 6 And Len(Cells(i, "A").Value) <> 0 Then Flag = 1
If Len(Cells(i, "A").Value) <> 0 Then Cells(i, "B") = CStr(Cells(i, "A")) & "-" & AddString
不知到是不是你想要的。
第2个回答 2011-02-15
Sub AAA()
Dim A As String
A = InputBox("请输入连接字符串")
Range("A1").Select
x = 1
C = 0
Do Until Cells(x, 1).Value = ""
b = Cells(x, 1)
Cells(x, 2).Value = Cells(x, 1).Value + "-" + A
If Len(b) <> 6 Then
C = 1
End If
x = x + 1
Loop
If C = 1 Then
MsgBox "数据可能有误,请手工检查"
Else
MsgBox "数据整理完毕"
End If
End Sub
我的语句没有章法,但简单易懂,只要会一点VB就能看懂,程序中没有考虑A列有空行的情况
第3个回答 2011-02-15
Sub xAbc()
Dim Sht As Worksheet
Dim L As Integer '记于记录长度不是6的单元格个数
Dim xStr As String
xStr = InputBox("请输入文字符串:")
If Len(xStr) = 0 Then
MsgBox "输入为空,请重新进行。"
Exit Sub
End If
Set Sht = Sheets("sheet1")
L = 0
For i = 1 To Sht.[A65536].End(xlUp).Row
If Len(Cells(i, 1)) <> 6 Then L = L + 1
Cells(i, 2) = Cells(i, 1) & "-" & xStr
Next i
If L = 0 Then
MsgBox "数据整理完毕!"
Else
MsgBox "数据可能有误,请手工检查!"
End If
End Sub
就两个字符串,直接用 & 连接就行了。用函数反而效率低。
从A列的第一行到最后一个非空行。本回答被提问者采纳
第4个回答 2011-02-15
Dim i, er As Integer
Dim s As String
Dim err()
er = -1
s = InputBox("请输入B列文字", "输入窗口")
With ActiveSheet
For i = 1 To .UsedRange.Rows.Count
If .Cells(i, 1) = "" Then
Exit For
Else
.Cells(i, 2) = .Cells(i, 1) & "-" & s
If Len(.Cells(i, 1)) <> 6 Then
er = er + 1
ReDim Preserve err(er)
err(er) = i
End If
End If
Next i
If er > -1 Then
s = Join(err(), ",")
s = "第" + s + "行数据有误,请手工检查!"
MsgBox s
Else
MsgBox "数据处理完毕!"
End If
End With
End Sub