将Countif和xlookup公式编写到VBA宏中。

huangapple go评论93阅读模式
英文:

Writing Countif and xlookup formulas into VBA Macro

问题

本周我开始学习使用VBA宏,所以如果我术语用词不准确,我提前道歉。

我在Excel中录制了我的宏,它“运行”,但运行时间很长,偶尔会崩溃。我认为问题可能是宏中有一些IfCountifs和Xlookup公式。

是否有更有效的方法来编写以下公式:

Dim lr As Long
lr = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row

Range("H7").Select
ActiveCell.FormulaR1C1 = _
    "=IF(COUNTIFS('Sheet1'!C[-7],Sheet2!RC[-7],'Sheet1'!C[1],""*Text*""), ""Text"", ""Text"")"
Range("H7").Select
Selection.AutoFill Destination:=Range("H7:H" & lr)

Range("K7").Select
ActiveCell.Formula2R1C1 = _
    "=XLOOKUP(1,('Sheet1'!C[-10]=Sheet2!RC[-10])*('Sheet1'!C[-1]=""Text""), 'Sheet1'!C[1], """")"
Range("K7").AutoFill Destination:=Range("K7:K" & lr)

在Excel中,公式看起来是这样的:

=IF(COUNTIFS('Sheet1'!A:A,Data!A7,'Sheet1'!I:I,"*Text*"), "Text", "Text")
=XLOOKUP(1,('Sheet1'!A:A=Sheet2!A7)*('Sheet1'!J:J="Text"), 'Sheet1'!L:L, "")

这个宏“运行”,但运行速度很慢,偶尔会崩溃。

英文:

This week I have started learning to use VBA macros so I apologies in advance if I get the terminology wrong.

I have recorded my macro in Excel and it "works' however it takes a long time to run and on occasion crash. I think the issue might be that I have a number of IfCountifs and Xlookup formulas in the macro.

Is there a more efficient way to write the following formulas:

    Dim lr As Long
    lr = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
    
        Range("H7").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(COUNTIFS('Sheet1'!C[-7],Sheet2!RC[-7],'Sheet1!C[1],""*Text*""), ""Text"", ""Text"")"
        Range("H7").Select
        Selection.AutoFill Destination:=Range("H7:H" & lr)
    
        Range("K7").Select
        ActiveCell.Formula2R1C1 = _
            "=XLOOKUP(1,('Sheet1'!C[-10]=Sheet2!RC[-10])*('Sheet1!C[-1]=""Text""), 'Sheet1'!C[1], """")"
        Range("K7").AutoFill Destination:=Range("K7:K" & lr)

In Excel the formulas look like this:

    =IF(COUNTIFS('Sheet1'!A:A,Data!A7,'Sheet1'!I:I,"*Text*"), "Text", "Text")
    =XLOOKUP(1,('Sheet1'!A:A=Sheet2!A7)*('Sheet1'!J:J="Text"), 'Sheet1'!L:L, "")

The macro 'works' but it is really slow and occasionally crashes.

答案1

得分: 1

以下是翻译好的部分:

Sub WriteFormulas()
    
    Const SRC_SHEET As String = "Sheet1"
    Const SRC_FIRST_ROW As Long = 2 ' adjust!!!
    
    Const DST_SHEET As String = "Sheet2"
    Const DST_FIRST_ROW As Long = 7
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Source
    
    Dim srg As Range, sLastRow As Long
    
    With wb.Sheets(SRC_SHEET)
        sLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set srg = .Range(.Cells(SRC_FIRST_ROW, "A"), .Cells(sLastRow, "A"))
    End With
    
    ' Sheet
    Dim sSheet As String: sSheet = "'" & SRC_SHEET & "'!"
    ' Lookup
    Dim SL1 As String: SL1 = sSheet & srg.Address
    Dim SL2 As String: SL2 = sSheet & srg.EntireRow.Columns("I").Address
    Dim SL3 As String: SL3 = sSheet & srg.EntireRow.Columns("J").Address
    ' Return
    Dim SR3 As String: SR3 = sSheet & srg.EntireRow.Columns("L").Address
    
    ' Destination
    
    Dim drg As Range, dLastRow As Long
    
    With wb.Sheets(DST_SHEET)
        dLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set drg = .Range(.Cells(DST_FIRST_ROW, "A"), .Cells(dLastRow, "A"))
    End With
    
    ' lookup
    Dim DL1 As String: DL1 = drg.Cells(1).Address(0, 0)
    
    ' Formulas
    
    ' =IF(COUNTIFS('Sheet1'!A:A,A7,'Sheet1'!I:I,"*Text*"),"Yes", "No")
    With drg.Columns("H")
        .Formula = "=IF(COUNTIFS(" & SL1 & "," & DL1 & "," & SL2 _
            & ",""*Text*""),""Yes"",""No"")"
    End With
    
    ' =XLOOKUP(1,(Sheet1!A:A=A7)*(Sheet1!J:J="Text"),Sheet1!L:L,"")
    With drg.Columns("K")
        .Formula2 = "=XLOOKUP(1,(" & SL1 & "=" & DL1 & ")*(" _
        & SL3 & "=""""Text"""")," & SR3 & ","""""")"
    End With
    
    ' Inform.
    
    MsgBox "Formulas written.", vbInformation

End Sub

请注意,这些是VBA代码的翻译部分,不包括代码注释。

英文:

Write Formulas Using VBA

将Countif和xlookup公式编写到VBA宏中。

  • You should avoid using entire columns to look up data.
  • You should consider putting the data into Excel (structured) tables. Then formulas with structured references would ensure it to be dynamic and all this VBA business wouldn't be necessary.

<!-- language: lang-vb -->

Sub WriteFormulas()
Const SRC_SHEET As String = &quot;Sheet1&quot;
Const SRC_FIRST_ROW As Long = 2 &#39; adjust!!!
Const DST_SHEET As String = &quot;Sheet2&quot;
Const DST_FIRST_ROW As Long = 7
Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code
&#39; Source
Dim srg As Range, sLastRow As Long
With wb.Sheets(SRC_SHEET)
sLastRow = .Cells(.Rows.Count, &quot;A&quot;).End(xlUp).Row
Set srg = .Range(.Cells(SRC_FIRST_ROW, &quot;A&quot;), .Cells(sLastRow, &quot;A&quot;))
End With
&#39; Sheet
Dim sSheet As String: sSheet = &quot;&#39;&quot; &amp; SRC_SHEET &amp; &quot;&#39;!&quot;
&#39; Lookup
Dim SL1 As String: SL1 = sSheet &amp; srg.Address
Dim SL2 As String: SL2 = sSheet &amp; srg.EntireRow.Columns(&quot;I&quot;).Address
Dim SL3 As String: SL3 = sSheet &amp; srg.EntireRow.Columns(&quot;J&quot;).Address
&#39; Return
Dim SR3 As String: SR3 = sSheet &amp; srg.EntireRow.Columns(&quot;L&quot;).Address
&#39; Destination
Dim drg As Range, dLastRow As Long
With wb.Sheets(DST_SHEET)
dLastRow = .Cells(.Rows.Count, &quot;A&quot;).End(xlUp).Row
Set drg = .Range(.Cells(DST_FIRST_ROW, &quot;A&quot;), .Cells(dLastRow, &quot;A&quot;))
End With
&#39; lookup
Dim DL1 As String: DL1 = drg.Cells(1).Address(0, 0)
&#39; Formulas
&#39; =IF(COUNTIFS(&#39;Sheet1&#39;!A:A,A7,&#39;Sheet1&#39;!I:I,&quot;*Text*&quot;),&quot;Yes&quot;, &quot;No&quot;)
With drg.Columns(&quot;H&quot;)
.Formula = &quot;=IF(COUNTIFS(&quot; &amp; SL1 &amp; &quot;,&quot; &amp; DL1 &amp; &quot;,&quot; &amp; SL2 _
&amp; &quot;,&quot;&quot;*Text*&quot;&quot;),&quot;&quot;Yes&quot;&quot;,&quot;&quot;No&quot;&quot;)&quot;
End With
&#39; =XLOOKUP(1,(Sheet1!A:A=A7)*(Sheet1!J:J=&quot;Text&quot;),Sheet1!L:L,&quot;&quot;)
With drg.Columns(&quot;K&quot;)
.Formula2 = &quot;=XLOOKUP(1,(&quot; &amp; SL1 &amp; &quot;=&quot; &amp; DL1 &amp; &quot;)*(&quot; _
&amp; SL3 &amp; &quot;=&quot;&quot;Text&quot;&quot;),&quot; &amp; SR3 &amp; &quot;,&quot;&quot;&quot;&quot;)&quot;
End With
&#39; Inform.
MsgBox &quot;Formulas written.&quot;, vbInformation
End Sub

huangapple
  • 本文由 发表于 2023年8月5日 07:08:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定