Question

IN VBA Please 2.Write a function that takes 2 ranges as input and gets the sum...

IN VBA Please

2.Write a function that takes 2 ranges as input and gets the sum of absolute difference of every element if there two ranges have the same numbers of elements.

the function can send error message to user if the size of these two ranges are different.

      the function can take not only 2 ranges but also two arrays as input

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find the sample VB code:

We can use the Abs function to get the absolute value.
Below is a sample function to calculate the absolute difference between two ranges.

------------------------

Private Sub CommandButton1_Click()
Dim Rng1 As Range
Dim Rng2 As Range
Dim CountRng1 As Long
Dim CountRng2 As Long
Dim absSum As Long
Dim cell As Range

Set Rng1 = Range("A1:A3")
Set Rng2 = Range("B1:B3")
CountRng1 = Rng1.Count
CountRng2 = Rng2.Count

If CountRng1 = CountRng2 Then
    For i = 1 To CountRng1
        absSum = absSum + Abs(Rng1(i).Value - Rng2(i).Value)
    Next i
    MsgBox (absSum)
Else
        MsgBox ("Ranges are not the same size")
End If
End Sub

-------------------------


I have added VB Script code to a command button in excel sheet:

Hope this helps
Thanks,

Add a comment
Know the answer?
Add Answer to:
IN VBA Please 2.Write a function that takes 2 ranges as input and gets the sum...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • GOALI Using your text editor, write the function sumSquares(aList). The function takes a list as ninput...

    GOALI Using your text editor, write the function sumSquares(aList). The function takes a list as ninput and returns (not prints) the sum of the squares of the all the numbers which absolute value s divisible by 3. If an element in the list is not a number, the function should ignore the value and ontinue. When the function receives an input that is not a list, code should return an error message o the user. Hints: review the type) or...

  • Excel to VBA Write a program that will add 2 numbers input by the user and...

    Excel to VBA Write a program that will add 2 numbers input by the user and display the result in a spreadsheet. Use an input box and Val () function to convert the user input to a numerical data type.

  • Write a function called ''minandmax'' . Function ''minandmax'' takes only one input argument that is a...

    Write a function called ''minandmax'' . Function ''minandmax'' takes only one input argument that is a matrix M . This matrix M can be any size (any number of columns and rows) with any random integer values . Function ''minandmax'' returns two output arguments. The first output argument is called ''difference_row'' that returns a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. The second output argument is called ''difference_all''...

  • Please write a java program that takes in user input of various numbers as a type...

    Please write a java program that takes in user input of various numbers as a type string, prints the sum of all numbers entered, and prints the smallest and largest numbers. Arrays are optional as they have not been covered by my professor yet. Thank you

  • Write a C function that takes two strings (character arrays) as input and return 1 if...

    Write a C function that takes two strings (character arrays) as input and return 1 if they are equal and return 0 if they re not. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal in the same order.

  • Write a C function that takes two strings (character arrays) as input and return 1 if...

    Write a C function that takes two strings (character arrays) as input and return 1 if they are equal and return 0 if they re not. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal in the same order.

  • Create a program that takes in user input and stores the input into an array. Also...

    Create a program that takes in user input and stores the input into an array. Also within the program show how you can: show the elements in the array, remove an element from the array, add an element to the array. Do NOT ask the user how many elements they want to add, instead once they are done adding, they should hit ‘q’ and it should break out of the input. This is a c++ program. The input type shall...

  • VBA on Excel 2. Complete this problem on Sheet2. a) Write a user-defined function AreaOfTriangle that...

    VBA on Excel 2. Complete this problem on Sheet2. a) Write a user-defined function AreaOfTriangle that computes and returns the area of a triangle with sides s1, s2, s3. b)Test your function AreaOfTriangle by writing a main procedure, which has the user to input the sides , calls the function to find the area , and displays the result with a message box. Test your program with s1=6.5, s2=4.2, s3=5.1.

  • IN PYTHON 3.7.4 1. Write a function that gets two numbers from the user and returns...

    IN PYTHON 3.7.4 1. Write a function that gets two numbers from the user and returns an absolute value of the smaller number. For example, if the user enters -7 and 4, the function returns 7. 2. Write a function that gets two numbers from the user. If the two numbers are consecutive, return “Consecutive”. Otherwise, return “Not Consecutive”.

  • a) Write a function that takes as input 2 parameters, an array and its size (both...

    a) Write a function that takes as input 2 parameters, an array and its size (both ints) and returns the max. Include the driving (main program also. To pass an array to a function, do the following: return_type my_function(int my_array[], int size) b) Write a function that does the above but can contain duplicate numbers and is capable of removing them. Scan in input. Note you may need the sizeof( ) function which is one of the standard libraries.Thus you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT