简体   繁体   English

Excel 将数据从 1 个工作表复制剪切粘贴到另一个工作表中,状态在工作表 2 新列中更新

[英]Excel copy cut paste data from 1 sheet to another with a status in updated in sheet 2 new column

I am new to macro I have created a macro that copies data from excel sheet1 column A & B and paste it in sheet 2 with status as updates in column c.我是宏的新手我创建了一个宏,该宏从 Excel sheet1 列 A 和 B 复制数据并将其粘贴到工作表 2 中,状态为 c 列中的更新。 However, it is not working properly it executes with incorrect/incomplete way like for some values in sheet 2 column B it shows updated in column c but for some, it does not... Please help me below is my code.但是,它无法正常工作,它以不正确/不完整的方式执行,例如对于第 2 列 B 中的某些值,它在列 c 中显示更新,但对于某些情况,它没有......请帮助我下面是我的代码。 Secondly, I have coded first for copy paste data from sheet1 to sheet2 there I have specified the range A2:A9999 and B2:B9999 I am not able to simplify it.其次,我首先编码了从 sheet1 到 sheet2 的复制粘贴数据,我指定了范围 A2:A9999 和 B2:B9999 我无法简化它。 I mean it should take the entire column A and B than the specified range.我的意思是它应该占用整个 A 列和 B 列而不是指定范围。 Please help me with these 2 parts.............请帮我处理这两个部分......................

下面是输出,如果您看到单元格 C2 和 C3 为空白,它也应该填充一条注释更新,这不会发生在我的代码中

Sub CopyData()

Dim i As Long
Dim wt As Excel.Worksheet

Set wr = Worksheets("Sheet2")

'Copies and cuts the data from sheet1(TIS) and paste the same in sheet2

With Worksheets("SampleFile")
    .Range("A2:A9999").Copy wr.Range("A2") 'Copy
    .Range("A2:A9999").Cut wr.Range("A2") 'Cut
    .Range("B2:B9999").Copy wr.Range("B2") 'Copy
    .Range("B2:B9999").Cut wr.Range("B2") 'Cut
End With


For i = 1 To wr.Cells(wr.Rows.Count, "B").End(xlUp).Row
If wr.Range("B" & i).Value = "FXV" Then
   wr.Range("C" & i).Value = "Updated"
   ElseIf wr.Range("B" & i).Value = "FST" Then
   wr.Range("C" & i).Value = "Updated"
   ElseIf wr.Range("B" & i).Value = "FLB" Then
   wr.Range("C" & i).Value = "Updated"
   ElseIf wr.Range("B" & i).Value = "FFH" Then
   wr.Range("C" & i).Value = "Updated"
   ElseIf wr.Range("B" & i).Value = "FFJ" Then
   wr.Range("C" & i).Value = "Updated"
   
   End If
Next i
End Sub

This code should cut data from A2 to B and LastRow in worksheet SampleFile and paste it to Range A2 in worksheet Sheet2 .此代码应将工作表SampleFile数据从 A2 剪切到 B 和LastRow并将其粘贴到工作表Sheet2范围 A2 。 Then it will loop through all the rows in Sheet2 looking for the value in column B, if it matches the Select Cases will input Updated in column C:然后它将遍历Sheet2所有行以查找 B 列中的值,如果匹配,则Select Cases将在 C 列中输入Updated

Option Explicit
Sub CopyData()

    Dim wr As Worksheet: Set wr = Worksheets("Sheet2")
    'Copies and cuts the data from sheet1(TIS) and paste the same in sheet2
    'there is no need to copy if you are going to cut
    'also use a defined range to copy instead 9999 rows
    With ThisWorkbook.Worksheets("SampleFile")
        Dim LastRow As Long: LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        'you can also cut both columns at once
        .Range("A2:B" & LastRow).Cut wr.Range("A2") 'Cut
    End With
    
    Dim i As Long
    With wr
        For i = 1 To .Cells(.Rows.Count, "B").End(xlUp).Row
        'in this case is way shorter to code using the Select statement
        'you could also use If x = y or x = z or x = a but Select looks cleaner.
            .Cells(i, "B") = Trim(.Cells(i, "B"))
            Select Case .Range("B" & i)
                Case "FXV", "FST", "FLB", "FFH", "FFJ"
                    .Range("C" & i) = "Updated"
            End Select
        Next i
    End With
    
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在新工作表中剪切复制粘贴并在新列中将状态更新为“已更新” - Cut Copy Paste in new sheet and updates status as “Updated” in new column 将数据从 1 个工作表复制到另一个状态为已更新的工作表 - Copy data from 1 sheet to another sheet with a status as updated 复制并粘贴到另一个工作表Excel vba中的新列 - Copy and paste to new column in another sheet Excel vba 使用excel VBA将数据从一张纸复制并粘贴到另一张纸,然后从第二张纸复制到第三张纸 - Copy and paste data from one a sheet to another sheet, and from second sheet to third using excel VBA 过滤 excel 列并将粘贴复制到另一个新工作表,工作表名称应为过滤器值 - Filter a excel column and copy paste to another new sheet and the sheet name should be filter value 使用Excel VBA中的自动筛选器从Excel工作表剪切粘贴数据 - Cut Paste data from excel sheet using Autofilter in Excel VBA 如何使用列名从一张工作表复制数据并粘贴到具有相同列名的另一张工作表? - How to copy data from one sheet using column name and paste to another sheet with same column name? Excel VBA:过滤、剪切并粘贴到另一个工作表 - Excel VBA: Filter, cut, and paste to another sheet 从一张纸复制数据并粘贴到另一张纸的A列的第一个空白单元格中 - Copy data from one sheet and paste into the first blank cell in column A of another sheet 如何使用vba将多列数据从一张工作表复制并粘贴到另一张工作表 - How to copy and paste multiple column data from one sheet to another sheet using vba
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM