Domanda

The left() function (not Leftsub()) is NOT populating the destination range. What am I doing wrong? Thank you!!!

Sub LeftSub()

Dim SourceRange As Range, DestinationRange As Range, i As Integer, LastRow As Integer

Worksheets("JE_data").Activate
Range("J2").Activate


LastRow = Cells(Rows.count, "A").End(xlUp).row

'Define our source range:
Set SourceRange = Worksheets("JE_data").Range(Cells(2, 10), Cells(LastRow, 10))

'Define our target range where we will print.
'Note that this is expected to be of same shape as source
Set DestinationRange = Worksheets("JE_data").Range(Cells(2, 11), Cells(LastRow, 11))

'Iterate through each source cell and print left 30 characters in target cell
For i = 2 To SourceRange.count

    DestinationRange(i, 11).Value = Left(SourceRange(i, 10).Value, 30)

Next i

End Sub

È stato utile?

Soluzione

The error is here

Left(SourceRange(i, 10).Value, 30)

Already you have defined SourceRange as a Range of Cells

Set SourceRange = Worksheets("JE_data").Range(Cells(2, 10), Cells(LastRow, 10))

Again you are pointing using i, 10 gives you

.Range(Cells(2, 10), Cells(LastRow, 10))(i,10)

That points to the cell relative to upper left cell of your selection which means (2+i,10+10)th Cell (as (2,10) is your Upper left cell of selected Range) that doesn't work. Instead you can directly use this command

Left(Worksheets("JE_data").Cells(i,10).Value, 30)

And for DestinationRange As well

Worksheets("JE_data").Cells(i, 11).Value = Left(Worksheets("JE_data").Cells(i,10).Value, 30)

Hope this helps

Altri suggerimenti

Just from quickly looking at it - try replacing

DestinationRange(i, 11).Value = Left(SourceRange(i, 10).Value, 30)

with

DestinationRange(i, 1).Value = Left(SourceRange(i, 1).Value, 30)

When I want to find the last cell in a sheet, I use this:

llof = ActiveSheet.UsedRange.SpecialCells(xlLastCell).address

or

llof = ActiveSheet.UsedRange.SpecialCells(xlLastCell).row

the beauty of this command is that it re-sets the spreadsheet pointers so that any un-used space from adding and deleting lines are removed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top