PaulAsaran
New Member
- Joined
- Feb 11, 2019
- Messages
- 22
I imagine this is painfully simple, yet all my research and trying to figure it out on my own has led to failure.
I've learned how to create a button in Excel and create code in VBA to have that button change cell values. The problem is that right now I only know how to affect one cell at a time or, at best, a range where all cells are to have the same result. But today I have a column of cells that are taking information from other sheets in the workbook:
[TABLE="class: grid, width: 300, align: center"]
<tbody>[TR]
[TD="align: center"]Pole Strength[/TD]
[/TR]
[TR]
[TD="align: center"]=IF(NOT(Data!A2),Data!N2,0)[/TD]
[/TR]
[TR]
[TD="align: center"]=IF(NOT(Data!A3),Data!N3,0)[/TD]
[/TR]
[TR]
[TD="align: center"]=IF(NOT(Data!A4),Data!N4,0)[/TD]
[/TR]
[TR]
[TD="align: center"]=IF(NOT(Data!A5),Data!N5,0)[/TD]
[/TR]
</tbody>[/TABLE]
I want the user to be able to write over the calculations with their own numbers, but be able to replace the calculations if they press a "Recalculate" button. I know the general idea of how the button works, having made one for clearing data before, but this is a slightly different animal with some more complicated elements I'm not used to.
My goal is to create a simpler code, one that doesn't require me to specify each and every cell that needs changing. My idea was to use a For Each loop to sequentially increase the row number of the cell for each step of the loop (i.e. Data!A2 > Data!A3 > Data! A4, and so on). Trying to input a formula based on this failed, so I ultimately came up with this:
Note that Sheet2 is the sheet that is being changed by the code and Sheet5 is the "Data" sheet that I'm pulling information from.
This is literally something I slapped together on my own after a few hours of studying various subjects trying to find a good way to achieve my goal. I'm not receiving any errors, but the code also isn't doing anything, so clearly something is wrong (I am once more reminded that I really need to take a proper VBA course).
If someone could point out what I'm doing wrong (I'm sure it's obvious), I'd appreciate it. Would a "Do Until" or simple "For" loop be better?
I've learned how to create a button in Excel and create code in VBA to have that button change cell values. The problem is that right now I only know how to affect one cell at a time or, at best, a range where all cells are to have the same result. But today I have a column of cells that are taking information from other sheets in the workbook:
[TABLE="class: grid, width: 300, align: center"]
<tbody>[TR]
[TD="align: center"]Pole Strength[/TD]
[/TR]
[TR]
[TD="align: center"]=IF(NOT(Data!A2),Data!N2,0)[/TD]
[/TR]
[TR]
[TD="align: center"]=IF(NOT(Data!A3),Data!N3,0)[/TD]
[/TR]
[TR]
[TD="align: center"]=IF(NOT(Data!A4),Data!N4,0)[/TD]
[/TR]
[TR]
[TD="align: center"]=IF(NOT(Data!A5),Data!N5,0)[/TD]
[/TR]
</tbody>[/TABLE]
I want the user to be able to write over the calculations with their own numbers, but be able to replace the calculations if they press a "Recalculate" button. I know the general idea of how the button works, having made one for clearing data before, but this is a slightly different animal with some more complicated elements I'm not used to.
My goal is to create a simpler code, one that doesn't require me to specify each and every cell that needs changing. My idea was to use a For Each loop to sequentially increase the row number of the cell for each step of the loop (i.e. Data!A2 > Data!A3 > Data! A4, and so on). Trying to input a formula based on this failed, so I ultimately came up with this:
Code:
Private Sub PoleStrength_Click()
Dim rng As Range, cell As Range, cnt As Integer, A As Range, N As Range
Set rng = Sheet2.Range("R17:R43")
cnt = 0
A = Sheet5.Cells(2 + cnt, 1)
N = Sheet5.Cells(2 + cnt, 14)
For Each cell In rng
If Not A Then
ActiveCell.Value = N
Else
ActiveCell.Value = 0
End If
cnt = cnt + 1
Next
End Sub
Note that Sheet2 is the sheet that is being changed by the code and Sheet5 is the "Data" sheet that I'm pulling information from.
This is literally something I slapped together on my own after a few hours of studying various subjects trying to find a good way to achieve my goal. I'm not receiving any errors, but the code also isn't doing anything, so clearly something is wrong (I am once more reminded that I really need to take a proper VBA course).
If someone could point out what I'm doing wrong (I'm sure it's obvious), I'd appreciate it. Would a "Do Until" or simple "For" loop be better?