简体   繁体   English

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

[英]Excel VBA using Cells properties in a formula

I have created a macro that looks at number in a cell and then copies a group of three columns and inserts them to the right of the group. 我创建了一个宏,该宏查看单元格中的数字,然后复制一组三列,并将其插入到该组的右侧。 This all works fine. 这一切都很好。

I have a formula in a cell after these groups of columns that looks to see if there is a 1 in the cell. 这些列组之后的单元格中有一个公式,用于查看该单元格中是否有1。 the code below is what there would be assuming I created 2 groups. 下面的代码是假设我创建了2个组的代码。

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

I want to be able to automatically add the M9=1,P=1 if I had created four groups. 如果我创建了四个组,我希望能够自动添加M9 = 1,P = 1。

If someone has the time to help it would be much appreciated. 如果有人有时间帮助,将不胜感激。

Sorry, learning as I go on here. 抱歉,我在这里继续学习。

I am creating a matrix where I can build up a number of functions in the columns direction and a number of inputs that effect the functions in the rows direction. 我正在创建一个矩阵,在该矩阵中我可以在列方向上构建许多功能,并在行方向上构建影响功能的许多输入。 I start off with a 'group' of three columns per function, In my first group G9 is the expected condition, H9 is the result during simulation and I9 is the result during real world tests. 我从每个功能由三列组成的“组”开始,在我的第一组中,G9是预期条件,H9是模拟过程中的结果,而I9是现实世界测试中的结果。 I want to be able to say how many functions and inputs there will be and automatically create the matrix. 我希望能够说出会有多少个函数和输入并自动创建矩阵。

If I have two functions then there will be two groups of columns from G to L. 如果我有两个函数,那么将有两组列从G到L。

After all of the functions I have a check to see if they all passed, with two functions this check would be in M9, where I have the formula =IF(AND(H9=1,K9=1),1,0) that checks to see if there is a 1 in both H9 and K9 and then puts a 1 in M9. 在所有功能之后,我进行检查以查看它们是否全部通过,其中两个功能将在M9中进行检查,其中我有公式= IF(AND(H9 = 1,K9 = 1),1,0)检查H9和K9中是否都有1,然后在M9中放入1。

If I had four functions then I would need the check formula of =IF(AND(H9=1,K9=1,N=1,Q=1),1,0) in S1 如果我有四个函数,则需要在S1中使用= IF(AND(H9 = 1,K9 = 1,N = 1,Q = 1),1,0)的检查公式

I want to create the check formula within a loop so that it adds in the correct cells to check. 我想在循环中创建检查公式,以便它添加要检查的正确单元格。

Hope this explains it a little bit better, but probably not!! 希望这可以更好地解释它,但可能不会!

Here is the code so far 这是到目前为止的代码

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

So this did the trick.... 所以这成功了。

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

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

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