简体   繁体   中英

How to write Pythagoras formula in excel VBA, like I need to select all the values of column A and column B

Sub MS()    

Data = Sheets("Tabelle1").Select   
Rows("1:1").Select
Rows("11409:11409").Select

Dim bilder As Long
Dim n As Long
Dim d As Long
Dim t As Long

bilder = 64
n = 1    
d = 0
t = 0

'Dim i As Long   

'For i = 1 To lastrow

Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row).Select

Range("b1:b" & Cells(Rows.Count, 1).End(xlUp).Row).Select

'Range("a1").Select

'Range("b1").Select

Range("a1,b1").Select

Do While ActiveCell.Value <> ""       
    Radius = Sqr(Range("A1").Value * Range("A1").Value + Range("B1").Value * Range("B1").Value)      
    ActiveCell.Offset(1, 1).Select 
Loop    

End Sub

I'm not sure why you'd want to do it this way (given that it can be done with a simple formula in-cell), but looking at the remnants of code in your question we can see what you're trying to achieve. Here's how I'd do it:

Sub MS()

    Dim sht As Worksheet, StartRow As Long, LastRow As Long, OutputColumn As Long
    Dim SideA As Double, SideB As Double, SideC As Double
    
    With Worksheets("Tabelle1")
    
        'Set StartRow to the first row of your data ignoring headers
        StartRow = 2
        
        'Locate LastRow as last occupied cell in column A
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        'Set OutputColumn to 3
        OutputColumn = 3
        
        'Start loop
        For r = StartRow To LastRow
            SideA = .Cells(r, 1).Value
            SideB = .Cells(r, 2).Value
            SideC = Sqr(SideA * SideA + SideB * SideB)
            .Cells(r, OutputColumn).Value = SideC
        Next
        
    End With

End Sub

Output:

在此处输入图像描述

You do not need to select the range to work with it. You may want to see How to avoid using Select in Excel VBA

In your code you are not writing the output to any cell. Here are two ways that will help you achieve what you want.

NON VBA - WAY 1

Put the formula =SQRT(A1*A1+B1*B1) or =SQRT(A1^2+B1^2) in C1 and drag it down

VBA - WAY 2 (Without Looping)

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    
    Set ws = Sheets("Tabelle1")
    
    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        
        With .Range("C1:C" & lRow)
            .Formula = "=SQRT(A1*A1+B1*B1)"
            .Value = .Value
        End With
    End With
End Sub

VBA - WAY 3 (Without Looping) Slightly complicated way of doing this. Explanation can be seen HERE

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    
    Set ws = Sheets("Tabelle1")
    
    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        
        With .Range("C1:C" & lRow)
            .Value = Evaluate("index(SQRT((A1:A" & lRow & _
                              ")^2+(B1:B" & lRow & _
                              ")^2),)")

        End With
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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