Solved

Set range with Excel VBA

Posted on 2015-01-27
10
176 Views
Last Modified: 2015-01-28
How to set whole column with Excel VBA, such that for example, column AB = column AA * column K

Tks
0
Comment
Question by:AXISHK
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40574616
If you want to set formula for whole column, try this.
ActiveSheet.Columns("AB").Formula = "=AA1*K1"

Open in new window

0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 40574619
Hi,

pls try

Range(Range("AB1"), Range("AB" & Range("K" & Rows.Count).End(xlUp).Row)).FormulaR1C1 = "=RC[-1]*RC[-17]"


Regards
0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40574625
If you just want to put values instead formulas, please try this.
Sub hakan()
    Dim iterrow As Range
    Application.Calculation = xlCalculationManual
    With ActiveSheet
        For Each iterrow In .UsedRange.Rows
            .Range("AB" & iterrow.Row).Value = .Range("AA" & iterrow.Row).Value * .Range("K" & iterrow.Row).Value
        Next iterrow
    End With
    Application.Calculation = xlCalculationAutomatic
End Sub

Open in new window

0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 40574667
Why would you do that? It's really inefficient to populate an entire column, especially with the new file formats.
0
 

Author Comment

by:AXISHK
ID: 40574686
.Range("AB" & iterrow.Row).Value = .Range("AA" & iterrow.Row).Value * .Range("K" & iterrow.Row).Value

It stops at above statement with "Type mistake"

The worksheet will create dynamically through VBA and I need to keep 1st row as heading.

Hakan's solution seems suitable in my case when it run with error, Any idea ?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40574702
I am not getting any error, can you send a picture of code in your sheet?

@Rory You're right, it is bad to have whole column populated. Last code i've sent stops at the end of used range.
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 100 total points
ID: 40574750
I imagine the error comes from trying to multiply the column headers together.
0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40574755
please try this.
Sub hakan()
    Dim iterrow As Range
    Application.Calculation = xlCalculationManual
    With ActiveSheet
        For Each iterrow In .UsedRange.Rows
            .Range("AB" & iterrow.Row).Value = Val(Replace(.Range("AA" & iterrow.Row).Value, ",", ".")) * Val(Replace(.Range("K" & iterrow.Row).Value, ",", "."))
        Next iterrow
    End With
    Application.Calculation = xlCalculationAutomatic
End Sub

Open in new window

0
 
LVL 5

Accepted Solution

by:
Hakan Yılmaz earned 400 total points
ID: 40574757
or try this if you want to skip non numeric values.
Sub hakan()
    Dim iterrow As Range
    Application.Calculation = xlCalculationManual
    With ActiveSheet
        For Each iterrow In .UsedRange.Rows
            If IsNumeric(.Range("AA" & iterrow.Row).Value) And IsNumeric(.Range("K" & iterrow.Row).Value) Then
                .Range("AB" & iterrow.Row).Value = .Range("AA" & iterrow.Row).Value * .Range("K" & iterrow.Row).Value
            End If
        Next iterrow
    End With
    Application.Calculation = xlCalculationAutomatic
End Sub

Open in new window

0
 

Author Closing Comment

by:AXISHK
ID: 40575330
Tks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now