Excel宏-基于另一列的值获取一列的值

Excel宏-基于另一列的值获取一列的值,excel,vba,Excel,Vba,如果列B中存在值,我需要一个宏来写入列a中的行值 例如: Column A Column B Arjun Arun 12 对于上面的例子,我需要一个宏,它可以在工作手册的第2页中写“Arun 12”,标题为“Name”和“Hours”。在此之前,宏应该完全清除第2页中的数据。这应该可以完成您的任务 Sub DoStuff() Dim lastRow As integer, lastRowSheet2 As integer, i As Integer Dim sheet1

如果列B中存在值,我需要一个宏来写入列a中的行值

例如:

Column A   Column B
Arjun
Arun         12

对于上面的例子,我需要一个宏,它可以在工作手册的第2页中写“Arun 12”,标题为“Name”和“Hours”。在此之前,宏应该完全清除第2页中的数据。

这应该可以完成您的任务

Sub DoStuff()

Dim lastRow As integer, lastRowSheet2 As integer, i As Integer
Dim sheet1 As WorkSheet, sheet2 As Worksheet

Set sheet1 = Sheets("Sheet1")
Set sheet2 = Sheets("Sheet2")

lastRow = sheet1.Range("A" & Rows.Count).End(xlUp).Row

sheet2.Cells.Clear

For i = 1 To lastRow

If sheet1.Range("A" & i).Value <> "" And sheet1.Range("B" & i).Value <> "" then

    lastRowSheet2 = sheet2.Range("A" & Rows.Count).End(xlUp).Row

    sheet1.Range("A" & i & ":B" & i).Copy Destination:= sheet2.Range("A" & lastRowSheet2 + 1)

End If

Next i

End Sub
Sub-DoStuff()
将lastRow设置为整数,将lastRowSheet2设置为整数,将i设置为整数
将图纸1标注为工作表,将图纸2标注为工作表
设置图纸1=图纸(“图纸1”)
设置图纸2=图纸(“图纸2”)
lastRow=sheet1.Range(“A”和Rows.Count).End(xlUp).Row
表2.1.1.2清除
对于i=1到最后一行
如果sheet1.Range(“A”&i).Value”和sheet1.Range(“B”&i).Value”),则
lastRowSheet2=sheet2.Range(“A”和Rows.Count).End(xlUp).Row
sheet1.范围(“A”&i&“:B”&i)。复制目标:=sheet2.范围(“A”&lastRowSheet2+1)
如果结束
接下来我
端接头

如果B不是空字符串,这将把A列和B列的所有行从Sheet1复制到Sheet2。并且还会添加标题“Name”和“Hours”

Option Explicit'要求在使用前必须定义每个变量,例如使用Dim语句。
次级DoStuff_良好实践()
Dim lastRowSrc As Long,lastRowDest As Long,i As Long'声明行计数为Long,以便可以使用所有行
将SHT源设置为工作表,将SHT目标设置为工作表
设置shtSource=ThisWorkbook.Worksheets(“Sheet1”)“工作表的完整合格标识
Set shtDestination=ThisWorkbook.Sheets(“Sheet2”)
lastRowSrc=shtSource.Range(“A”&shtSource.Rows.Count).End(xlUp).Row”确定最后使用的行
'清除目标工作表并写入标题:
shtdestimation.Cells.Clear
shtdestimation.Range(“A1”).Value=“Name”
shtDestination.Range(“B1”).Value=“小时”
lastRowDest=1'以第1行作为目标开始
对于i=1到lastRowSrc'循环所有使用的行
如果shtSource.Range(“A”&i).值为vbNullString,则_
shtSource.Range(“B”&i).Value vbNullString然后检查单元格是否不是空字符串
shtSource.Range(“A”&i&“:B”&i).复制目标:=shtDestination.Range(“A”&lastRowDest+1)”复制当前行
lastRowDest=lastRowDest+1'跳转到目标中最后使用的行
如果结束
接下来我
端接头

lastRow
lastRowSheet2
i
应为
Long
类型。Excel的行数超过了
Integer
所能处理的行数。为了安全性和完整性,可能是的,而不仅仅是可能,它确实应该是
长的
。因为它肯定会在大于
整数的行数上崩溃,这是一个严重的问题,不仅仅是“很好地拥有这个特性”。不使用
Long
进行行计数是非常糟糕的做法。即使您删除了您的注释,我也会解释这一点:VBA中的INTEGER是max
32767
,Excel行最多为
1048576
。这意味着您的sub正在为第一个
32767
工作,并且在
1015809
行中失败(因此我们可以同意,此sub在更多情况下失败,而不是正常工作)。这与抱怨无关。整数是行计数的错误类型。如果将这些定义更改为
,只要
,不是会简单得多吗?不要把这些话当作是针对个人的,它只是关于良好的实践。除非有特殊原因,否则默认情况下始终使用
Long
。)到目前为止你试过什么?分享您的代码尝试,我们很乐意帮助您
Option Explicit 'requires that every variable has to be defined before use, e.g. with a Dim statement. 

Sub DoStuff_GoodPractice()
    Dim lastRowSrc As Long, lastRowDest As Long, i As Long 'declare row counts as Long so all rows can be used
    Dim shtSource As Worksheet, shtDestination As Worksheet

    Set shtSource = ThisWorkbook.Worksheets("Sheet1")  'full qualified identification of the worksheets
    Set shtDestination = ThisWorkbook.Sheets("Sheet2")

    lastRowSrc = shtSource.Range("A" & shtSource.Rows.Count).End(xlUp).Row 'determine the last used row

    'clear destination sheet and write headers:
    shtDestination.Cells.Clear
    shtDestination.Range("A1").Value = "Name"
    shtDestination.Range("B1").Value = "Hours"

    lastRowDest = 1   'start with row 1 as destination

    For i = 1 To lastRowSrc 'loop through all used rows
        If shtSource.Range("A" & i).Value <> vbNullString And _
           shtSource.Range("B" & i).Value <> vbNullString Then 'check if cells are not a null string
            shtSource.Range("A" & i & ":B" & i).Copy Destination:=shtDestination.Range("A" & lastRowDest + 1) 'copy current row
            lastRowDest = lastRowDest + 1 'jump to the last used row in destination
        End If
    Next i
End Sub