简体   繁体   English

将Excel公式转换为VBA宏

[英]Convert Excel formula into VBA Macro

I have the following formula in an excel worksheet that I want to make a Macro: 我要制作宏的excel工作表中有以下公式:

IF(OR(AA2=2,AA2=3,AA2=4),"00",IF(AA2=5,"0"&LEFT(Z2,1),IF(AA2=6,LEFT(Z2,2))))

I want to establish this formula for a certain range based on another column. 我想根据另一列为某个范围建立此公式。 I have multiple formulas written already that work to do this such as: 我已经写了多个公式来做到这一点,例如:

Range("B3:B" & Cells(Rows.Count, "M").End(xlUp).Row).Value = "=B2+1"

Basically I want to make the If/Or statement above work in VBA with the desired range. 基本上我想使上述If / Or语句在VBA中以所需的范围工作。

Any help would be appreciated! 任何帮助,将不胜感激!

You will have a dynamic range from Excel when the formula is entered via VBA as such: 当通过VBA这样输入公式时,您将在Excel中获得一个动态范围:

Range( Cells(2,"AB"), Cells (colMVal, "AB")).Formula = "=IF(OR(AA2=2,AA2=3,AA2=4),""00"",IF(AA2=5,""0""&LEFT(Z2,1),IF(AA2=6,LEFT(Z2,2))))"

Note that the formula is entered into row 2 all the way down to the column M value to dictate the final row (colMVal). 请注意,该公式一直输入到第2行,一直到M列的值以指示最后一行(colMVal)。 Also note the double quotes WITHIN the formula. 还要注意公式中的双引号。

If anything is required to be FIXED, rather than dynamic, you would use "$", such that: 如果需要固定而不是动态的任何内容,则可以使用“ $”,例如:

Range( Cells(2,"AB"), Cells (colMVal, "AB")).Formula = "=IF(OR(AA$2=2,AA$2=3,AA$2=4),""00"",IF(AA$2=5,""0""&LEFT(Z$2,1),IF(AA$2=6,LEFT(Z$2,2))))"

Where I have locked that ALL references verify that the row is 2, hence AA$2. 在我锁定所有引用的位置,请验证该行是否为2,即AA $ 2。 As Excel fills the formula into each row of the desired range, it will dynamically assign the correct row. 当Excel将公式填充到所需范围的每一行时,它将动态分配正确的行。

Just setup your function, turn on the Macro Recorder, click on the cell that contains your function, hit F2 and hit Enter. 只需设置功能,打开宏记录器,单击包含您的功能的单元格,按F2并按Enter。 If you want to setup dynamic start and end rows, or columns, you can use the methodologies below. 如果要设置动态的开始和结束行或列,可以使用下面的方法。

Sub DynamicRange()
'Best used when only your target data is on the worksheet

'Refresh UsedRange (get rid of "Ghost" cells)
  Worksheets("Sheet1").UsedRange

'Select UsedRange
  Worksheets("Sheet1").UsedRange.Select

End Sub

OR 要么

Sub DynamicRange()
'Best used when first column has value on last row and first row has a value in the last column

Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range

Set sht = Worksheets("Sheet1")
Set StartCell = Range("D9")

'Find Last Row and Column
  LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
  LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column

'Select Range
  sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select

End Sub

OR 要么

Sub DynamicRange()
'Best used when you want to include all data stored on the spreadsheet

Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range

Set sht = Worksheets("Sheet1")
Set StartCell = Range("D9")

'Refresh UsedRange
  Worksheets("Sheet1").UsedRange

'Find Last Row and Column
  LastRow = StartCell.SpecialCells(xlCellTypeLastCell).Row
  LastColumn = StartCell.SpecialCells(xlCellTypeLastCell).Column

'Select Range
  sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select

End Sub

OR 要么

Sub DynamicRange()
'Best used when your data does not have any entirely blank rows or columns

Dim sht As Worksheet
Dim StartCell As Range

Set sht = Worksheets("Sheet1")
Set StartCell = Range("D9")

'Select Range
  StartCell.CurrentRegion.Select

End Sub

OR 要么

Sub DynamicRange()
'Best used when column length is static

Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range

Set sht = Worksheets("Sheet1")
Set StartCell = Range("D9")

'Refresh UsedRange
  Worksheets("Sheet1").UsedRange

'Find Last Row
  LastRow = sht.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

'Select Range
  sht.Range("D9:M" & LastRow).Select

End Sub

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

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