简体   繁体   English

Excel vba 自动填充编码

[英]Excel vba autofill coding

I am currently using vba to perform the following tasks.我目前正在使用 vba 执行以下任务。 So I copied data from sheet1 and pasted it in sheet2.所以我从 sheet1 复制数据并将其粘贴到 sheet2 中。 There was already data in sheet2, so I pasted below the existing data. sheet2中已经有数据,所以我粘贴在现有数据下方。 Now I need to perform a vlookup to get data from sheet3 to sheet2 for certain columns, so the vlookup would send the data below the existing data on sheet2.现在我需要执行 vlookup 以将数据从 sheet3 获取到 sheet2 的某些列,因此 vlookup 会将数据发送到 sheet2 上现有数据下方的数据。 I have the following codes below我有以下代码

Range(“F1”).End(xldown).Offset(1).Select
ActiveCell.FormulaR1C1 = “=Vlookup(RC[7],Monitor_Report[#All],4,FALSE)”

ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & “:” & ActiveCell.End(xlDown).Offset(-1,0).Address) ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & “:” & ActiveCell.End(xlDown).Offset(-1,0).Address)

So currently even tho, the next row has no data, it still auto fills and results in #N/A after the cell where the vlookup occurs first.所以目前即使如此,下一行也没有数据,它仍然会自动填充并在首先发生 vlookup 的单元格之后导致 #N/A 。 i do not want the autofill to happen if the next row has no data.如果下一行没有数据,我不希望自动填充发生。 Can anyone assist me to modify this code?谁能帮我修改这段代码?

If you're set on using the manual way of .Select and .AutoFill , I'd just add an IFNA -statement to your formula:如果您打算使用.Select.AutoFill的手动方式,我只需在您的公式中添加一个IFNA语句:

Sub test()
  Dim ws As Worksheet
  Set ws = ThisWorkbook.Worksheets("Sheet1")

  ws.Range("F1").End(xlDown).Offset(1).Select
    
  Dim fRow As Long

  With ws
    fRow = .Cells(.Rows.Count, ws.ListObjects("Monitor_Report").DataBodyRange.Column).End(xlUp).Row
    fAdr = ActiveCell.Column
  End With

  adr_ = ws.Cells(fRow, fAdr).Address

  ActiveCell.Value = "=IFNA(VLOOKUP(M3,Monitor_Report[#All],4,FALSE),"""")" '
  ActiveCell.AutoFill Destination:=ws.Range(ActiveCell.Address & ":" & adr_)


End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM