简体   繁体   English

Excel VBA中的循环公式

[英]Loop formula in excel vba

I am trying to loop a formula instead of using filldown because i have two formulas depending on the length of text in Column A's Cell. 我试图循环一个公式而不是使用filldown因为我有两个公式,具体取决于列A单元格中文本的长度。

my two formulas are like 我的两个公式就像

.Range("C2").Formula = "=CHAR(83)&CHAR(45)&LEFT(A2,LEN(A2)-9)&CHAR(45)&UPPER(MID(A2,4,LEN(A2)-8))&CHAR(32)&D2&E2"

.Range("C2").Formula = "=LEFT(A2,LEN(A2)-9)&CHAR(45)&UPPER(MID(A2,8,LEN(A2)-12))&CHAR(32)&D2&E2"

This is what i have so far. 这是我到目前为止所拥有的。

Sub AddFormulas()
Application.ScreenUpdating = False

Dim aCell As String
Dim cCell As String
Dim dCell As String
Dim eCell As String
Dim rowStart As Integer
Dim aCol As Integer
Dim cCol As Integer
Dim dCol As Integer
Dim eCol As Integer
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row

rowStart = 2
aCol = 1
cCol = 3
dCol = 4
eCol = 5
'Range("A2")
aCell = Cells(rowStart, aCol).Address(RowAbsolute:=False, _
                                ColumnAbsolute:=False)
'Range("C2")
aCell = Cells(rowStart, cCol).Address(RowAbsolute:=False, _
                                ColumnAbsolute:=False)
'Range("D2")
dCell = Cells(rowStart, dCol).Address(RowAbsolute:=False, _
                                ColumnAbsolute:=False)
'Range("E2")
eCell = Cells(rowStart, eCol).Address(RowAbsolute:=False, _
                                ColumnAbsolute:=False)

With ThisWorkbook.Sheets("Sheet1")
        .Range(cCell).Formula = "=CHAR(83)&CHAR(45)&LEFT(aCell,LEN(aCell)-9)&CHAR(45)&UPPER(MID(aCell,4,LEN(aCell)-8))&CHAR(32)&(dCell)&(eCell)"
End With
Application.ScreenUpdating = True
End Sub

Thx 谢谢

You haven't explained the number of chars to determine which formula to use, you can use an IF statement to determine the char length of a cell using LEN Then you can post your formula with just one line of code to the entire range as Excel is smart enough to increment by row. 您尚未解释确定要使用哪个公式的字符数,可以使用IF语句使用LEN确定单元格的字符长度,然后可以将只用一行代码的公式作为Excel发布到整个范围中足够聪明,可以逐行递增。 Here is an example: 这是一个例子:

Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=IF(LEN(A1)=2,123,987)"

This will put 123 in column B where the corresponding row in column A has a length of 2 characters otherwise it will enter 987. 这会将123放入B列,其中A列中的相应行的长度为2个字符,否则将输入987。

Change the LEN(A1)=2 to the length to trigger the first formula, change 123 to the first formula and change 987 to the second formula you want. LEN(A1)=2更改为长度以触发第一个公式,将123更改为第一个公式,将987更改为所需的第二个公式。 If you are testing any cell other than column A, also change that in the LEN part of the formula. 如果要测试除A列以外的任何其他单元格,请同时在公式的LEN部分中进行更改。

Lastly, why on earth are you making this so complex? 最后,为什么在地球上如此复杂? Why do CHAR(83)&CHAR(45) instead of "S-" its going to be harder to maintain if you put everything in by it's ASCII code. 如果将所有内容都用ASCII代码放入,为什么用CHAR(83)&CHAR(45)而不是"S-"来维护就更难了。

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

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