Solved

Set range with Excel VBA

Posted on 2015-01-27
10
224 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 50

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 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