简体   繁体   English

在 VBA 复制粘贴宏中使用 UDF

[英]Using a UDF in a VBA copy paste macro

In a workbook of mine there are 2 worksheets (Sheet1 & Sheet2) and Sheet1 has some data like在我的工作簿中,有 2 个工作表(Sheet1 和 Sheet2),Sheet1 有一些数据,例如

s1

I'm trying to copy the data from columns SERIAL NO., HS CODE and PALLET MMT to Sheet2's columns PROD.我正在尝试将 SERIAL NO.、HS CODE 和 PALLET MMT 列中的数据复制到 Sheet2 的 PROD 列中。 ID, HS CODE & NET WT. ID、HS 代码和净重。 respectively.分别。 Now the first two copies are pretty straight forward but the problem I'm having is generating NET WT.现在前两个副本非常简单,但我遇到的问题是生成 NET WT。 (it is the product of two numbers inside the brackets & divided by 1000) (括号内的两个数除以1000的乘积)

Default Sheet2 looks like:默认 Sheet2 看起来像:

s2

Result Sheet2 data should look like:结果 Sheet2 数据应如下所示:

s2Result

I've done:我弄完了:

Sub Macro1()
'
' Macro1 Macro
'

'
    Cells.Find(What:="SERIAL", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    ActiveCell.Offset(1, 0).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Sheet2").Select
    Cells.Find(What:="PROD", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    Range("B4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Sheets("Sheet1").Select
    Range("A1").Select
    Cells.Find(What:="CODE", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    ActiveCell.Offset(1, 0).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("C4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
' @@@@@@@@@@ NET WT. ?????? @@@@@@@@@@@@@
End Sub

I've also made a function to calculate NET WT.我还做了一个函数来计算 NET WT。 & but struggling to figure out how to use it in my code without making it too complicated & 但努力弄清楚如何在我的代码中使用它而不让它太复杂

Function netWT(CellRef As String)
    Dim i As Long, Result As String, ch As String
    For i = 1 To Len(CellRef)
        ch = Mid(CellRef, i, 1)
        Result = Result & IIf(ch Like "[0-9]", ch, " ")
    Next i
    Result = Application.Trim(Result)
    netWT = (Split(Result, " ")(1) * Split(Result, " ")(2)) / 1000
End Function

Help please.请帮忙。 Different approach than mine are also welcome...It just has to do the job efficiently.也欢迎与我不同的方法......它只需要有效地完成工作。

This should do it...adjust ranges as required.这应该可以...根据需要调整范围。

Sub PopulateNetWeightColumn()
    Sheets("Sheet2").Activate
    Range("C4").Select
    While Range("C" & ActiveCell.Row) <> ""
        Range("E" & ActiveCell.Row) = CalculateNetWeight(WorksheetFunction.VLookup(Range("C" & ActiveCell.Row), Sheets("Sheet1").Range("B3:G12"), 4, False))
        ActiveCell.Offset(1, 0).Select
    Wend
End Sub
Function CalculateNetWeight(palletString)
    Dim mult_1, mult_2
    palletString = Mid(palletString, InStr(palletString, "(") + 1, 100)
    palletString = Trim(Replace(Replace(Replace(palletString, " ml", ""), " gm", ""), ")", ""))
    mult_1 = CLng(Left(palletString, InStr(palletString, "x") - 1))
    mult_2 = CLng(Replace(palletString, mult_1 & "x", ""))
    CalculateNetWeight = (mult_1 * mult_2) / 1000
End Function

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

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