Simple Excel VBA problem; Applying a formula to a column if it contains a number

In summary, the conversation is about automating a process in which the macro checks two columns for numbers, operates on those numbers, and outputs the result in a third column. The code provided uses a loop to check each cell in the specified range and perform the desired action. The goal is to automate this process for future use.
  • #1
6.28318531
54
0
Hello,

I have a relatively simple problem, You have two columns filled with numbers. I want my macro to go through and check the columns or one of the colums to see if it has a number in it, then operate on those numbers and display an output in a third column .

I tried this;
Code:
Sub Total()
'
' Total Macro
'

'


If IsNumeric(Range("H8", Range("H8").End(xlDown))) Then
Dim CopyRange As Range
Set CopyRange = Range("J8", Range("J8").End(xlDown))
   Sheets("Sheet1").Range(CopyRange.Address).FormulaR1C1 = "=RC[-1]*RC[-2]"

End If

End Sub

I know I can simply drag a formula but I want to automate this, and I also want to be able to reuse the same thing later.

Thanks for any help.
 
Physics news on Phys.org
  • #2
You can try something like this: Sub Total()Dim lRow As LongDim c As RangelRow = Cells(Rows.Count, 8).End(xlUp).RowFor Each c In Range("H8:H" & lRow) If IsNumeric(c.Value) Then c.Offset(0, 2).FormulaR1C1 = "=RC[-1]*RC[-2]" End IfNext cEnd Sub
 

Related to Simple Excel VBA problem; Applying a formula to a column if it contains a number

1. What is Excel VBA and how is it different from regular Excel formulas?

Excel VBA (Visual Basic for Applications) is a programming language used within Excel to automate tasks and manipulate data. It is different from regular Excel formulas because it allows you to create more complex and customized functions that go beyond the capabilities of basic formulas.

2. How can I apply a formula to an entire column in Excel using VBA?

To apply a formula to an entire column in Excel using VBA, you can use a loop to iterate through each cell in the column and apply the formula to each cell individually. You can also use the "Autofill" method to automatically apply the formula to the entire column.

3. What is the best way to check if a column contains numbers in Excel using VBA?

The best way to check if a column contains numbers in Excel using VBA is to use the "IsNumeric" function. This function checks if a value is a number and returns a boolean value (True or False) accordingly. You can use this function in combination with a loop to check each cell in the column.

4. Is there a way to apply a formula to a column only if it contains numbers?

Yes, you can use the "IsNumeric" function to check if a column contains numbers and then use an "If" statement to only apply the formula if the condition is met. This way, the formula will only be applied to cells that contain numbers.

5. Can I use VBA to apply a formula to a specific range of cells within a column?

Yes, you can use the "Range" function in VBA to specify a specific range of cells within a column to apply the formula to. This allows you to have more control over which cells the formula is applied to within the column.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
55K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
18
Views
5K
  • Programming and Computer Science
Replies
2
Views
4K
  • Computing and Technology
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • General Discussion
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
Back
Top