Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
VBA匹配并更新现有字段,并将新字段添加到下一行的下一个空单元格中_Vba_Excel_Match_Vlookup - Fatal编程技术网

VBA匹配并更新现有字段,并将新字段添加到下一行的下一个空单元格中

VBA匹配并更新现有字段,并将新字段添加到下一行的下一个空单元格中,vba,excel,match,vlookup,Vba,Excel,Match,Vlookup,我是新来的,我正在尝试做一个VBA Vlookup函数 我的目标是使用A列将表1从第1页拖到第2页的表2,如果A列存在,则更新B列和C列 如果A不存在,则添加到表1中的下一个空行中还包括B列和C列 请参考下图-更新结果的表1预期 先谢谢你 当前只能编码以更新现有字段,但不确定如何将不匹配的字段添加到Sheet1的下一个空行中 Sub getOpenExcel() ' Your daily report has a date in it's name ' to sele

我是新来的,我正在尝试做一个VBA Vlookup函数

我的目标是使用A列将表1从第1页拖到第2页的表2,如果A列存在,则更新B列和C列

如果A不存在,则添加到表1中的下一个空行中还包括B列和C列

请参考下图-更新结果的表1预期

先谢谢你

当前只能编码以更新现有字段,但不确定如何将不匹配的字段添加到Sheet1的下一个空行中

Sub getOpenExcel()

    '   Your daily report has a date in it's name
    '   to select an open workbook we must first know it's name
    '   AND - it must be already open
    '   Your examples are 2017-03-11-18875, 2017-03-12-18875, 2017-03-13-18875

    '   If the name is the current date then this would work to get the filename

    Dim fileName As String, monthNum As String, dayNum As String, wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet, rng1 As Range, rng2 As Range

    '   this adds a ZERO to the front of month numbers less than 10
    If Month(Date) < 10 Then
        monthNum = "0" & CStr(Month(Date))
    Else
        monthNum = CStr(Month(Date))
    End If


    '   You may or may not need this section
    '   it adds a ZERO to the front of day numbers less than 10
    If Day(Date) < 10 Then
        dayNum = "0" & CStr(Day(Date))
    Else
        dayNum = CStr(Day(Date))
    End If
    '   many cases the daily report will come from the previous day
    '   If your file has yesterday's date, then comment out the above code and
    'uncomment the following code
    '
    'If Day(DateAdd("d", -1, Date)) < 10 Then
    '    dayNum = "0" & Day(DateAdd("d", -1, Date))
    'Else
    '    dayNum = Day(DateAdd("d", -1, Date))
    'End If


    fileName = "GREENBILL_RECON_DETAILED_REPORT_" & CStr(Year(Date)) & monthNum & dayNum
    '   if today's date is 3/14/17 then "fileNem" = "2017-03-12-18875"

    '   If your daily report is an excel book, then we need to add the proper extension.
    '   It could be one of many, "xls", ".xlsx" , ".xlsm", etc....
    '   If your daily report is open - look at the top.  It should have the file name and extension.'
    '   Replace the below extension with the correct one.
    fileName = fileName & ".csv"
    '   Again, if today's date is 3/14/17 then "fileNem" =  "2017-03-12-18875.xlsx"


    '   This is where we set both workbooks to variables
    '
    Set wb1 = ThisWorkbook
    Set ws1 = wb1.Worksheets("Sheet1")

    On Error GoTo notOpen
    Set wb2 = Workbooks(fileName)                ' This is your daily report
    On Error GoTo 0
    Set ws2 = wb2.Worksheets("GREENBILL_RECON_DETAILED_REPORT")
    ws1.Activate
    '*************************************************************************************
    ' If successful this is the area where you put your code to copy and paste automatically '
    ' If you need this pasted to the first empty row at bottom of page then 'put code here to find the first empty row and use that varaible
    ' with range("a" & firstUnusedRow) intstead of A1 ...

    wb2.Activate
    Range("A1:Z500").Copy _
          Destination:=wb1.Worksheets("Sheet1").Range("A1") 'change A1 to A &
    firstUnusedRow
    '*************************************************************************************
    ' This is the clean up and exit code

    Set wb1 = Nothing
    Set wb2 = Nothing
    Exit Sub
notOpen:
    On Error GoTo 0
    Set wb1 = Nothing
    MsgBox "The file " & fileName & " is not open"
    Exit Sub

End Sub

Sub Rectangle3_Click()

    On Error Resume Next

    Dim Dept_Row As Long                         ' To Change to Billing_Acc
    Dim Dept_Clm As Long                         ' To Change to Org_Seqno

    Table1 = Sheet1.Range("A1:A10")              ' Input file name
    Table2 = Sheet2.Range("A1:B10")              ' Range of table

    Dept_Row = Sheet1.Range("B1").Row
    Dept_Clm = Sheet1.Range("B1").Column

    For Each cl In Table1
        Sheet1.Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(cl, Table2, 2, False)
        Dept_Row = Dept_Row + 1
    Next cl
    MsgBox "Done"

End Sub
子getOpenExcel()
“您的每日报告的名称中有日期
'要选择打开的工作簿,必须首先知道其名称
“而且——它一定已经打开了
“您的示例是2017-03-11-188752017-03-12-188752017-03-13-18875
'如果名称是当前日期,则这将用于获取文件名
Dim文件名为字符串,monthNum为字符串,dayNum为字符串,wb1为工作簿,wb2为工作簿
将ws1标注为工作表,ws2标注为工作表,rng1标注为范围,rng2标注为范围
这会在小于10的月份数字前面加上一个零
如果月份(日期)小于10,则
monthNum=“0”和CStr(月(日))
其他的
月=CStr(月(日))
如果结束
'您可能需要也可能不需要此部分
“它在日数的前面加上一个小于10的零
如果日期小于10,则
dayNum=“0”和CStr(日期)
其他的
dayNum=CStr(日)
如果结束
“许多情况下,每日报告将来自前一天
'如果您的文件有昨天的日期,则注释掉上述代码并
'取消注释以下代码
'
'如果日期(DateAdd(“d”,-1,Date))<10,则
'dayNum=“0”&Day(日期添加(“d”,-1,日期))
”“否则呢
'dayNum=Day(DateAdd(“d”,-1,Date))
"完"
fileName=“绿单检查详细报告”&CStr(年(日))&monthNum&dayNum
'如果今天的日期是2017年3月14日,则“fileNem”=“2017-03-12-18875”
'如果您的每日报告是excel手册,那么我们需要添加适当的扩展名。
“它可能是许多“xls”、“.xlsx”、“.xlsm”等中的一个。。。。
“如果你的每日报告是开放的,请查看顶部。它应该有文件名和扩展名。”
'用正确的延长件更换下面的延长件。
fileName=fileName&“.csv”
'同样,如果今天的日期是2017年3月14日,则“fileNem”=“2017-03-12-18875.xlsx”
'这是我们将两个工作簿都设置为变量的地方
'
设置wb1=ThisWorkbook
设置ws1=wb1。工作表(“表1”)
关于错误转到notOpen
设置wb2=工作簿(文件名)'这是您的每日报告
错误转到0
设置ws2=wb2.工作表(“绿单检查详细报告”)
ws1.激活
'*************************************************************************************
'如果成功,这是您放置代码以自动复制和粘贴的区域'
“如果需要将其粘贴到页面底部的第一个空行,则”将代码放在此处以查找第一个空行并使用该变量
'的范围(“a”&firstUnusedRow)代替A1。。。
wb2.激活
范围(“A1:Z500”)。副本_
目的地:=wb1。工作表(“表1”)。范围(“A1”)将A1更改为A&
第一个未使用的行
'*************************************************************************************
'这是清理和退出代码
设置wb1=Nothing
设置wb2=无
出口接头
诺托潘:
错误转到0
设置wb1=Nothing
MsgBox“文件”&文件名&“未打开”
出口接头
端接头
子矩形3_单击()
出错时继续下一步
Dim部门行“长”更改为计费依据
Dim Dept_Clm As Long’更改为组织序号
Table1=Sheet1.Range(“A1:A10”)'输入文件名
表2=表2.范围(“A1:B10”)'表的范围
部门行=表1.范围(“B1”).行
部门Clm=表1.范围(“B1”)列
对于表1中的每个cl
表1.单元格(部门行,部门Clm)=应用程序.工作表功能.VLookup(cl,表2,2,假)
部门行=部门行+1
下一个cl
MsgBox“完成”
端接头

下面的代码是通过匹配两张表的A列来迭代所有行完成的。如果未找到,将在活页1中添加新行

Dim lngRow1, lngRow2 As Long
lngRow1 = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lngRow2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

Dim isFound As Boolean
Dim lastRow As Long
Dim i, j As Long

lastRow = lngRow1
For i = 1 To lngRow2
    isFound = False
    For j = 1 To lngRow1
        If Sheets("Sheet1").Range("A" & i) = Sheets("Sheet2").Range("A" & j) Then
            Sheets("Sheet1").Range("B" & i) = Sheets("Sheet2").Range("B" & j)
            Sheets("Sheet1").Range("C" & i) = Sheets("Sheet2").Range("C" & j)
            isFound = True
        End If
    Next j
    If Not isFound Then
        lastRow = lastRow + 1
        Sheets("Sheet1").Range("A" & lastRow) = Sheets("Sheet2").Range("A" & i)
        Sheets("Sheet1").Range("B" & lastRow) = Sheets("Sheet2").Range("B" & i)
        Sheets("Sheet1").Range("C" & lastRow) = Sheets("Sheet2").Range("C" & i)
    End If
Next i

考虑上述示例图像编写的代码。如果列数与示例不同,请相应修改代码

我不想说得粗鲁,但是。。。您刚刚描述了您需要做什么以及如何做,那么为什么不直接做呢…?嗨@Rawrplus,我需要每天更新列表,并且生成一个新的工作表,而不包含旧字段。因此我需要这个逻辑。我已经创建了用于更新B&C列的VBA,但我不确定如何将逻辑添加到VBA中,以便将A中找不到的字段添加到Sheet1的下一个空行中。谢谢。没关系,很漂亮。尽管如此,我的主要观点是——它在这里的工作方式是,到目前为止,你应该发布你当前的努力。理想情况下,这意味着你当前努力的代码。这个地方是专门帮助你编写代码的,而不是为你编写代码:)@Rawrplus,谢谢你给我解释。我将把代码复制过来。我仍然不清楚算法应该如何工作。我的意思是,我知道你基本上想把两张桌子合并成一个大桌子,但我不清楚这里的主要概念是什么。如果我理解正确,您首先循环表1,如果值在表2中,那么表2的值将被传递,然后您只添加两个表中未包含的额外值?请尝试用fashionOmg感谢Nagarajan这样的算法来描述您预期结果的计算。你是生命的救世主。谢谢你的帮助。可以设置val吗