本文为《别怕,Excel VBA其实特别简单(第3版)》随书问题参考答案
要解决本例的问题,可以在将数据写入工作表前,先将超过15个字符的数据强制转为文本,如:
Sub 合并多表数据()
Dim EndRow As Long, DataArr As Variant, ToRng As Range
EndRow = Worksheets("中通快递").Range("A1048576").End(xlUp).Row
DataArr = Worksheets("中通快递").Range("A2:H" & EndRow).Value
Set ToRng = Worksheets("汇总结果").Range("A1048576").End(xlUp).Offset(1, 0)
'借助循环语句将数组中的数据转为文本格式
Dim a As Long, b As Long
For a = 1 To UBound(DataArr, 1)
For b = 1 To UBound(DataArr, 2)
'If语句判断数据是否超过15个字符
If Len(DataArr(a, b)) > 15 Then
DataArr(a, b) = "'" & DataArr(a, b)
End If
Next b
Next a
ToRng.Resize(UBound(DataArr, 1), 8) = DataArr
End Sub
解决问题的方法肯定不止一种,除此之外,你还能想出哪些解决问题的办法?