Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 添加从第一个黑色行开始的数据_Excel_Vba - Fatal编程技术网

Excel 添加从第一个黑色行开始的数据

Excel 添加从第一个黑色行开始的数据,excel,vba,Excel,Vba,步骤1:我有一个VBA代码,可以根据某些条件(使用循环)在“sheet1”的“sheet2”中插入数据 步骤2:我需要创建一个新代码,循环创建的“sheet2”,并根据某些条件在其末尾添加步骤1“sheet2”上创建的部分行 例如: 我在步骤1-“Sheet2”中创建了图像上显示的行 在本文末尾,我只想介绍B列中名为contcontabil CREDIT“15.IB”的行,方法是将G列中名为VALOARE的值(在本例中为25,00)替换为其值*12,5%(25,00*12,5%=3,13)。

步骤1:我有一个VBA代码,可以根据某些条件(使用循环)在“sheet1”的“sheet2”中插入数据

步骤2:我需要创建一个新代码,循环创建的“sheet2”,并根据某些条件在其末尾添加步骤1“sheet2”上创建的部分行

例如: 我在步骤1-“Sheet2”中创建了图像上显示的行

在本文末尾,我只想介绍B列中名为contcontabil CREDIT“15.IB”的行,方法是将G列中名为VALOARE的值(在本例中为25,00)替换为其值*12,5%(25,00*12,5%=3,13)。 新结果如图所示(请查找添加的灰色行):

!我的问题:

  • 我希望循环在第一个图像上是静态的(不要转到最后一行,它将包含第二个图像上显示的新行)
  • 如何在第一个图像行下方插入新行,新行中包含数据,但包含如上所示计算的值
  • 提前谢谢

    请在下面找到我的尝试:

    exp_conta_lastRow = exp_conta.Cells(Rows.Count, 1).End(xlUp).row
    For j = 2 To exp_conta_lastRow
        If InStr(exp_conta.Cells(j, 2), "13.IO") Or _
            InStr(exp_conta.Cells(j, 2), "15.IA") Or _
            InStr(exp_conta.Cells(j, 2), "15.IB") Or _
            InStr(exp_conta.Cells(j, 2), "15.IC") Or _
            InStr(exp_conta.Cells(j, 2), "15.ID") Or _
            InStr(exp_conta.Cells(j, 2), "15.IH") Or _
            InStr(exp_conta.Cells(j, 2), "15.II") Or _
            InStr(exp_conta.Cells(j, 2), "15.IJ") Or _
            InStr(exp_conta.Cells(j, 2), "15.IK") Or _
            InStr(exp_conta.Cells(j, 2), "15.IL") Or _
            InStr(exp_conta.Cells(j, 2), "15.IM") Or _
            InStr(exp_conta.Cells(j, 2), "15.IN") Or _
            InStr(exp_conta.Cells(j, 2), "15.IP") Or _
            InStr(exp_conta.Cells(j, 2), "15.IR") Or _
            InStr(exp_conta.Cells(j, 2), "15.IS") Or _
            InStr(exp_conta.Cells(j, 2), "15.IV") Or _
            InStr(exp_conta.Cells(j, 2), "15.IW") Or _
            InStr(exp_conta.Cells(j, 2), "15.IX") Then
                exp_conta.Cells(exp_conta_lastRow + j - 1, 1) = "test" 'copy of the entire row and on the "Valoare column" * 12,5%, instead of the existing value
        End If
    Next
    

    请尝试下一个代码:

    Sub testInsertRowsAtTheEnd()
      Dim sh1 As Worksheet, sh2 As Worksheet, lastR1 As Long, lastR2 As Long, arrC As Variant
      Dim i As Long, arr As Variant, arr1 As Variant
      
      Set sh1 = ActiveSheet         ' use here your necessary sheet (the one where from the lines will be copied)
      Set sh2 = Worksheets("Shet2") 'idem as above in terms of naming...
      arr = Split("13.IO,15.IA,15.IB,15.IC,15.ID,15.IH,15.II,15.IJ,15.IL,15.IM,15.IN,15.IP,15.IR,15.IS,15.IV,15.IW,15.IX", ",")
      
      lastR1 = sh1.Range("A" & Rows.count).End(xlUp).row
      For i = 2 To lastR1
        If issOK(sh1.Range("B" & i).Value, arr) Then
            lastR2 = sh2.Range("A" & Rows.count).End(xlUp).row + 1 'first empty row
            arrC = sh1.Range("A" & i & ":H" & i).Value: arrC(1, 7) = arrC(1, 7) * 12.5 / 100
            sh2.Range("A" & lastR2).Resize(1, UBound(arrC, 2)).Value = arrC
        End If
      Next i
    End Sub
    
    Function issOK(strVal As String, arr As Variant) As Boolean ' it also accepts strings
       Dim El As Variant
       If IsArray(arr) Then
            For Each El In arr
                 If InStr(strVal, El) > 0 Then issOK = True: Exit Function
            Next
       Else
            If InStr(strVal, arr) > 0 Then issOK = True
       End If
    End Function
    
    编辑: 添加了一个函数,该函数能够处理迭代条件要考虑的字符串,如代码示例中所示,而不仅仅是问题描述中要求的字符串。您可以根据需要在逗号之间添加/更改值

    为了测试和更好地理解功能工作方式,请使用下一种方式:

    Sub testissOK()
      Dim arr As Variant, arr1 As Variant
      arr = Split("13.IO,15.IA,15.IB,15.IC,15.ID,15.IH,15.II,15.IJ,15.IL,15.IM,15.IN,15.IP,15.IR,15.IS,15.IV,15.IW,15.IX", ",")
      arr1 = Split("test1,test2", ",")
      
      Debug.Print issOK("Rupere13.IO2000", arr)
      Debug.Print issOK("acesta este un test", arr1), issOK("acesttest1esteOK", arr1)
      Debug.Print issOK("test2detestare", "test2"), issOK("test2detestare", "test1")
      If issOK("testscriere13.IOetcetera", arr) Then
          Debug.Print "Este, nene..."
      End If
      If issOK("testscrieretest1etcetera", arr1) Then
           Debug.Print "OK si-aici..."
      End If
    End Sub
    

    请尝试下一个代码:

    Sub testInsertRowsAtTheEnd()
      Dim sh1 As Worksheet, sh2 As Worksheet, lastR1 As Long, lastR2 As Long, arrC As Variant
      Dim i As Long, arr As Variant, arr1 As Variant
      
      Set sh1 = ActiveSheet         ' use here your necessary sheet (the one where from the lines will be copied)
      Set sh2 = Worksheets("Shet2") 'idem as above in terms of naming...
      arr = Split("13.IO,15.IA,15.IB,15.IC,15.ID,15.IH,15.II,15.IJ,15.IL,15.IM,15.IN,15.IP,15.IR,15.IS,15.IV,15.IW,15.IX", ",")
      
      lastR1 = sh1.Range("A" & Rows.count).End(xlUp).row
      For i = 2 To lastR1
        If issOK(sh1.Range("B" & i).Value, arr) Then
            lastR2 = sh2.Range("A" & Rows.count).End(xlUp).row + 1 'first empty row
            arrC = sh1.Range("A" & i & ":H" & i).Value: arrC(1, 7) = arrC(1, 7) * 12.5 / 100
            sh2.Range("A" & lastR2).Resize(1, UBound(arrC, 2)).Value = arrC
        End If
      Next i
    End Sub
    
    Function issOK(strVal As String, arr As Variant) As Boolean ' it also accepts strings
       Dim El As Variant
       If IsArray(arr) Then
            For Each El In arr
                 If InStr(strVal, El) > 0 Then issOK = True: Exit Function
            Next
       Else
            If InStr(strVal, arr) > 0 Then issOK = True
       End If
    End Function
    
    编辑: 添加了一个函数,该函数能够处理迭代条件要考虑的字符串,如代码示例中所示,而不仅仅是问题描述中要求的字符串。您可以根据需要在逗号之间添加/更改值

    为了测试和更好地理解功能工作方式,请使用下一种方式:

    Sub testissOK()
      Dim arr As Variant, arr1 As Variant
      arr = Split("13.IO,15.IA,15.IB,15.IC,15.ID,15.IH,15.II,15.IJ,15.IL,15.IM,15.IN,15.IP,15.IR,15.IS,15.IV,15.IW,15.IX", ",")
      arr1 = Split("test1,test2", ",")
      
      Debug.Print issOK("Rupere13.IO2000", arr)
      Debug.Print issOK("acesta este un test", arr1), issOK("acesttest1esteOK", arr1)
      Debug.Print issOK("test2detestare", "test2"), issOK("test2detestare", "test1")
      If issOK("testscriere13.IOetcetera", arr) Then
          Debug.Print "Este, nene..."
      End If
      If issOK("testscrieretest1etcetera", arr1) Then
           Debug.Print "OK si-aici..."
      End If
    End Sub
    
    试试看

    试试看


    我不知道用什么值来替换G:G列中的25…两张图片都显示了25…@FaneDuru我想添加一个具有相同数据的新行,而不是原始行中的25,以获得一个新值,即25(在我们的示例中)*12,5%。我们的想法是让包含保费的行和其他具有相同信息但仅包含税务价值的行我不知道使用什么值来替换G列中的25:G…两张图片都显示了25…@FaneDuru我想添加一个具有相同数据的新行,而不是原始行中的25,以获得一个新值ch是25(在我们的例子中)*12,5%。我们的想法是让包含保费的行和其他具有相同信息但只包含税值的行我不确定我是否得到了该函数。如果arr包含其中一个值,该函数将返回true。如果不退出函数?我想添加更多类似的条件。例如:如果是“18.IT”“那么价值*2,5%等。根据产品的不同,我有更多的税%。此外,需要在上述代码上使用该函数。我说的对吗?@Alin Iustinian Toderita:在某种程度上(仅仅)。。。我的意思是,
    Instr
    找到一个数组元素,它使函数为真并退出它。如果不是,则迭代将进行到最后一个元素,但没有找到匹配项,则函数为
    False
    。为了做到你所说的,我将调整这个概念。我的意思是,我将在主代码中创建数组,并将其作为参数传递给函数。这样,根据传递的数组,可以使用相同的函数设置不同的条件。我会在几分钟后发布更新的答案…我不确定我是否得到了这个功能。如果arr包含其中一个值,则函数将返回true。如果没有退出功能?我想增加更多这样的条件。如果是“18.IT”,那么值*2,5%等等。根据产品的不同,我有更多的税%。此外,需要在上述代码上使用该函数。我说的对吗?@Alin Iustinian Toderita:在某种程度上(仅仅)。。。我的意思是,
    Instr
    找到一个数组元素,它使函数为真并退出它。如果不是,则迭代将进行到最后一个元素,但没有找到匹配项,则函数为
    False
    。为了做到你所说的,我将调整这个概念。我的意思是,我将在主代码中创建数组,并将其作为参数传递给函数。这样,根据传递的数组,可以使用相同的函数设置不同的条件。我会在几分钟后发布更新的答案。。。