Excel 使用数组将范围转换为字符串值
我想将一系列单元格从整数转换为字符串。然而,由于我有太多的数据,我不能对范围使用标准循环,因为它需要太长的时间。 相反,我想使用数组并将所需的范围(数组)转换为字符串值 这就是我试图通过修改将范围转换为字符串的standardcode来实现的,而不是我将在数组的下面部分中使用的范围:Excel 使用数组将范围转换为字符串值,excel,vba,Excel,Vba,我想将一系列单元格从整数转换为字符串。然而,由于我有太多的数据,我不能对范围使用标准循环,因为它需要太长的时间。 相反,我想使用数组并将所需的范围(数组)转换为字符串值 这就是我试图通过修改将范围转换为字符串的standardcode来实现的,而不是我将在数组的下面部分中使用的范围: Sub CovertToString() Dim ws As Worksheet Set ws = Sheets("Sheet1") Dim sArray As Variant Dim LastRow As In
Sub CovertToString()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Dim sArray As Variant
Dim LastRow As Integer
Dim cell As Variant
With ws
LastRow = .Cells(.rows.Count, 1).End(xlUp).row
sArray = .Range(.Cells(1, 8), .Cells(LastRow, 8))
For Each cell In sArray
cell = "'" & cell.Value
Next
End With
End Sub
不幸的是,它不起作用,我理解,因为我不知道如何更正它。这种方式将单元格格式转换为
文本:
Sub ConvertToString()
Dim ws As Worksheet
Dim LastCell As Range
Dim rCell As Range
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws
Set LastCell = .Cells(.Rows.Count, 1).End(xlUp).Offset(, 7)
'Convert format to 'Text'
.Range(.Cells(1, 8), LastCell).NumberFormat = "@"
End With
End Sub
这种方法将把范围复制到数组中,并在返回工作表之前向每个值添加一个“
:
Sub ConvertToString()
Dim ws As Worksheet
Dim LastCell As Range
Dim vValues() As Variant
Dim R As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws
'Your code is looking for last cell in column A, so offset to column H once found.
'This is a reference to the last cell, not the row number so can be used in the range.
Set LastCell = .Cells(.Rows.Count, 1).End(xlUp).Offset(, 7)
vValues = .Range(.Cells(1, 8), LastCell).Value
'Add a ' to each value.
For R = 1 To UBound(vValues, 1)
vValues(R, 1) = "'" & vValues(R, 1)
Next R
'Paste back to sheet.
.Range(.Cells(1, 8), LastCell) = vValues
End With
End Sub
第二个阵列解决方案非常完美:)谢谢!