我有一个列,每个单元格都有一组数字。
我只想将列上的数据复制到最后一行,并在每个单元格后面留下最后3位数字。
我的最后一行有一个变量: LASTROW 和已设置的范围。
我的代码有点像这样
inputWS1.Range("A4:A" & lastRow).Copy outputWS.Range("B2")
正如我所说的那样,我希望将数据从A4复制到B2并打开,但是当我们移动到另一张纸上的B2时,我想删除A4上的最后3个字符。
请帮助并感谢您的帮助!
编辑:我当前的代码是:
Private Sub Update_Click()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim path As String, fileName As String
Dim lastRowUniversal As Long, lastRowOutput As Long, rowCntr As Long, lastColumn As Long
Dim inputWS1 As Worksheet, inputWS2 As Worksheet, inputWS3 As Worksheet, outputWS As Worksheet
Dim rng As Range
Dim arr As Variant
Dim i As Long
Const dtFORM As String = "=IF(ISNUMBER(J4:J<r>),DATE(YEAR(J4:J<r>)-1," & "MONTH(J4:J<r>),DAY(J4:J<r>)),J4:J<r>)"
'set your sheets here
Set inputWS1 = ThisWorkbook.Sheets("Universal")
Set inputWS2 = ThisWorkbook.Sheets("Geovera")
Set inputWS3 = ThisWorkbook.Sheets("Citizens")
Set outputWS = ThisWorkbook.Sheets("Carriers")
Set rng = inputWS3.Range("D2:D")
arr = inputWS3.Range("M2:M" & LastRowCitizens)
'get last rows from both sheets
lastRowUniversal = inputWS1.Cells(Rows.Count, "A").End(xlUp).Row
LastRowCitizens = inputWS3.Cells(Rows.Count, "A").End(xlUp).Row
lastRowGeovera = inputWS2.Cells(Rows.Count, "A").End(xlUp).Row
LastRowPolicy = outputWS.Cells(Rows.Count, "B").End(xlUp).Row
lastRowOutput = outputWS.Cells(Rows.Count, "A").End(xlUp).Row
lastColumn = inputWS1.Cells(1, Columns.Count).End(xlToLeft).Column
'get last rows from both sheets
lastRowUniversal = inputWS1.Cells(Rows.Count, "A").End(xlUp).Row
LastRowCitizens = inputWS3.Cells(Rows.Count, "A").End(xlUp).Row
lastRowGeovera = inputWS2.Cells(Rows.Count, "A").End(xlUp).Row
LastRowPolicy = outputWS.Cells(Rows.Count, "B").End(xlUp).Row
lastRowOutput = outputWS.Cells(Rows.Count, "A").End(xlUp).Row
lastColumn = inputWS1.Cells(1, Columns.Count).End(xlToLeft).Column
rowCntr = 1
For i = 1 To UBound(arr)
arr(i) = Left(arr(i), Len(arr(i)) - 3)
Next i
'get last rows from both sheets
lastRowUniversal = inputWS1.Cells(Rows.Count, "A").End(xlUp).Row
LastRowCitizens = inputWS3.Cells(Rows.Count, "A").End(xlUp).Row
lastRowGeovera = inputWS2.Cells(Rows.Count, "A").End(xlUp).Row
LastRowPolicy = outputWS.Cells(Rows.Count, "B").End(xlUp).Row
lastRowOutput = outputWS.Cells(Rows.Count, "A").End(xlUp).Row
lastColumn = inputWS1.Cells(1, Columns.Count).End(xlToLeft).Column
'Universal
inputWS1.Range("A4:A" & lastRowUniversal).Copy outputWS.Range("B2")
inputWS1.Range("B4:B" & lastRowUniversal).Copy outputWS.Range("C2")
inputWS1.Range("N4:N" & lastRowUniversal).Value = inputWS1.Name
inputWS1.Range("N4:N" & lastRowUniversal).Copy outputWS.Range("E2")
inputWS1.Range("L4:L" & lastRowUniversal).Value = inputWS1.Evaluate(Replace(dtFORM, "<r>", lastRowUniversal))
inputWS1.Range("L4:L" & lastRowUniversal).Copy outputWS.Range("G2")
inputWS1.Range("G4:G" & lastRowUniversal).Copy outputWS.Range("H2")
'Geovera
inputWS2.Range("F2:F" & lastRowGeovera).Copy outputWS.Range("B" & lastRowUniversal - 1)
inputWS2.Range("I2:I" & lastRowGeovera).Copy outputWS.Range("C" & lastRowUniversal - 1)
inputWS2.Range("P2:P" & lastRowGeovera).Value = inputWS2.Name
inputWS2.Range("P2:P" & lastRowGeovera).Copy outputWS.Range("E" & lastRowUniversal - 1)
inputWS2.Range("N2:N" & lastRowGeovera).Copy outputWS.Range("H" & lastRowUniversal - 1)
inputWS2.Range("G2:G" & lastRowGeovera).Copy outputWS.Range("G" & lastRowUniversal - 1)
'Citizens
inputWS3.Range("M2:M" & LastRowCitizens).Value = arr
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
答案 0 :(得分:3)
使用左侧功能
With inputWS3
For i = 2 To lastrow
.Cells(i, "M") = Left(.Cells(i, "M").Value, Len(.Cells(i, "M").Value) - 3)
Next i
End With
答案 1 :(得分:3)
正如h2so4指出Left
和Len
是一个很好的方法。但是,在实现这样的事情方面,循环数组而不是范围是非常重要的。如果你有大量的数据,它会快得多。
Sub Test()
Dim rng As Range
Dim arr As Variant
Dim i As Long
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:A1000") 'your input range
arr = Application.Transpose(rng) 'fill variant array with values from range
For i = 1 To UBound(arr)
arr(i) = Left(arr(i), Len(arr(i)) - 3) 'loop through array removing last three chars
Next i
rng.Value = Application.Transpose(arr) 'return array to the worksheet in one go
End Sub