Macro with different cell ranges everyday

cdalgorta

Board Regular
Joined
Jun 5, 2022
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi. Probably a macro beginners question. Apologies and thanks in advance.
So everyday I download "report A", the cells range are different because things get bought/sold. Ex: A3:L900 and the next day is A3:L1000.
*Simple example:
Day 1: Macro starts recording on a sheet with data range A3:L900
- I insert a new column B
- I type a function in cell B4[=IFERROR(LEFT(A4,FIND("_",A4)-1),A4)] and click on the corner of the cell B4 so it copies the function for all the rest of the rows in column B until B900
- Stop recording

Day 2: I run the same Macro on a sheet with data range A3:L1000:
-I run the macro and it only copies the function for cells B4:B900 and not for the rest B901:B1000

Question: What should I have done so the function I recorded on Day 1, also worked on B901:B1000 on Day 2? And hopefully also work on B901:B100000 in Day 500 in the future?
Hope I didn't confuse anyone and thanks in advance once again.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Use a variable to hold the number of the last row:
VBA Code:
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range ("A3:L" & LastRow)
 
Upvote 0
Use a variable to hold the number of the last row:
VBA Code:
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range ("A3:L" & LastRow)
Hi, apologies for the late response. Family emergency :(
Back to your response. I'm extremely new to VBA. I tried playing around with what you said and watched some videos, but could not make it work. Could you please tell me what exactly would I have to add/delete in the uploaded "VBA example" to make the "test sample" work even if it had more than 15 rows? Thank you very much in advance!

VBA:
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range ("A3:L" & LastRow)

Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(2, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "Invoice"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=IFERROR(LEFT(RC[-1],FIND(""_"",RC[-1])-1),RC[-1])"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A12")
ActiveCell.Range("A1:A12").Select
End Sub


Test Sample with 15 rows.xlsx
ABC
1
2Hello
3invoicecostrevenue
41231216
51251216
6124_11317
7124_21519
81271923
91281721
10124_31620
111282428
12124_71216
131272529
14124_55862
151254549
16
Hello
 

Attachments

  • Before Macro - Test Sample with 15 rows.JPG
    Before Macro - Test Sample with 15 rows.JPG
    25.8 KB · Views: 8
  • VBA example.JPG
    VBA example.JPG
    40.5 KB · Views: 8
  • After Macro - Test Sample with 15 rows.JPG
    After Macro - Test Sample with 15 rows.JPG
    35 KB · Views: 8
Upvote 0
Start with your sheet looking like this:
Book1
ABC
1invoicecostrevenue
21231216
31251216
4124_11317
5124_21519
61271923
71281721
8124_31620
91282428
10124_71216
111272529
12124_55862
131254549
Sheet1

Then try this macro:
VBA Code:
Sub cdalgorta()
    Application.ScreenUpdating = False
    Dim v1 As Variant, v2 As Variant, i As Long, j As Long
    v1 = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
    ReDim v2(1 To UBound(v1), 1 To 1)
    For i = 1 To UBound(v1)
        If InStr(v1(i, 1), "_") > 0 Then
            j = j + 1
            v2(j, 1) = Split(v1(i, 1), "_")(0)
        Else
            j = j + 1
            v2(j, 1) = v1(i, 1)
        End If
    Next i
    Range("A2").Resize(UBound(v2)) = v2
End Sub
There is no longer any need for a formula.
 
Upvote 0
Start with your sheet looking like this:
Book1
ABC
1invoicecostrevenue
21231216
31251216
4124_11317
5124_21519
61271923
71281721
8124_31620
91282428
10124_71216
111272529
12124_55862
131254549
Sheet1

Then try this macro:
VBA Code:
Sub cdalgorta()
    Application.ScreenUpdating = False
    Dim v1 As Variant, v2 As Variant, i As Long, j As Long
    v1 = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
    ReDim v2(1 To UBound(v1), 1 To 1)
    For i = 1 To UBound(v1)
        If InStr(v1(i, 1), "_") > 0 Then
            j = j + 1
            v2(j, 1) = Split(v1(i, 1), "_")(0)
        Else
            j = j + 1
            v2(j, 1) = v1(i, 1)
        End If
    Next i
    Range("A2").Resize(UBound(v2)) = v2
End Sub
There is no longer any need for a formula.
Thank you! The thing is that I would need the way to do it with the macro on my example, so I can use it as reference. Because that was just a sample. In the real thing for work, I need to use dozens of different formulas for hundreds of columns and thousands of rows. The above would only help me with 1 of the formulas, but not with the rest.
If you could please help me to know what I have to add/delete in the sample I gave, it would be perfect. Thank you again 🙏

VBA:
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range ("A3:C" & LastRow)

Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(2, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "Invoice"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=IFERROR(LEFT(RC[-1],FIND(""_"",RC[-1])-1),RC[-1])"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A12")
ActiveCell.Range("A1:A12").Select
End Sub
 
Upvote 0
It is hard to suggest a solution without seeing your actual file. How many formulae are you using? Does each formula perform the same function as the one you posted? It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet (de-sensitized if necessary). Alternately, you could upload a copy of your file which includes all the formulae, to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data. I wouldn't need the entire sheet, only a dozen or so rows of data should be enough.
 
Upvote 0
See if this helps:
This is making as few changes as possible to what you had.
Normally, you would go through and remove all occurences of "Select" and "Activate". They are unnecessary and they make the code slow and hard to follow.

VBA Code:
Sub FillDownFormula()

    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    'XXX Range ("A3:C" & LastRow)   ' XXX errors out
   
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    ActiveCell.Offset(2, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Invoice"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(LEFT(RC[-1],FIND(""_"",RC[-1])-1),RC[-1])"
    ActiveCell.Select
    ' XXX Change this next line
    'Selection.AutoFill Destination:=ActiveCell.Range("A1:A12")
    ' XXX To this
    Selection.AutoFill Destination:=Range(ActiveCell, Range("B" & LastRow))
   
    ' XXX Remove this
    'ActiveCell.Range("A1:A12").Select
End Sub
 
Upvote 0
Solution
See if this helps:
This is making as few changes as possible to what you had.
Normally, you would go through and remove all occurences of "Select" and "Activate". They are unnecessary and they make the code slow and hard to follow.

VBA Code:
Sub FillDownFormula()

    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    'XXX Range ("A3:C" & LastRow)   ' XXX errors out
  
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    ActiveCell.Offset(2, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Invoice"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(LEFT(RC[-1],FIND(""_"",RC[-1])-1),RC[-1])"
    ActiveCell.Select
    ' XXX Change this next line
    'Selection.AutoFill Destination:=ActiveCell.Range("A1:A12")
    ' XXX To this
    Selection.AutoFill Destination:=Range(ActiveCell, Range("B" & LastRow))
  
    ' XXX Remove this
    'ActiveCell.Range("A1:A12").Select
End Sub
omg! This worked perfectly. Thank you so much!!!!! 🙏🙏🙏🙏🙏🙏😭
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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