遍历范围,复制某些单元格,并粘贴格式到另一个工作表

时间:2020-08-14 07:30:20

标签: excel vba

我正在研究将范围内的某些单元格复制到另一个工作表的方法。一切正常,但我决定为其添加更多功能,现在我无法弄清这里出了什么问题。

调试器指向NameVar = .Range(.Cells(FirstWord.row + 1, FirstWord.Column), .Cells(SecondWord.row - 2, FirstWord.Column + 9)).Value2,说“类型不匹配”。

我正在尝试将单元格值存储到变量NameVar中。然后,我尝试根据选择的语言添加“件数” =“件数”的版本。这样它将是NameVar pcs在另一个变量NameVarResult中。最后,只需将变量结果传递到另一个工作表上的另一个单元格即可。

这是我当前的代码:

Private Sub copyToTable(SectionName As Variant, SearchWordOne As String, SearchWordTwo As String, RowToPaste As Integer, OperatingWorksheet As Worksheet)

    'On Error Resume Next

    Dim FirstWord, SecondWord
    Dim NameVar As String, NameVarResult As String
    Dim AmountVar As String, AmountVarResult As String
    Dim PriceVar As String, PriceVarResult As String
            
    Set FirstWord = OperatingWorksheet.Range("C:C").Find(SectionName & " - " & SearchWordOne, LookIn:=xlValues, lookat:=xlWhole)
    Set SecondWord = OperatingWorksheet.Range("C:C").Find(SectionName & " - " & SearchWordTwo, LookIn:=xlValues, lookat:=xlWhole)
    
    With OperatingWorksheet
        If Not FirstWord Is Nothing Then
            ' Copy - Paste name
            NameVar = .Range(.Cells(FirstWord.row + 1, FirstWord.Column), .Cells(SecondWord.row - 2, FirstWord.Column + 9)).Value2
            
            If ThisWorkbook.Worksheets("OtherData").Range("K80").Value = "English" Then
                NameVarResult = NameVar & " " & "pc(s)"
                ThisWorkbook.Worksheets("TableForOL").Range("B" & RowToPaste).Value = NameVarResult
            ElseIf ThisWorkbook.Worksheets("OtherData").Range("K80").Value = "German" Then
                NameVarResult = NameVar & " " & "Stck"
                ThisWorkbook.Worksheets("TableForOL").Range("B" & RowToPaste).Value = NameVarResult
            Else
                NameVarResult = NameVar & " " & "st"
                ThisWorkbook.Worksheets("TableForOL").Range("B" & RowToPaste).Value = NameVarResult
            End If
        
            ' Copy - Paste amount
            AmountVar = .Range(.Cells(FirstWord.row + 1, FirstWord.Column + 10), .Cells(SecondWord.row - 2, FirstWord.Column + 10)).Value
            
            If ThisWorkbook.Worksheets("OtherData").Range("K80").Value = "English" Then
                AmountVarResult = AmountVar & " " & "pc(s)"
                ThisWorkbook.Worksheets("TableForOL").Range("C" & RowToPaste).Value = AmountVarResult
            ElseIf ThisWorkbook.Worksheets("OtherData").Range("K80").Value = "German" Then
                AmountVarResult = AmountVar & " " & "Stck"
                ThisWorkbook.Worksheets("TableForOL").Range("C" & RowToPaste).Value = AmountVarResult
            Else
                AmountVarResult = AmountVar & " " & "st"
                ThisWorkbook.Worksheets("TableForOL").Range("C" & RowToPaste).Value = AmountVarResult
            End If
    
            ' Copy - Paste price
            PriceVar = .Range(.Cells(FirstWord.row + 1, FirstWord.Column + 17), .Cells(SecondWord.row - 2, FirstWord.Column + 17)).Value
            
            If ThisWorkbook.Worksheets("OtherData").Range("K80").Value = "English" Then
                PriceVarResult = PriceVar & " " & "pc(s)"
                ThisWorkbook.Worksheets("TableForOL").Range("E" & RowToPaste).Value = PriceVarResult
            ElseIf ThisWorkbook.Worksheets("OtherData").Range("K80").Value = "German" Then
                PriceVarResult = PriceVar & " " & "Stck"
                ThisWorkbook.Worksheets("TableForOL").Range("E" & RowToPaste).Value = PriceVarResult
            Else
                PriceVarResult = PriceVar & " " & "st"
                ThisWorkbook.Worksheets("TableForOL").Range("E" & RowToPaste).Value = PriceVarResult
            End If
        End If
    End With
    
End Sub

0 个答案:

没有答案