Excel VBA使用公式中的单元格属性

时间:2017-12-19 14:32:24

标签: excel vba excel-formula cells

我创建了一个宏来查看单元格中的数字,然后复制一组三列并将它们插入到组的右侧。一切正常。

我在这些列组之后的单元格中有一个公式,用于查看单元格中是否有1。下面的代码就是假设我创建了两个组。

=IF(AND(H9=1,J9=1),1,0)

如果我创建了四个组,我希望能够自动添加M9 = 1,P = 1.

如果有人有时间提供帮助,我们将不胜感激。

抱歉,我继续学习。

我正在创建一个矩阵,我可以在列方向上构建许多函数,以及在行方向上影响函数的许多输入。 我从每个函数的三个列的“组”开始,在我的第一组G9是预期条件,H9是模拟期间的结果,I9是实际测试期间的结果。我希望能够说明将有多少函数和输入并自动创建矩阵。

如果我有两个函数,那么将有两组从G到L的列。

在所有函数之后,我检查它们是否全部通过,有两个函数,这个检查将在M9中,其中我有公式= IF(AND(H9 = 1,K9 = 1),1, 0)检查H9和K9中是否有1,然后在M9中放1。

如果我有四个函数,那么我需要在S1中检查公式= IF(AND(H9 = 1,K9 = 1,N = 1,Q = 1),1,0)

我想在循环中创建检查公式,以便添加正确的单元格进行检查。

希望这能更好地解释它,但可能不会!!

这是迄今为止的代码

Private Sub CommandButton1_Click()

' Copy the template worksheet
Worksheets("ZoneTemplate").Copy After:=Worksheets("ProjectConfig")

' Rename the worksheet to the correct Zone
Sheets("ZoneTemplate (2)").Name = Sheets("ProjectConfig").Range("B9")

' Setup the variables
Dim Loop1 As Integer
Dim MySheet As String
Dim NoOfOutputs As Integer
Dim NoOfColumnsOffset As Integer
Dim Loop2 As Integer

' Get the name of the sheet ready for use in the loop
MySheet = Sheets("ProjectConfig").Range("B9").Value  
' Get the number of outputs to add  
NoOfOutputs = Sheets("ProjectConfig").Range("E9") - 1  

' Loop for the number of safety output functions
For Loop1 = 1 To NoOfOutputs
    ' select the columns to copy and copy them to buffer
    Worksheets(MySheet).Range("G:I").Select    
    Selection.Copy
    ' Insert the copied columns infront of J1 and shift everything along to the right
    Worksheets(MySheet).Range("J1").Insert Shift:=xlShiftToRight        
Next Loop1

End Sub

1 个答案:

答案 0 :(得分:0)

所以这就行了......

Private Sub CommandButton1_Click()

' Copy the template worksheet
Worksheets("ZoneTemplate").Copy After:=Worksheets("ProjectConfig")

' Rename the worksheet to the correct Zone
Sheets("ZoneTemplate (2)").Name = Sheets("ProjectConfig").Range("B9")

' Setup the variables
Dim Loop1 As Integer
Dim MySheet As String
Dim NoOfOutputs As Integer
Dim NoOfInputs As Integer
Dim NoOfColumnsOffset As Integer
Dim Loop2 As Integer
Dim Loop3 As Integer
Dim loop4 As Integer
Dim SOAddr1 As String
Dim SimAddr As String


MySheet = Sheets("ProjectConfig").Range("B9").Value             ' Get the name of the sheet ready for use in the loop
NoOfOutputs = Sheets("ProjectConfig").Range("E9") - 1           ' Get the number of outputs to add

' Loop for the number of safety output functions
For Loop1 = 1 To NoOfOutputs

Worksheets(MySheet).Range("Safety_Output_Function").Select      ' select the columns to copy and copy them to buffer
Selection.Copy
Worksheets(MySheet).Range("J7").Insert Shift:=xlShiftToRight    ' Insert the copied columns infront of J1 and shift everything along to the right

Next Loop1

' Loop to generate the formula for the Sim Result check
For Loop2 = 1 To (NoOfOutputs)                                  'Sheets("ProjectConfig").Range("E9")

NoOfColumnsOffset = 8 + (Loop2 * 3)                             ' Work out the cell number for the new column
SOAddr1 = Cells(9, NoOfColumnsOffset).Address(RowAbsolute:=False, ColumnAbsolute:=False)        ' Convert the cell number to a letter reference
SimAddr = SimAddr & "," & SOAddr1 & "=1"                        ' build the string to add for each column and each time we come round the loop

Next Loop2

' put the new formulas in
Worksheets(MySheet).Cells(9, (NoOfColumnsOffset + 2)).Formula = "=IF(AND(H9=1" & SimAddr & "),1,0)"

' Loop to generate the formula for the Hardware Result check
For Loop3 = 1 To (NoOfOutputs)                                  'Sheets("ProjectConfig").Range("E9")

NoOfColumnsOffset = 9 + (Loop3 * 3)                             ' Work out the cell number for the new column
SOAddr1 = Cells(9, NoOfColumnsOffset).Address(RowAbsolute:=False, ColumnAbsolute:=False)        ' Convert the cell number to a letter reference
SimAddr = SimAddr & "," & SOAddr1 & "=1"                        ' build the string to add for each column and each time we come round the loop

Next Loop3

' put the new formulas in
Worksheets(MySheet).Cells(9, (NoOfColumnsOffset + 2)).Formula = "=IF(AND(I9=1" & SimAddr & "),1,0)"


NoOfInputs = Sheets("ProjectConfig").Range("D9") - 1            ' Get the number of Inputs to add

' Loop for the number of safety output functions
For loop4 = 1 To NoOfInputs

Worksheets(MySheet).Range("9:9").Select      ' select the columns to copy and copy them to buffer
Selection.Copy
Worksheets(MySheet).Range("A10").Insert Shift:=xlDown    ' Insert the copied columns infront of J1 and shift everything along to the right

Next loop4

End Sub