macro to search on another workbook and replace data

excel01noob

Board Regular
Joined
Aug 5, 2019
Messages
93
Office Version
  1. 365
  2. 2016
Hi

I have two table, table 1 is the registered invoices for a particular month where I usually have:
- old customer accounts (more than 5 digits - column ACCT_ID - table 1);
- newer accounts have only 5 digits as account number (column ACCT_ID - table 1).

I need to prepare report where I combine both account numbers.
For the old accounts, I need to get a REGIST number that comes from table 2 (this table is saved on another workbook which I don't want to open)

Under column F "result" I want to have the accounts with this format:

-new accounts: starting with N and then the 5-digits account and three zeros (example: acc 27193 is N27193000);
-old accounts: I need to pick the first 4 digits from the left of the account under ACCT_ID. Then I need to match that 4-digit number with ACCT_ID from table 2 (example: both accounts 7268999 and 7268111 will have the same first 4 digits which as per table 2 gives REGIST F72682001


RANS_TYPARTICVALUEDTACCT_IDAMMTRESULT?MANDTACCT_IDLEG_ACCTREGIST
INVCAS3225TF2021-05-03T00:00:00.0000007268999225640107268000F72682001
INVCHE1174WE2021-05-04T00:00:00.00000040431111005.990104043002F40431985
INVCHE459DE2021-05-08T00:00:00.00000072681111985.430104482283F44821989
INVCAS287SY2021-05-11T00:00:00.000000448299934.50107004204F70041978
INVCAS1174WE2021-05-11T00:00:00.000000267252569.520101009000F10091993
INVACC133LO2021-05-13T00:00:00.0000007268000875.440101005080F10091993
INVACC459DE2021-05-13T00:00:00.00000027194224.550101005203F10091993
INVCHE117RE2021-05-28T00:00:00.0000007004111103298.5
INVCHE287SY2021-05-28T00:00:00.000000271931433.99
TABLE 1TABLE 2


How can use a macro that used both workbooks and fill in column F on table 1?
Thank you so much!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I've managed to work part of the sub routine but I am having difficulty completing it

Option Explicit

Sub accounts_old_and_new()

Dim LastRow As Long

LastRow = Range("D" & Rows.Count).End(xlUp).Row

Dim i As Long

For i = 2 To LastRow

If Len(Range("D" & i)) = 5 Then
Range("F" & i).Value = "C" & Range("D" & i)

'Else

Range("F" & i).Value = ???

Else

End If
Next i

End Sub
 
Upvote 0
Hi,​
according to « this table is saved on another workbook which I don't want to open » so if this table is smart enough designed you should use a VLOOKUP formula …​
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,546
Members
449,038
Latest member
Guest1337

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