issue with using range that I have obtained

Oberon70

Board Regular
Joined
Jan 21, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi,

I keep receiving the below message:

1643069566233.png


for the below macro

VBA Code:
Sub Delete_Last_2_Rows()

Dim i As Long
Dim rng_1 As Range
Dim rng_2 As Range
Dim ws As Worksheet
Set ws = Sheets("Data")
Set rng_1 = Range("A:A")

ws.Select

i = WorksheetFunction.CountA(rng_1) + 2

'MsgBox "A" & i

'MsgBox FindLast(3)

'MsgBox "A" & i & ":" & FindLast(3)

Set rng_2 = ws.Range("A" & i & ":" & FindLast(3))

ws.Rows(rng_2).Select
    Selection.Delete Shift:=xlUp

End Sub

FindLast runs a macro that obtains the last cell for the last row with data in it.

The below is A & the variable i combined

1643069860573.png


The below is the results from FindLast(3)

1643069923570.png


when I run MsgBox rng_2.address

1643070469671.png



I appreciate any help.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Is your goal simply to delete the last two lines?
If so, most of this code is unnecessary.

If column B always has data in it, you could just use something like this:
VBA Code:
Sub MyDeleteRows()

    Dim lr As Long
   
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
   
'   Delete rows
    Rows(lr - 1 & ":" & lr).Delete
   
End Sub
 
Upvote 0
Solution
Okay, I couldn't sleep, so I have rebooted my computer and added the mini-sheet addon to excel. As per above, I am trying to automate the manual process of me sorting through over 20 statements a month. This is basically, just having the information we need to receipt.

The steps:

1. Open a workbook that I will disable from being saved.
2. The workbook will have a few buttons to choose from, but at this stage, there is only one button, which will activate a macro.

VBA Project - Dummy File (VBA Code Processed).xlsm
ABCDEFGHIJKL
2
3
4
5
6
7
8
9
10
11
12
13
14
Sheet1


3. The macro will open a box so that I or one of my co-workers is able to select the statement we want to copy the "Data" sheet from.

The code splits the "data" sheet over sheets; Data_Statement_Details and Data. At this stage I am obtaining information from the sheet Data.

VBA Project - Dummy File (VBA Code Processed).xlsm
EFGHIJKLMNOPQRSTUV
1STRPAYDAMTSTRPAYTAMTSTRCOMAMTSTROTHCHAMTSTRGSTAMTSTRCHCRAMTSTRREFAMTSTRTTYCODESTRCHGCATFILCODETRNSRVCHAMTTRNSOLCHAMTTRNCRTCHAMTTRNNOTSRVSOLCRTCHAMTTTYCATTRNGSTEXEMPTAMTFINADDINFO_3FILCLIREF2
20-50-350-3.5PAYTOTH1712007700008100
30503503.5PAYTOTH1706056000008100and legal expenses
4010700.7PAYTOTH1708085900008100
502,020.001,414.000141.4PAYTOTH8364000008100217447
6010700.7PAYTOTH54346000008100
702517.501.75PAYTOTH54890300008100
80201401.4PAYTOTH1708064700008100
904.823.3700.34PAYTOTH54630800008100
100424.96297.47029.75PAYTOTH1709057600008100
11010700.7PAYTOTH54863400008100
12010700.7PAYTOTH55078500008100
130201401.4PAYTOTH55268900008100
140500350035PAYTOTH1709037900008100
15010700.7PAYTOTH54346000008100
16053.500.35PAYTOTH1707023400008100
17010700.7PAYTOTH1711034400008100
18010700.7PAYTOTH55078500008100
190503503.5PAYTOTH1706056000008100and legal expenses
2001007007PAYTOTH1707003000008100
21010700.7PAYTOTH1708085900008100
22010700.7PAYTOTH54346000008100
23010700.7PAYTOTH1711034400008100
2402517.501.75PAYTOTH54890300008100
250201401.4PAYTOTH1708064700008100
26010700.7PAYTOTH55078500008100
27010700.7PAYTOTH54961600008100
2803.022.1100.21PAYTOTH55592200008100
29051.7636.2303.62PAYTOTH54396400008100
30010700.7PAYTOTH54346000008100
310201401.4PAYTOTH55268900008100
32053.500.35PAYTOTH1707023400008100
33010700.7PAYTOTH1708085900008100
34010700.7PAYTOTH55078500008100
35010700.7PAYTOTH54346000008100
360503503.5PAYTOTH1706056000008100and legal expenses
3700002,698.50CLCC00006100
3800000806.06TRRF0000900806.06
data


4. I want to delete the last two rows or not have them included in the dynamic range. The range determined by the code is assigned to a Defined Name.
5. The code then copies a Column from the range A2 and the last row determined by the code. A new sheet is created called receipted and it is copied here from A2.
6. I will add a reference to each column.

VBA Project - Dummy File (VBA Code Processed).xlsm
EFGHIJKLMNOPQRSTUV
1STRPAYDAMTSTRPAYTAMTSTRCOMAMTSTROTHCHAMTSTRGSTAMTSTRCHCRAMTSTRREFAMTSTRTTYCODESTRCHGCATFILCODETRNSRVCHAMTTRNSOLCHAMTTRNCRTCHAMTTRNNOTSRVSOLCRTCHAMTTTYCATTRNGSTEXEMPTAMTFINADDINFO_3FILCLIREF2
20-50-350-3.5PAYTOTH1712007700008100
30503503.5PAYTOTH1706056000008100and legal expenses
4010700.7PAYTOTH1708085900008100
502,020.001,414.000141.4PAYTOTH8364000008100217447
6010700.7PAYTOTH54346000008100
702517.501.75PAYTOTH54890300008100
80201401.4PAYTOTH1708064700008100
904.823.3700.34PAYTOTH54630800008100
100424.96297.47029.75PAYTOTH1709057600008100
11010700.7PAYTOTH54863400008100
12010700.7PAYTOTH55078500008100
130201401.4PAYTOTH55268900008100
140500350035PAYTOTH1709037900008100
15010700.7PAYTOTH54346000008100
16053.500.35PAYTOTH1707023400008100
17010700.7PAYTOTH1711034400008100
18010700.7PAYTOTH55078500008100
190503503.5PAYTOTH1706056000008100and legal expenses
2001007007PAYTOTH1707003000008100
21010700.7PAYTOTH1708085900008100
22010700.7PAYTOTH54346000008100
23010700.7PAYTOTH1711034400008100
2402517.501.75PAYTOTH54890300008100
250201401.4PAYTOTH1708064700008100
26010700.7PAYTOTH55078500008100
27010700.7PAYTOTH54961600008100
2803.022.1100.21PAYTOTH55592200008100
29051.7636.2303.62PAYTOTH54396400008100
30010700.7PAYTOTH54346000008100
310201401.4PAYTOTH55268900008100
32053.500.35PAYTOTH1707023400008100
33010700.7PAYTOTH1708085900008100
34010700.7PAYTOTH55078500008100
35010700.7PAYTOTH54346000008100
360503503.5PAYTOTH1706056000008100and legal expenses
3700002,698.50CLCC00006100
3800000806.06TRRF0000900806.06
data


7. The macro then adds the formula Vlookup to the Sheet "Receipt"
8. The next step is to consolidate the number of transactions on the statement. A lot of the transactions are a couple of payments made by the same client. (This part I am really stuck on as I haven't figured out how to code this yet.

VBA Project - Dummy File (VBA Code Processed).xlsm
ABCD
2295678410-$50.00-$38.50-$11.50
3394308240$50.00$38.50$11.50
4889035494$10.00$7.70$2.30
5939394939$2,020.00$1,555.40$464.60
6848593874$10.00$7.70$2.30
7239394838$25.00$19.25$5.75
838299999$20.00$15.40$4.60
9675768476$4.82$3.71$1.11
10545496854$424.96$327.22$97.74
11111111111$10.00$7.70$2.30
12545485848$10.00$7.70$2.30
13485848584$20.00$15.40$4.60
14340145338$500.00$385.00$115.00
15848593874$10.00$7.70$2.30
16222222222$5.00$3.85$1.15
17768576757$10.00$7.70$2.30
18545485848$10.00$7.70$2.30
19394308240$50.00$38.50$11.50
2099495848$100.00$77.00$23.00
21889035494$10.00$7.70$2.30
22848593874$10.00$7.70$2.30
23768576757$10.00$7.70$2.30
24239394838$25.00$19.25$5.75
2538299999$20.00$15.40$4.60
26545485848$10.00$7.70$2.30
27909945984$10.00$7.70$2.30
28359489584$3.02$2.32$0.70
29878574757$51.76$39.85$11.91
30848593874$10.00$7.70$2.30
31485848584$20.00$15.40$4.60
32222222222$5.00$3.85$1.15
33889035494$10.00$7.70$2.30
34545485848$10.00$7.70$2.30
35848593874$10.00$7.70$2.30
36394308240$50.00$38.50$11.50
Receipt
Cell Formulas
RangeFormula
B2:B36B2=VLOOKUP(A2,Statement_Data,6,FALSE)
C2:C36C2=VLOOKUP(A2,Statement_Data,7,FALSE) + VLOOKUP(A2,Statement_Data,9,FALSE)
D2:D36D2=B2-C2
Named Ranges
NameRefers ToCells
Statement_Data=data!$A$2:$V$38B2:C36


8. Once the data is consolidated, I delete all the rows from the 1st empty row of the consolidated data and copy and paste special the consolidated Data from A2. I then place the remaining information in a form for receipting. The goal is one the Data is copied from the statement then I end up with the below and a new workbook is created with the below.

VBA Project - Dummy File (VBA Code Processed).xlsm
ABCDEFGH
1Claim NumberPaymentInc CommNet to QBEInv NumberBatch NumDate ReceiptedReceipt Number
2038299999$40.00$30.80$9.207060
3099495848$100.00$77.00$23.007060
4111111111$10.00$7.70$2.307060
5222222222$10.00$7.70$2.307060
6239394838$50.00$38.50$11.507060
7295678410-$50.00-$38.50-$11.507060
8340145338$500.00$385.00$115.007060
9359489584$3.02$2.32$0.707060
10394308240$150.00$115.50$34.507060
11485848584$40.00$30.80$9.207060
12545485848$40.00$30.80$9.207060
13545496854$424.96$327.22$97.747060
14675768476$4.82$3.71$1.117060
15768576757$20.00$15.40$4.607060
16848593874$50.00$38.50$11.507060
17878574757$51.76$39.85$11.917060
18889035494$30.00$23.10$6.907060
19909945984$10.00$7.70$2.307060
20939394939$2,020.00$1,555.40$464.607060
21$3,504.56$2,698.50$806.06
Goal
Cell Formulas
RangeFormula
E2:E20E2=Data_Statement_Details!$B$37
B21:D21B21=SUM(B2:B20)



:( I am not sure if the code I pasted above will show up:( in what I have entered.
 
Upvote 0
That is asking much more than was implied in your first post.
Your original question seem focused on one particular issue, not the whole entire project.
I was willing to try to help out on that one particular issue you originally asked about, but probably don't have time to commit myself to helping with the larger project.
 
Upvote 0
All good, I figured out the issue with deleting the two rows. I have everything else working accept for the consolidating the data. Sorry, I didn’t mean to imply that I needed help on the whole thing. I am just the type of person that explain what my end goal is and when I posted this post it was deleting the two rows.
 
Upvote 0
I have everything else working accept for the consolidating the data.
Glad to hear that you got that part working.
Since the data consolidation is different than your original question, if you would like help with that, it may be best to post that as its own new question.
That way it will appear in the "Unanswered threads", increaing the likelihood of responses and help.
 
Upvote 0
Thank you for all the help, I was able to Delete the last two rows:)
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

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