VBA to take input from user and process data

swapnilk

Board Regular
Joined
Apr 25, 2016
Messages
75
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi,

I am using the undernoted formula:

Excel Formula:
C2=COUNTIF(A:A,A2)=COUNTIF(B:B,B2)

The issue is that the range A:A and B:B can change and the result will also be displayed in different cell. So is there a way to prompt user to enter three variables i.e. columns where A:A data is, column for B:B data and where the user wants to display the result.

For e.g. on running the code it will ask user to select three variables i.e. location of C2, A:A and B:B, based on the input entered by the user the script will replace the formula "C2=COUNTIF(A:A,A2)=COUNTIF(B:B,B2)" with the user entered input and display the result.

Any help is appreciated!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try:
VBA Code:
Sub Processdata()
    Application.ScreenUpdating = False
    Dim loc As String, col1 As String, col2 As String
    loc = InputBox("Enter the location, for example C2.")
    col1 = InputBox("Enter the first column letter.")
    col2 = InputBox("Enter the second column letter.")
    Range(loc).Formula = "=COUNTIF(" & col1 & ":" & col1 & "," & col1 & "2)=COUNTIF(" & col2 & ":" & col2 & "," & col2 & "2)"
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hi, your solution worked perfectly. I have different formulas and was trying to use the same code that you have given but excel is adding @ symbol in front of the formula and the formula is not functioning the way it should. E.g for undernoted formula
Excel Formula:
=TRANSPOSE(FILTER(A:A,B2=B:B))
I am using this code
Excel Formula:
Sub Test()
    Application.ScreenUpdating = False
    Dim loc As String, col1 As String, col2 As String
    loc = InputBox("Enter the Result location e.g , for example C2.")
    col1 = InputBox("Enter 1st Column Letter e.g A")
    col2 = InputBox("Enter 2nd Column Letter e.g. B")
    Range(loc).Formula = "=TRANSPOSE(FILTER(" & col1 & ":" & col1 & "," & col2 & 2 & "=" & col2 & ":" & col2 & "))"
    Application.ScreenUpdating = True
End Sub

The formula that gets pasted in C2 by running the above code is
Excel Formula:
=@TRANSPOSE(FILTER(A:A,B2=B:B))

Any idea why the @ symbol is coming after = sign. The formula also doesn't give the required result.
 
Upvote 0
Hi, your solution worked perfectly. I have different formulas and was trying to use the same code that you have given but excel is adding @ symbol in front of the formula and the formula is not functioning the way it should. E.g for undernoted formula
Excel Formula:
=TRANSPOSE(FILTER(A:A,B2=B:B))
I am using this code
Excel Formula:
Sub Test()
    Application.ScreenUpdating = False
    Dim loc As String, col1 As String, col2 As String
    loc = InputBox("Enter the Result location e.g , for example C2.")
    col1 = InputBox("Enter 1st Column Letter e.g A")
    col2 = InputBox("Enter 2nd Column Letter e.g. B")
    Range(loc).Formula = "=TRANSPOSE(FILTER(" & col1 & ":" & col1 & "," & col2 & 2 & "=" & col2 & ":" & col2 & "))"
    Application.ScreenUpdating = True
End Sub

The formula that gets pasted in C2 by running the above code is
Excel Formula:
=@TRANSPOSE(FILTER(A:A,B2=B:B))

Any idea why the @ symbol is coming after = sign. The formula also doesn't give the required result.

ok, so i solved the problem by adding Formula2 in place of Formula
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top