根据单元格内容复制到各个列 [英] Copy to various columns based on cell contents

查看:70
本文介绍了根据单元格内容复制到各个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBA的新手,我需要创建宏,该宏将在A列中查找包含测试",数据"或新"的所有单元,并将所有这些单元复制到B列(从B2开始),所有其他单元格都应从C2开始复制到C列.

I'm new to VBA and I need to create macro, which will find all cells in column A that contain "test", "data" or "new" and copy all these cells to column B, starting in B2, all other cells should be copied to column C, starting in C2.

推荐答案

打开VBA编辑器,将以下代码粘贴到Sheet1中(在Microsoft Excel对象下):

Open your VBA editor and paste below code in Sheet1 (under Microsoft Excel Object):

Option Explicit

Sub MyMacro()
Dim iLastRow As Long 
Dim i As Long 
Dim sVal As String

iLastRow = range("A65536").End(xlUp).Row 'get last row in use

For i = 2 To iLastRow 'iterate trough rows from second to last in use
    sVal = range("A" & i).Value
    If sVal = "test" Or sVal = "data" Or sVal = "new" Then
        range("B" & i).Value = sVal
    Else
        If sVal <> "" Then range("C" & i).Value = sVal
    End If

Next i 
End Sub

确保将鼠标指针放在过程MyMacro中的某个位置,然后按F5键执行代码.它应该可以满足您的要求:-)

Ensure that mouse pointer is set somewhere inside procedure MyMacro and hit F5 to execute code. It should do the magic you've asked for :-)

这篇关于根据单元格内容复制到各个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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