Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90301

How to copy a range to another worksheet with for each loop?

$
0
0

I would like to copy a range to an another worksheet and to insert before it 2 columns on the new worksheet. It doesn't copy the original range.

It works, if I copy to the same worksheet.

Set ws = ThisWorkbook.Sheets("Sh2")
Set ws2 = ThisWorkbook.Sheets("Sh3")
Set wb = ThisWorkbook

rowNum = ws.UsedRange.Rows.Count

For Each row In ws.Range("A1:A"& rowNum)
    ws2.Range("A1:A"& rowNum).Offset(0, 0).Value2 = wb.Name
    ws2.Range("A1:A"& rowNum).Offset(0, 1).Value2 = ws.Name
Next row

For Each cell In ws.UsedRange
    cell.Offset(10, 2).Value2 = cell.Value2
Next cell

I expect the column "A" contains the name of active workbook, the "B" the name of active worksheet, the other rows comes the original content. Now, the filename and worksheet's name copied to the new worksheet, but the content not.


Viewing all articles
Browse latest Browse all 90301

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>