Replace text with reference cell with the help of a code

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys

I am trying to add a code in the beginning of the already existing code. I have 2 steps to write. I am not able to record the below with the help of a macro and to write the code is too complicated for me. Need help to understand and write the code.

  • select the column C and replace “(as per details)” with the name in cell K1.
  • In column G my application won’t work if the voucher numbers are same. So, I want to give the voucher numbers in the series of 1001 to as many vouchers are there in the sheet. In this case there are 11 vouchers. They have to be replaced with 1001 to 1011.
Test Multiple voucher workings.xlsx
ABCDEFGHIJKL
1W Test PurchasesKotak Mahindra Bank
2Kotak Mahindra Bank Book
3
4
51-Apr-2021 to 14-Jul-2021
6DateParticularsVch TypeVch No.DebitCredit
701-07-2021Cr(as per details)Receipt14040.00
8Sunday1015.00 Cr
9Monday2025.00 Cr
10Tuesday1000.50 Cr
11Round Off0.50 Dr
1201-07-2021Dr(as per details)Payment11001.00
13January100.00 Dr
14February200.00 Dr
15March300.00 Dr
16April400.00 Dr
17Round Off1.00 Dr
1802-07-2021Cr(as per details)Receipt24040.00
19Sunday1015.00 Cr
20Monday2025.00 Cr
21Tuesday1000.50 Cr
22Round Off0.50 Dr
2303-07-2021Dr(as per details)Payment21001.00
24January100.00 Dr
25February200.00 Dr
26March300.00 Dr
27April400.00 Dr
28Round Off1.00 Dr
2904-07-2021Dr(as per details)Payment31001.00
30January100.00 Dr
31February200.00 Dr
32March300.00 Dr
33April400.00 Dr
34Round Off1.00 Dr
3505-07-2021Cr(as per details)Receipt34040.00
36Sunday1015.00 Cr
37Monday2025.00 Cr
38Tuesday1000.50 Cr
39Round Off0.50 Dr
4006-07-2021CrCashContra11000.00
4106-07-2021DrCashContra22000.00
4209-07-2021Dr(as per details)Payment81001.00
43January100.00 Dr
44February200.00 Dr
45March300.00 Dr
46April400.00 Dr
47Round Off1.00 Dr
4814-07-2021DrCashContra32000.00
4914-07-2021CrCashContra41000.00
5014120.008004.00
51DrClosing Balance6116.00
5214120.0014120.00
Bank 5
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
@RAJESH1960 Maybe this?

VBA Code:
Sub VchNos()
Dim NewName As String
Dim VchNo As Integer

NewName = Range("K1")
VchNo = 1000

For Each Cell In Range("G7:G" & Range("C" & Rows.Count).End(xlUp).row)
    If Not Cell = vbNullString Then
        VchNo = VchNo + 1
        Cell.Value = VchNo
     End If
     
    If Cell.Offset(0, -4) = "(as per details)" Then Cell.Offset(0, -4).Value = NewName
       
Next Cell
End Sub
 
Upvote 0
@RAJESH1960 Maybe this?

VBA Code:
Sub VchNos()
Dim NewName As String
Dim VchNo As Integer

NewName = Range("K1")
VchNo = 1000

For Each Cell In Range("G7:G" & Range("C" & Rows.Count).End(xlUp).row)
    If Not Cell = vbNullString Then
        VchNo = VchNo + 1
        Cell.Value = VchNo
     End If
    
    If Cell.Offset(0, -4) = "(as per details)" Then Cell.Offset(0, -4).Value = NewName
      
Next Cell
End Sub
Snakehips, I am getting a compile error - variable not defined ...For Each Cell In Range
 
Upvote 0
Snakehips, I had to rearrange my data due to some problems. I am trying to run your code in a sheet where the columns have changed. I changed the code as per the columns. When I run the first half of the code separately it is running fine but when I add the second half where I have to replace “(as per details”) with the cell reference I am not able to get the result. I am getting a compile error. Can you please see what is wrong with the code.?
Sub VoucherNos()
Dim VchNo As Integer
Dim CELL As Variant

VchNo = 1000
For Each CELL In Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row)
If Not CELL = vbNullString Then
VchNo = VchNo + 1
CELL.Value = VchNo
End If
Next
Dim NewName As String
NewName = Sheets("Original").Range("K1")
If CELL.Offset(0, -4) = "(as per details)" Then CELL.Offset(0, -4).Value = NewName

End With
End Sub

Book1
ABCDEFG
1LineDateVch TypeVch No.ParticularsDebitCredit
21702-07-2021Receipt3Sunday1015.00
33004-07-2021Payment6January100.00
43606-07-2021Contra1Cash1000.00
53706-07-2021Contra2Cash2000.00
64411-07-2021Receipt11Sunday1015.00
74511-07-2021Payment10January100.00
84612-07-2021Receipt12Sunday1015.00
94712-07-2021Payment11January100.00
104814-07-2021Contra3Cash2000.00
114914-07-2021Contra4Cash1000.00
12
13101-07-2021Receipt1(as per details)4040.00
142Sunday1015.00
153Monday2025.00
164Tuesday1000.50
175Round Off0.50
18601-07-2021Payment1(as per details)1001.00
197January100.00
208February200.00
219March300.00
2210April400.00
2311Round Off1.00
241202-07-2021Receipt2(as per details)4040.00
2513Sunday1015.00
2614Monday2025.00
2715Tuesday1000.50
2816Round Off0.50
291803-07-2021Payment2(as per details)1001.00
3019January100.00
3120February200.00
3221March300.00
3322April400.00
3423Round Off1.00
352404-07-2021Payment3(as per details)1001.00
3625January100.00
3726February200.00
3827March300.00
3928April400.00
4029Round Off1.00
413105-07-2021Receipt4(as per details)4040.00
4232Sunday1015.00
4333Monday2025.00
4434Tuesday1000.50
4535Round Off0.50
463809-07-2021Payment9(as per details)1001.00
4739January100.00
4840February200.00
4941March300.00
5042April400.00
5143Round Off1.00
Bank
 
Upvote 0
@RAJESH1960 Try this.

VBA Code:
Sub VchNos()
Dim NewName As String
Dim VchNo As Integer
Dim Cell As Range

NewName = Range("K1")
VchNo = 1000
'Step thro Voucher column D2:D'Lastrow'as determined by last entry in E
For Each Cell In Range("D2:D" & Range("E" & Rows.Count).End(xlUp).row)
    If Not Cell = vbNullString Then  'if not an empty cell then is a voucher number so update it
        VchNo = VchNo + 1
        Cell.Value = VchNo
     End If
 'Particular column is E which is 1 column offset right from D
 'Check and update if required
    If Cell.Offset(0, 1) = "(as per details)" Then Cell.Offset(0, 1).Value = NewName
       
Next Cell
End Sub
 
Upvote 0
Solution
@RAJESH1960 Try this.

VBA Code:
Sub VchNos()
Dim NewName As String
Dim VchNo As Integer
Dim Cell As Range

NewName = Range("K1")
VchNo = 1000
'Step thro Voucher column D2:D'Lastrow'as determined by last entry in E
For Each Cell In Range("D2:D" & Range("E" & Rows.Count).End(xlUp).row)
    If Not Cell = vbNullString Then  'if not an empty cell then is a voucher number so update it
        VchNo = VchNo + 1
        Cell.Value = VchNo
     End If
 'Particular column is E which is 1 column offset right from D
 'Check and update if required
    If Cell.Offset(0, 1) = "(as per details)" Then Cell.Offset(0, 1).Value = NewName
     
Next Cell
End Sub
checking.... I am a bit slow.
 
Upvote 0
@RAJESH1960 Try this.

VBA Code:
Sub VchNos()
Dim NewName As String
Dim VchNo As Integer
Dim Cell As Range

NewName = Range("K1")
VchNo = 1000
'Step thro Voucher column D2:D'Lastrow'as determined by last entry in E
For Each Cell In Range("D2:D" & Range("E" & Rows.Count).End(xlUp).row)
    If Not Cell = vbNullString Then  'if not an empty cell then is a voucher number so update it
        VchNo = VchNo + 1
        Cell.Value = VchNo
     End If
 'Particular column is E which is 1 column offset right from D
 'Check and update if required
    If Cell.Offset(0, 1) = "(as per details)" Then Cell.Offset(0, 1).Value = NewName
      
Next Cell
End Sub
I had to change just one line and it worked perfectly. Thanks once again Snakehips.
NewName = Sheets("Original").Range("K1")
 
Upvote 0
Thanks for the explanation line by line. It really helps me to understand better.
I had to change just one line and it worked perfectly. Thanks once again Snakehips.
NewName = Sheets("Original").Range("K1")
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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