Excel 删除行并在声明为变量的范围内插入行

Excel 删除行并在声明为变量的范围内插入行,excel,vba,Excel,Vba,我必须检查完整的A列,检查单元格值长度是否小于6,然后将其删除 我以前在工作表上做过这样的操作 activesheet.range("A" & row_number).select selection.entirerow.delete 正如人们建议使用变体一样,我希望使用变体 我已将一组范围转换为变体 dim var as variant var=sheet1.range("A1:D1000").value 假设我有20行,其列中的单元格值长度小于6。我必须删除varian

我必须检查完整的A列,检查单元格值长度是否小于6,然后将其删除

我以前在工作表上做过这样的操作

 activesheet.range("A" & row_number).select
 selection.entirerow.delete
正如人们建议使用变体一样,我希望使用变体

我已将一组范围转换为变体

 dim var as variant 
 var=sheet1.range("A1:D1000").value
假设我有20行,其列中的单元格值长度小于6。我必须删除variant变量中的这20行,包括variant中的其他相应列,即B、C、D。我的意思是应该完全删除var(“A18:D18”)

我听说人们说我们不能从一个变体中删除条目,我们应该使用一个新的变体,只将这些值复制到新的变体中。如果是这种情况,我如何将一个变体复制到另一个变体

对于列单元格值长度大于6的行,应将单元格值转换为标准格式。我已经用这张纸做了

activesheet.cells("some cell!).value=activesheet.cells("").value
我在每一行中循环,这需要一些时间,因为每次我都在敲表寻找值。我现在想使用变体,获取完整的范围,执行操作并将其写回

如何删除我们在var(“A2:D2”)等变量中输入的完整行,然后将var(“A4:D4”)值复制到var2(“A6:D6”)等其他变量

我们是否可以像在工作表中插入行一样,在变量的中间插入一个条目

 activesheet.range("A" & row_number).select
 selection.entirerow.delete
  • 第一张工作表(数组X)上的值为A1:D1000
  • 测试单元格中的每一列,查看其长度是否超过6个字符
  • 如果大于6,则整行(本例中为4个单元格)将写入为 数组Y中的新行
  • 简化后的数组将写入第六个工作表中的A1
  • 将sheet1列的格式添加到sheet2列

    Sub VarExample()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = ActiveWorkbook.Sheets(1)
    Set ws2 = ActiveWorkbook.Sheets(2)
    Dim X
    Dim Y
    Dim lngRow As Long
    Dim lngCOl As Long
    Dim lngCnt As Long
    'define the size of the array to be processed on sheet 1
    X = ws1.Range("A1:D1000").Value2
    'make the second array the same size as the first
    ReDim Y(1 To UBound(X, 1), 1 To UBound(X, 2))
    'Look at the first record in each row  [,1] part to see if it is longer than 6 chars
    For lngRow = 1 To UBound(X, 1)
        If Len(X(lngRow, 1)) > 6 Then
         'Longer than 6 so add 1 more row to the length of the 2nd array
            lngCnt = lngCnt + 1
            'Loop through value in this row of the first array and place in the second array
            For lngCOl = 1 To UBound(X, 2)
                Y(lngCnt, lngCOl) = X(lngRow, lngCOl)
            Next lngCOl
        End If
    Next
    'create a range on the second sheet equal in size to the second array and dump the array to it
    ws1.[a1].Resize(UBound(Y, 1), UBound(Y, 2)).Value2 = Y
    'copy formatting
    ws1.[a1].Resize(1, UBound(X, 2)).EntireColumn.Copy
    ws2.[a1].Resize(1, UBound(X, 2)).EntireColumn.PasteSpecial Paste:=xlPasteFormats
    Application.CutCopyMode = False
    End Sub
    
  • 像这样的

     activesheet.range("A" & row_number).select
     selection.entirerow.delete
    
  • 第一张工作表(数组X)上的值为A1:D1000
  • 测试单元格中的每一列,查看其长度是否超过6个字符
  • 如果大于6,则整行(本例中为4个单元格)将写入为 数组Y中的新行
  • 简化后的数组将写入第六个工作表中的A1
  • 将sheet1列的格式添加到sheet2列

    Sub VarExample()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = ActiveWorkbook.Sheets(1)
    Set ws2 = ActiveWorkbook.Sheets(2)
    Dim X
    Dim Y
    Dim lngRow As Long
    Dim lngCOl As Long
    Dim lngCnt As Long
    'define the size of the array to be processed on sheet 1
    X = ws1.Range("A1:D1000").Value2
    'make the second array the same size as the first
    ReDim Y(1 To UBound(X, 1), 1 To UBound(X, 2))
    'Look at the first record in each row  [,1] part to see if it is longer than 6 chars
    For lngRow = 1 To UBound(X, 1)
        If Len(X(lngRow, 1)) > 6 Then
         'Longer than 6 so add 1 more row to the length of the 2nd array
            lngCnt = lngCnt + 1
            'Loop through value in this row of the first array and place in the second array
            For lngCOl = 1 To UBound(X, 2)
                Y(lngCnt, lngCOl) = X(lngRow, lngCOl)
            Next lngCOl
        End If
    Next
    'create a range on the second sheet equal in size to the second array and dump the array to it
    ws1.[a1].Resize(UBound(Y, 1), UBound(Y, 2)).Value2 = Y
    'copy formatting
    ws1.[a1].Resize(1, UBound(X, 2)).EntireColumn.Copy
    ws2.[a1].Resize(1, UBound(X, 2)).EntireColumn.PasteSpecial Paste:=xlPasteFormats
    Application.CutCopyMode = False
    End Sub
    

  • 根据要求添加niko的评论,如果您有任何进一步的疑问,请告诉我。干杯建议按照要求添加niko,如果您有任何进一步的疑问,请告诉我。干杯