删除数组列,并更改两列的位置 [英] Delete an array column, and change position of two columns

查看:50
本文介绍了删除数组列,并更改两列的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,假设我有一个4x4的各种数字数组.

So let's say i have and 4x4 array of various numbers.

我要删除第三列数组,并切换第二列和第四列在数组中的位置.

I want to delete the third array column, and switch positions of the second and fourth columns within the array.

最终的目标是将信息从工作表复制到数组中,并准备将数组粘贴到另一张工作表中.

The ultimate goal is copying information from a sheet into an array, and prepping the array to paste into another sheet.

我该怎么做?

Sub test()
Dim Arr as variant
Arr=Sheets("Worksheet").Range("A1:D4")
'Delete third column
'Switch second and what was the fourth but now is the 3rd column.
Sheets("Sheet2").Range("A1").Resize(UBound(Arr, 1), UBound(Arr, 4)) = Arr
End Sub

推荐答案

删除数组中的'n'开关列

代码用第二列中的数据覆盖第三列,同时用第四列中的数据覆盖第二列.最后,最后一列(第四列)被删除.

Delete 'n' Switch Column in Array

The code overwrites the third column with data from the second column, and at the same time overwrites the second column with data from the fourth column. Finally the last (fourth) column is removed.

Sub deleteAndSwitch()
    
    Dim wb As Workbook: Set wb = ThisWorkbook

    Dim Data As Variant, i As Long
    Data = wb.Worksheets("Worksheet").Range("A1:D4").Value
    For i = 1 To UBound(Data)
        Data(i, 3) = Data(i, 2)
        Data(i, 2) = Data(i, 4)
    Next i
    ReDim Preserve Data(1 To UBound(Data), 1 To 3)
    'e.g.
    wb.Worksheets("Sheet2").Range("A1") _
      .Resize(UBound(Data), UBound(Data, 2)).Value = Data
 
End Sub

这篇关于删除数组列,并更改两列的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆