我遇到以下代码的问题。 该代码应该复制长字符串。 源文件将此内容作为字符串。它长约50个字符。有数字和字母混合。
问题是,对于某些粘贴范围,它们是科学记数法,其他功能无法查找。
以下是代码的相关部分
For i = 1 To Columns.Count
If Left(ActiveWorkbook.Sheets(1).Cells(7, i).Value, 4) = "1068" Then
ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(7, i).End(xlUp), ActiveWorkbook.Sheets(1).Cells(7, i).End(xlDown)).Copy _
Destination:=ThisWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
End If
Next i
有没有办法只粘贴这样一个范围的值? 我尝试使用方法“粘贴值”,但得到了VBA错误消息
答案 0 :(得分:0)
而不是:
ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(7, i).End(xlUp), ActiveWorkbook.Sheets(1).Cells(7, i).End(xlDown)).Copy _
Destination:=ThisWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
请改为:
ActiveWorkbook.Sheets(1).Range( _
ActiveWorkbook.Sheets(1).Cells(7, i).End(xlUp), _
ActiveWorkbook.Sheets(1).Cells(7, i).End(xlDown) _
).Copy
ThisWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
或者,我建议采取一些措施使代码更清晰:
Dim rngCopyFrom as Range
Dim rngCopyTo as Range
Set rngCopyFrom = ActiveWorkbook.Sheets(1).Range( _
ActiveWorkbook.Sheets(1).Cells(7, i).End(xlUp), _
ActiveWorkbook.Sheets(1).Cells(7, i).End(xlDown) _
)
Set rngCopyTo = ThisWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
rngCopyFrom.Copy
rngCopyTo.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode=False
答案 1 :(得分:0)
更改单元格的格式。
您可以使用以下方法进行更正:
.NumberFormat = "##0_)"
以下是如何管理格式的示例:
ActiveWorkbook.Sheets(1).Cells(7, i).NumberFormat = "##0_)"
ActiveWorkbook.Sheets(1).Range(ActiveWorkbook.Sheets(1).Cells(7, i).End(xlUp), ActiveWorkbook.Sheets(1).Cells(7, i).End(xlDown)).NumberFormat = "##0_)"
ThisWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).NumberFormat = "##0_)"
更改格式后,不应显示科学记数法。
答案 2 :(得分:0)
将数字字符串转换为数值:
t = ActiveWorkbook.Sheets(1).Range(Cells(7, I).End(xlUp), _
ActiveWorkbook.Sheets(1).Cells(7, I).End(xlDown) _
).Value
Set dest = ThisWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Cells(1).Resize(UBound(t))
dest.NumberFormat = ""
dest.FormulaLocal = t