VBA Excel添加增量文本,也会排除空白

时间:2016-07-15 09:12:27

标签: excel vba excel-vba macros

我正在处理用户交互式工作表,该工作表将在外部数据库文件中查找值,并根据ID号将这些值发布到用户工作表中,例如材料编号。

在这种情况下,我还想为工作表中的每种材料添加“项目编号”,但不使用预定义的Excel表格。我最好只粘贴文本和值,而不是公式,但最后,无论做什么工作。我已经尝试了两个版本,但是一个不会跳过空白,另一个不会粘贴公式,因为它给我一个“对象”错误,我似乎无法确定为什么它不接受这个命令。我猜.Formula有不同的要求。 (如果不是fndEntry什么都没有......之后的第一行)

以下是工作表的完整代码,包括与该问题无关的所有其他陈述。

我知道范围似乎是倒退的,但我似乎无法通过逻辑计算得到优秀,因此我的行计数是倒退的。 (在任何人开始评论之前):)如果你能解决为什么会这样,我更愿意听到原因。

Private Sub Worksheet_Change(ByVal Target As Range)

Dim material As String
Dim fndEntry As Range
Dim wb1 As Workbook, wb2 As Workbook
Dim lr As Integer

Application.ScreenUpdating = False
Application.DisplayAlerts = False

If Not Target.Column = 3 Then
    Exit Sub
End If

Set wb1 = ActiveWorkbook

lr = wb1.Sheets("Sagsnr.").Range("C1000:C" & Rows.Count).End(xlUp).Row

If lr < 20 Then
    Exit Sub
End If

Workbooks.Open Filename:="G:\Backoffice\Tilbudsteam\Kostdatabase\Matcost.xls", ReadOnly:=True

    Set wb2 = ActiveWorkbook

For i = 20 To lr

material = wb1.Sheets("Sagsnr.").Range("C" & i).Value

Set fndEntry = wb2.Sheets("Matcost").Range("C:D").Find(What:=material)

If Not fndEntry Is Nothing Then

    'wb1.Sheets("Sagsnr.").Range("A" & i).Formula = "=IF(C2="""","""",CONCATENATE(Pos.;COUNTA($C$20:C20)))"
    wb1.Sheets("Sagsnr.").Range("B" & i).Value = wb2.Sheets("Matcost").Range("H" & fndEntry.Row).Value  'Product group
    wb1.Sheets("Sagsnr.").Range("E" & i).Value = wb2.Sheets("Matcost").Range("Q" & fndEntry.Row).Value   'Available Stock
    wb1.Sheets("Sagsnr.").Range("F" & i).Value = wb2.Sheets("Matcost").Range("E" & fndEntry.Row).Value   'Materiale name
    wb1.Sheets("Sagsnr.").Range("G" & i).Value = wb2.Sheets("Matcost").Range("AJ" & fndEntry.Row).Value  'Marked for deletion
    wb1.Sheets("Sagsnr.").Range("I" & i).Value = wb2.Sheets("Matcost").Range("M" & fndEntry.Row).Value   'Datasheet
    wb1.Sheets("Sagsnr.").Range("K" & i).Value = wb2.Sheets("Matcost").Range("P" & fndEntry.Row).Value   'Lotsize
    wb1.Sheets("Sagsnr.").Range("M" & i).Value = wb2.Sheets("Matcost").Range("F" & fndEntry.Row).Value   'Material type (FERT/HAWA)
    wb1.Sheets("Sagsnr.").Range("N" & i).Value = wb2.Sheets("Matcost").Range("N" & fndEntry.Row).Value   'Date of Cost update
    wb1.Sheets("Sagsnr.").Range("O" & i).Value = wb2.Sheets("Matcost").Range("O" & fndEntry.Row).Value   'Last change of cost data
    wb1.Sheets("Sagsnr.").Range("P" & i).Value = wb2.Sheets("Matcost").Range("K" & fndEntry.Row).Value   'Stock category
    wb1.Sheets("Sagsnr.").Range("Q" & i).Value = wb2.Sheets("Matcost").Range("L" & fndEntry.Row).Value   'ABC code
    wb1.Sheets("Sagsnr.").Range("R" & i).Value = wb2.Sheets("Matcost").Range("V" & fndEntry.Row).Value   'Construction weight Cu
    wb1.Sheets("Sagsnr.").Range("S" & i).Value = wb2.Sheets("Matcost").Range("W" & fndEntry.Row).Value   'Construction weight Al
    wb1.Sheets("Sagsnr.").Range("T" & i).Value = wb2.Sheets("Matcost").Range("X" & fndEntry.Row).Value   'Sales weight Cu
    wb1.Sheets("Sagsnr.").Range("U" & i).Value = wb2.Sheets("Matcost").Range("Y" & fndEntry.Row).Value   'Sales weight Al
    wb1.Sheets("Sagsnr.").Range("AC" & i).Value = wb2.Sheets("Matcost").Range("Z" & fndEntry.Row).Value  'Construction weight PE
    wb1.Sheets("Sagsnr.").Range("AD" & i).Value = wb2.Sheets("Matcost").Range("AD" & fndEntry.Row).Value 'PE costs
    wb1.Sheets("Sagsnr.").Range("AE" & i).Value = wb2.Sheets("Matcost").Range("AA" & fndEntry.Row).Value 'Construction weight PVC
    wb1.Sheets("Sagsnr.").Range("AF" & i).Value = wb2.Sheets("Matcost").Range("AE" & fndEntry.Row).Value 'PVC costs
    wb1.Sheets("Sagsnr.").Range("AG" & i).Value = wb2.Sheets("Matcost").Range("AF" & fndEntry.Row).Value 'Other materials costs
    wb1.Sheets("Sagsnr.").Range("AH" & i).Value = wb2.Sheets("Matcost").Range("AB" & fndEntry.Row).Value 'Variable production costs
    wb1.Sheets("Sagsnr.").Range("AI" & i).Value = wb2.Sheets("Matcost").Range("AC" & fndEntry.Row).Value 'Fixed production costs

    End If
Next i

wb2.Close
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

以下是上述代码主要关注的一行。如上所述,我宁愿让VBA粘贴最终值“Pos。+ number”而不是公式,但无论如何都可以。

wb1.Sheets(“Sagsnr。”)。范围(“A”&amp; i).Formula =“= IF(C2 =”“”,“”“”,CONCATENATE(Pos .; COUNTA($ C) $ 20:C20)))“

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

开始,替换你的:

lr = wb1.Sheets("Sagsnr.").Range("C1000:C" & Rows.Count).End(xlUp).Row

lr = wb1.Sheets("Sagsnr.").Cells(Rows.count, "C").End(xlUp).row

还有:

material = wb1.Sheets("Sagsnr.").Range("C" & i).Value

为:

material = wb1.Sheets("Sagsnr.").Range("C:" & i).Value

答案 1 :(得分:0)

这是一个宏,它将根据您上面的Excel公式为A列填充值。你会注意到

  • 您必须更改工作簿名称以匹配您实际使用的任何名称。
  • 使用变体数组完成“工作”。这将比多次访问工作表运行得快得多。查看Variant Arrays and Worksheet Ranges上的Chip Pearsons网页,了解有关此方法的更详细讨论。但速度差异通常是10倍。
Option Explicit
Sub Totals()
    Dim vA As Variant, vC As Variant
    Dim WS As Worksheet
    Dim I As Long, J As Long
    Const sPOS As String = "Pos. "

'You will need to change the workbook name according to your system
Const WBName As String = "Stackoverflow dummy (1).xlsm"

Set WS = Workbooks(WBName).Worksheets("Sagsnr.")

With WS
    vC = .Range(.Cells(20, 3), .Cells(.Rows.Count, 3).End(xlUp))
End With

ReDim vA(1 To UBound(vC, 1), 1 To UBound(vC, 2))

J = 0
For I = 1 To UBound(vC)
    'Given that there are non-codes on the sheet in column C, you might need to check that the entry in
    '  Column C is an EAN Code rather than a null string.
    If vC(I, 1) <> "" Then
        J = J + 1
        vA(I, 1) = sPOS & J
    End If
Next I

With WS
    With .Range(.Cells(20, 1), .Cells(19 + UBound(vA, 1), 1))
        .Value = vA
        ' can add formatting properties if necessary)
    End With
End With

End Sub