+ Reply to Thread
Results 1 to 17 of 17

Auto ranging my automate script in Excel

  1. #1
    Registered User
    Join Date
    03-19-2024
    Location
    Los Angeles
    MS-Off Ver
    Microsoft 365 Enterprise
    Posts
    8

    Auto ranging my automate script in Excel

    Hello,

    I am trying to automate a monthly process where I am receiving a set of data. The column width is the same, but rows can vary drastically. I automated a formula in the last column, and I am duplicating it for every row, but the formula is being pulled to the end of the sheet, instead of to the last row that has data. How can I stop the code from going past the last row containing data? And I am trying to accomplish this using the automate tab in Excel.

    Thank you

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Auto ranging my automate script in Excel

    Post the code.


    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Auto ranging my automate script in Excel

    Maybe getlastrow

    https://learn.microsoft.com/en-us/ja...trow-member(1)

  4. #4
    Registered User
    Join Date
    03-19-2024
    Location
    Los Angeles
    MS-Off Ver
    Microsoft 365 Enterprise
    Posts
    8

    Re: Auto ranging my automate script in Excel

    For whatever reason, its not letting me upload the file. I followed the instructions, and selected the file, but it just does not appear anywhere. Perhaps I am being blocked from attaching or uploading?

    But here is a screen grab. My issue essentially lies in the highlighted line. I need that to stop at the last row that contains data. I ve been told this is possible, just not how exactly.

    Attachment 863532

    function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();
    // Set fill color to #B4C6E7 for range I1 on selectedSheet
    selectedSheet.getRange("I1").getFormat().getFill().setColor("#B4C6E7");
    // Set range I1:I2 on selectedSheet
    selectedSheet.getRange("I1:I2").setFormulasLocal([["Movement to Book"],["=G2-H2"]]);
    // Paste to extended range obtained by extending down from range I2 on selectedSheet from range I2 on selectedSheet
    selectedSheet.getRange("I2").getExtendedRange(ExcelScript.KeyboardDirection.down).copyFrom(selectedSheet.getRange("I2"), ExcelScript.RangeCopyType.formulas, false, false);
    // Add a new worksheet
    let sheet3 = workbook.addWorksheet();
    // Add a new pivot table on sheet3
    let newPivotTable = workbook.addPivotTable("PivotTable4", selectedSheet.getRange("A1:I1048576"), sheet3.getRange("A3"));
    // This action currently can't be recorded.
    // Add pivot field to a hierarchy in newPivotTable
    newPivotTable.addRowHierarchy(newPivotTable.getHierarchy("Co Code"));
    // Add pivot field to a hierarchy in newPivotTable
    newPivotTable.addRowHierarchy(newPivotTable.getHierarchy("PRFT_CENTER_ID"));
    // Add pivot field to a hierarchy in newPivotTable
    newPivotTable.addDataHierarchy(newPivotTable.getHierarchy("Movement to Book"));

    Thank you
    }

  5. #5
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Auto ranging my automate script in Excel

    Does this work for you?

    PHP Code: 
    function main(workbookExcelScript.Workbook) {
        
    let selectedSheet workbook.getActiveWorksheet();
        
    // Set fill color to #B4C6E7 for range I1 on selectedSheet
        
    selectedSheet.getRange("I1").getFormat().getFill().setColor("#B4C6E7");
        
    // Set range I1:I2 on selectedSheet
        
    selectedSheet.getRange("I1:I2").setFormulasLocal([["Movement to Book"], ["=G2-H2"]]);

        
    let lr selectedSheet.getRange("G1").getSurroundingRegion().getLastCell();

        
    let TheRange"I3:"+lr.getAddress();
        
    // Paste to extended range obtained by extending down from range I2 on selectedSheet from range I2 on selectedSheet

        
    selectedSheet.getRange(TheRange).copyFrom(selectedSheet.getRange("I2"), ExcelScript.RangeCopyType.formulasfalsefalse);


    // etc etc

    Last edited by ByteMarks; 03-21-2024 at 06:39 AM.

  6. #6
    Banned User!
    Join Date
    03-11-2024
    Location
    usa
    MS-Off Ver
    CURRENT
    Posts
    23

    Re: Auto ranging my automate script in Excel

    Sub AutomaticRanking()
    Dim ws As Worksheet
    Dim lastRow As Long

    ' Specify the sheet on which you want to perform automation
    Set ws = ThisWorkbook.Sheets("Sheet_name")

    ' Determine the last row with data in column A (you can select any other column)
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    ' Your automation code you want to execute

    ' Example:
    For i = 2 To lastRow ' Assume that the first row contains headers
    'Your code is here
    Next i
    End Su
    This code identifies the last row of data in column A and runs your automation only up to that row. Replace ""SheetName" with the name of the sheet you want to automate on, and change the row and column range accordingly if needed.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Auto ranging my automate script in Excel

    @CarissaKirby: the OP is using ExcelScript, not VBA.

    I missed this:
    And I am trying to accomplish this using the automate tab in Excel.

  8. #8
    Registered User
    Join Date
    03-19-2024
    Location
    Los Angeles
    MS-Off Ver
    Microsoft 365 Enterprise
    Posts
    8

    Re: Auto ranging my automate script in Excel

    Quote Originally Posted by ByteMarks View Post
    Does this work for you?

    PHP Code: 
    function main(workbookExcelScript.Workbook) {
        
    let selectedSheet workbook.getActiveWorksheet();
        
    // Set fill color to #B4C6E7 for range I1 on selectedSheet
        
    selectedSheet.getRange("I1").getFormat().getFill().setColor("#B4C6E7");
        
    // Set range I1:I2 on selectedSheet
        
    selectedSheet.getRange("I1:I2").setFormulasLocal([["Movement to Book"], ["=G2-H2"]]);

        
    let lr selectedSheet.getRange("G1").getSurroundingRegion().getLastCell();

        
    let TheRange"I3:"+lr.getAddress();
        
    // Paste to extended range obtained by extending down from range I2 on selectedSheet from range I2 on selectedSheet

        
    selectedSheet.getRange(TheRange).copyFrom(selectedSheet.getRange("I2"), ExcelScript.RangeCopyType.formulasfalsefalse);


    // etc etc

    Um Dude! Thank you!!! Worked like a charm, i just copied and pasted. So if I want to apply the same principal to another worksheet, I just have to modify the code there as well? I mean in theory, it should work for any worksheet within the same parameters?

  9. #9
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Auto ranging my automate script in Excel

    Great. It references the active worksheet so it should indeed work for any worksheet within the same parameters.

  10. #10
    Registered User
    Join Date
    03-19-2024
    Location
    Los Angeles
    MS-Off Ver
    Microsoft 365 Enterprise
    Posts
    8

    Re: Auto ranging my automate script in Excel

    Hey ByteMarks. I tried to use this code in a following worksheet, but realized its slightly different. Hoping you can advise. The code below is first modifying the first row with some formulas, but then once again pulling the formula down (to the 4000th line, instead of to the end), but I also want it to stop when there is no more data to pull. Except this is pulling from a neighboring worksheet, so thus it needs to detect that the line is blank on the other worksheet. I am putting current coding below:

    function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();
    // Set range A4 on selectedSheet
    selectedSheet.getRange("A4").setFormulaLocal("=IF('Insights Pivot'!C5<0,40,50)");
    // Set range D4:E4 on selectedSheet
    selectedSheet.getRange("D4:E4").setFormulasLocal([["=ABS('Insights Pivot'!C5)","='Insights Pivot'!B5"]]);
    // Set range H4 on selectedSheet
    selectedSheet.getRange("H4").setFormulaLocal("='Insights Pivot'!A5");
    // Set range P4 on selectedSheet
    selectedSheet.getRange("P4").setFormulaLocal("=IF('Insights Pivot'!C5<0,50,40)");
    // Paste to range A4:R4000 on selectedSheet from range A4:R4 on selectedSheet
    selectedSheet.getRange("A4:R4000").copyFrom(selectedSheet.getRange("A4:R4"), ExcelScript.RangeCopyType.all, false, false);
    }

    Thanks

  11. #11
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Auto ranging my automate script in Excel

    See if this works for you.
    I added a bit at the start so that it won't run if the selected sheet is the same as the one being looked up.

    PHP Code: 
    function main(workbookExcelScript.Workbook) {
        
    let wsInsights  workbook.getWorksheet("Insights Pivot");
        
    let selectedSheet workbook.getActiveWorksheet();

        
    //Don't run if can't find insights worksheet
        
    if (wsInsights == undefined){
          
    console.log("'Insights Pivot' sheet not found");
          return;
       
        
    //Don't run if select sheet is the same as the lookup
        
    }else if (selectedSheet.getName() == wsInsights.getName()) {
            
    console.log("Selected sheet is the same as insights");
            return;
        }

        
    //Carry on
        // Set range A4 on selectedSheet
        
    selectedSheet.getRange("A4").setFormulaLocal("=IF('Insights Pivot'!C5<0,40,50)");
        
    // Set range D4:E4 on selectedSheet
        
    selectedSheet.getRange("D4:E4").setFormulasLocal([["=ABS('Insights Pivot'!C5)""='Insights Pivot'!B5"]]);
        
    // Set range H4 on selectedSheet
        
    selectedSheet.getRange("H4").setFormulaLocal("='Insights Pivot'!A5");
        
    // Set range P4 on selectedSheet
        
    selectedSheet.getRange("P4").setFormulaLocal("=IF('Insights Pivot'!C5<0,50,40)");
        
        
    //Get the lastrow from the insights sheet
        
    let lr wsInsights.getRange("C5").getSurroundingRegion().getLastCell().getRowIndex()+1;
        
    //Create a range using the last row
        
    let TheRange "A4:R"+lr;
        
    // Paste to range A4:R? on selectedSheet from range A4:R4 on selectedSheet
        
    selectedSheet.getRange(TheRange).copyFrom(selectedSheet.getRange("A4:R4"), ExcelScript.RangeCopyType.allfalsefalse);

    Last edited by ByteMarks; 03-25-2024 at 07:57 AM.

  12. #12
    Registered User
    Join Date
    03-19-2024
    Location
    Los Angeles
    MS-Off Ver
    Microsoft 365 Enterprise
    Posts
    8

    Re: Auto ranging my automate script in Excel

    Bytemarks your spot on man. Code worked, almost perfectly. Its just using the last blank row, instead of stopping before that. But its so close to perfect, im not complaining!!! Thank you once again.

  13. #13
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Auto ranging my automate script in Excel

    Great.
    You could try not adding the 1 at the end of this line and see if that fixes it.

    PHP Code: 
     //Get the lastrow from the insights sheet
        
    let lr wsInsights.getRange("C5").getSurroundingRegion().getLastCell().getRowIndex() + 1

  14. #14
    Registered User
    Join Date
    03-19-2024
    Location
    Los Angeles
    MS-Off Ver
    Microsoft 365 Enterprise
    Posts
    8

    Re: Auto ranging my automate script in Excel

    Quote Originally Posted by ByteMarks View Post
    Great.
    You could try not adding the 1 at the end of this line and see if that fixes it.

    PHP Code: 
     //Get the lastrow from the insights sheet
        
    let lr wsInsights.getRange("C5").getSurroundingRegion().getLastCell().getRowIndex() + 1
    Worked like a charm! Thank you once again. Is this where i write SOLVED?

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Auto ranging my automate script in Excel

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  16. #16
    Registered User
    Join Date
    03-19-2024
    Location
    Los Angeles
    MS-Off Ver
    Microsoft 365 Enterprise
    Posts
    8

    Re: Auto ranging my automate script in Excel

    Yep did both. Thank you

  17. #17
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Auto ranging my automate script in Excel

    Glad I could help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Automate menu / office script and VBA
    By scottiex in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-29-2023, 11:32 PM
  2. VBA script excel for automate sending sms to a API
    By rodryg123 in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 10-07-2020, 05:30 AM
  3. Replies: 2
    Last Post: 01-04-2018, 09:40 AM
  4. Trying to create and auto-ranging goal seek macro, with a variable user input.
    By Spreadsheet_Bomber in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-21-2016, 06:04 AM
  5. Most efficient way to script to automate a long process?
    By Arcaklar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-21-2013, 11:55 PM
  6. VB Script to automate Excel Creation: Problem
    By usefuldata in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-12-2011, 08:31 AM
  7. VB Script to automate Excel does not run as Sheduled Task
    By dan artuso in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2006, 03:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1