Set variable columns between worksheets

2019-08-14 14:49发布

Who wants easy points? I am trying to set a range of columns from one sheet to another using variables. startm will always be an integer. I know I can do something like this or this but I hate offsetXD Is there a "smarter" way besides concatenating a million "s and "&"s??

For Each s In thisWB.Worksheets
...
With ws
.Columns(3:14-startm+1)=s.columns(startm:14) 'this don't work ;_;

3条回答
我想做一个坏孩纸
2楼-- · 2019-08-14 15:16

As requested, I would use:

.Columns(3).Resize(, 14 - startm + 1).value = s.Columns(startm).Resize(, 14 - startm + 1).value
查看更多
别忘想泡老子
3楼-- · 2019-08-14 15:17

Since you are working with column numbers instead of letters, the best approach here is a for loop:

For Each s In thisWB.Worksheets
...
With ws

    For col = 3 to 14-startm+1
        'may have to adjust these values in here to fit your dynamic need, but
        '  the overall idea here is sound
        .Columns(col).Value = s.Columns(col+startm).Value
    Next col
End ws
Next s

I didn't reverse engineer your startm +/- stuff here on backhalf of that .columns()... line, but this should do what you need after filling in that blank.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-08-14 15:23

You could try something like this:

Range(.Cells(1, 3), .Cells(1, 14 - startm + 1)).EntireColumn = _
   Range(Sheets(1).Cells(1, startm), Sheets(1).Cells(1, 14)).EntireColumn.Value

But without certain limitations, it could be slow

查看更多
登录 后发表回答