简体   繁体   English

VBA中的动态范围

[英]Dynamic Range in VBA

I am having hard times figuring out how to pass a dynamic range into the formula. 我很难弄清楚如何将动态范围传递到公式中。

Usually I will have a scenario where I know in what column my range is going to be (example below), where LR is number of rows in my range. 通常情况下,我会知道我的范围将在哪一列中(以下示例),其中LR是我的范围中的行数。

Range("A1:A" & LR).FormulaR1C1 = ....some formula here

Problem begins when I have to create a range dynamically using (in my case) column header name. 当我必须使用(在我的情况下)列标题名称动态创建范围时,问题就开始了。 I can find the column by Header Name, get column number, convert it into letter, but any of the solutions I've tried didn't work. 我可以按标题名称查找列,获取列号,将其转换为字母,但是我尝试过的任何解决方案均无效。

This is what I use to get column number: 这是我用来获取列号的方法:

Function getColumn(searchText As String, wsname As String) As Integer
    Set aCell = Sheets(wsname).Rows(1).Find(what:=searchText, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If aCell Is Nothing Then
            MsgBox ("Column was not found. Check spelling")
            Exit Function
        Else
            getColumn = aCell.Column
        End If
End Function

This code I use to convert it into the letter: 我使用此代码将其转换为字母:

Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function

Now, to populate the formula in the range I tried something like that, but unfortunately no luck: 现在,要填充该范围内的公式,我尝试了类似的方法,但不幸的是没有运气:

colLetter = Col_Letter(manuallyAdjustedNumber)
Range(colLetter & "2:" & colLetter & LR).FormulaR1C1 = "=EXACT([RC-1],[" & harnessDrawingNumber & " - " & manuallyAdjustedNumber & "])"

I will apreciate any help! 我将不胜感激! Thank you! 谢谢!

Forget changing to a letter and just use Cells 忘记换字母了,只使用Cells

 With Worksheets("Sheet1")
    .Range(.Cells(2,manuallyAdjustedNumber),.Cells(LR,manuallyAdjustedNumber)).FormulaR1C1 = ...
End With

You are making this way too complicated. 您使这种方式过于复杂。 Why you are using R1C1 notation and at the same time converting everything back and forth from column letters to column numbers is completely beyond me. 为什么要使用R1C1表示法,同时又将所有内容从列字母来回转换为列号,这完全超出了我的范围。

You have 2 problems: 您有2个问题:

  • You are always working with the ActiveWorkbook (the example below makes that explicit). 您始终在使用ActiveWorkbook(以下示例对此进行了明确说明)。
  • You need to makes sure that the Worksheet is found. 您需要确保找到工作表

It should look something more like this: 它看起来应该像这样:

Public Function GetColumn(searchText As String, wsname As String) As Integer
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            If LCase$(.Name) = LCase$(wsname) Then
                Dim header As Range
                Set header = .Rows(1).Find(searchText, , xlValues, xlWhole, xlNext, False)
                If Not header Is Nothing Then
                    GetColumn = header.Column
                    Exit Function
                End If
                MsgBox ("Column was not found. Check spelling")
                Exit Function
            End If
        End With
    Next
    MsgBox "Worksheet '" & wsname & "' not found."
End Function

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

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