Simple replace value with a certain value by column macro

peterhui50

New Member
Joined
Nov 4, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to create a macro that will replace all the "1"s in a column with a value in Range "A1", then move onto column 2 and replace it with the value in Range "B1" and so on.

1667585188496.png


this is what I have so far
Code:
Sub Macro2()
'
' Macro2 Macro

 Dim X As Integer
 X = InputBox("What column number?")
 Y = InputBox("Where is the value?")
 
Worksheets("Sheet1").Columns(X).Replace _
 What:=1, Replacement:=Range(Y).Value, _
 SearchOrder:=xlByColumns, MatchCase:=True
End Sub

As you can see, it requires the user to put the column number and the location of the value, it works but just not efficient at all.

I"m looking to just loop through a range of columns and the value of it's replacement will move along with it.

Any help is very much appreciated, I have several hundred of these columns and tried to do this in PQ but it gives another problem.
 

Attachments

  • 1667585180922.png
    1667585180922.png
    13.4 KB · Views: 4

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to the Board!

Try this:
VBA Code:
Sub MyReplaceMacro()

    Dim lc As Long
    Dim c As Long
    Dim x As Variant
    
    Application.ScreenUpdating = False
    
'   Find last column in row 1 with data
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    
'   Loop through each column
    For c = 1 To lc
'       See if anything in row 1
        If Cells(1, c) <> "" Then
'           Capture value from row 1
            x = Cells(1, c).Value
'           Replace all non-blank cells in column with value
            Columns(c).SpecialCells(xlCellTypeConstants, 23).Value = x
        End If
    Next c
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
Welcome to the Board!

Try this:
VBA Code:
Sub MyReplaceMacro()

    Dim lc As Long
    Dim c As Long
    Dim x As Variant
   
    Application.ScreenUpdating = False
   
'   Find last column in row 1 with data
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
   
'   Loop through each column
    For c = 1 To lc
'       See if anything in row 1
        If Cells(1, c) <> "" Then
'           Capture value from row 1
            x = Cells(1, c).Value
'           Replace all non-blank cells in column with value
            Columns(c).SpecialCells(xlCellTypeConstants, 23).Value = x
        End If
    Next c
   
    Application.ScreenUpdating = True
   
End Sub
worked like a charm! thank you!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,602
Members
449,089
Latest member
Motoracer88

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