VBA Code for Copying a Formula

ppiplastics

New Member
Joined
Feb 27, 2008
Messages
12
I would like to prompt the macro to ask for a column number and then enter the following formula in the cell from row 2 onward until the last data in the column:
=IF(A2="","Unique",IF(COUNTIF($A$2:$A$1000,A2)>0,"Match","Unique")). I want to populate the entire column where the data ends with the formula. In the second step I want the formula output to change from formula to a value.
Thank you for your help.
Thanks
Neal
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
.. enter the following formula in the cell from row 2 onward until the last data in the column
I assume you mean down to the last data in column A, not the column entered by the user since the code is going to populate that column with data?


=IF(A2="","Unique",IF(COUNTIF($A$2:$A$1000,A2)>0,"Match","Unique"))
I believe your formula is flawed as the only rows this will ever poulate with "Unique" are rows where column A is blank. Where there is data in column A, the COUNTIF will always be at least 1.

Try the following in a copy of your workbook, noting that the code
a) Asks for a a column letter (not number)
b) Uses your formula but I think you probably need to change that red 0 to a 1

Rich (BB code):
Sub PopulateColumn()
  Dim Col As String
  Dim lr As Long
  Dim CheckCell As Range
  
  Col = InputBox("Enter column label")
    On Error Resume Next
    Set CheckCell = Range(Col & Rows.Count)
    On Error GoTo 0
    If Not CheckCell Is Nothing Then
      lr = Range("A" & Rows.Count).End(xlUp).Row
      With Range(Col & "2:" & Col & lr)
        .Formula = "=IF(A2="""",""Unique"",IF(COUNTIF($A$2:$A$" & lr & ",A2)>0,""Match"",""Unique""))"
        .Value = .Value
      End With
    End If
End Sub
 
Last edited:
Upvote 0
Peter,
A big thank you and it worked like a charm! And yes my formula should have the greater than 1 value and not zero.
Thank you for your help.
Have a great day! Or evening!
Thanks
Neal
 
Upvote 0
By the way, is it possible to prompt for the column first that it looks for duplicate value? Currently, it is looking for those value in column A. I would rather have it prompt me to enter a column number which it must look for duplicate values in.
 
Upvote 0
So you want a prompt for both the column to check for duplicates and the column in which to put the results?
 
Upvote 0
Yes' Peter that is what I would appreciate:
1. Prompt me for column to look for duplicate values
2.Prompt me for column where to put results.
Appreciate your help.
Thank you
Neal
 
Upvote 0
Yes' Peter that is what I would appreciate:
1. Prompt me for column to look for duplicate values
2.Prompt me for column where to put results.
Try this version then
Code:
Sub PopulateColumn_v2()
  Dim UniqueCol As String, ResultCol As String
  Dim lr As Long
  Dim TestUniqueCol As Range, TestResultCol As Range
  Dim InvalidCols As String
  
  UniqueCol = InputBox("Enter column to check for Match/Unique")
  ResultCol = InputBox("Enter column for results")
  On Error Resume Next
  Set TestUniqueCol = Range(UniqueCol & Rows.Count)
  Set TestResultCol = Range(ResultCol & Rows.Count)
  On Error GoTo 0
  If Not TestUniqueCol Is Nothing And Not TestResultCol Is Nothing Then
    lr = Range(UniqueCol & Rows.Count).End(xlUp).Row
    With Range(ResultCol & "2:" & ResultCol & lr)
      .Formula = Replace("=IF(#2="""",""Unique"",IF(COUNTIF($#$2:$#$" & lr & ",#2)>1,""Match"",""Unique""))", "#", UniqueCol)
      .Value = .Value
    End With
  Else
    Select Case True
      Case TestUniqueCol Is Nothing And TestResultCol Is Nothing
        InvalidCols = "Match/uniques column (" & UniqueCol & ") and Result column (" & ResultCol & ")"
      Case TestUniqueCol Is Nothing
        InvalidCols = "Match/uniques column (" & UniqueCol & ")"
      Case Else
        InvalidCols = "Result column (" & ResultCol & ")"
    End Select
    MsgBox "You entered invalid column label(s) as follows:" & vbLf & InvalidCols
  End If
End Sub
 
Upvote 0
Peter,
Thank you again! It certainly was a perfect solution to something I had been trying to do but failed to accomplish on my own.
Best Regards,
Neal
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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