0

Currently, I'm trying to code a worksheet to be a logbook. the logs will be on sheet "Log_Master". I have a button that I will attach to my macros to run the script that will take selected information from "Log_Master" to another page named "Summary" with a table also named "summary". At the click of the button (Shift Change), I want information in certain cells to be written to the summary table. Each time the button is pressed I want the information to write to the next row on the table. I already have the sheet "Summary" and the table "Summary" being created via TypeScript (I can send code if needed). Could I complete this task in typeScript?

Some of the information is also in tables. one named "Runtime" and one named "Levels.

Then, I want to do some other functions that I will focus on after I complete this task.

So far, I have successfully moved the data to the other table via copy and paste code:

 Sub Autofill_Summary()
'
' autofill_summary Macro
'

'
 Sheets("Log_Master").Select
    Range("levels[[Silo 1]:[Water Level]]").Select
    Selection.Copy
    Sheets("Summary").Select
    Range("summary[Silo 1]").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   Sheets("Log_Master").Select
     Range("runtime[[Runtime]:[Ave Moist]]").Select
    Selection.Copy
    Sheets("Summary").Select
    Range("summary[Runtime]").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   Sheets("Log_Master").Select
    Range("L1").Select
    Selection.Copy
    Sheets("Summary").Select
    Range("summary[Date]").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   Sheets("Log_Master").Select
    Range("N1").Select
    Selection.Copy
    Sheets("Summary").Select
    Range("summary[Shift]").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   Sheets("Log_Master").Select
    Range("P1").Select
    Selection.Copy
    Sheets("Summary").Select
    Range("summary[Name]").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   Sheets("Log_Master").Select
    Range("C2").Select
End Sub

If this method is acceptable, how would i make it to where each time the button is selected it runs the macros and paste the information to the next row on sheet "Summary" in table "Summary"?

I also successfully moved the cell values to the "Summary" sheet but failed to land the data in the table and failed to get the information to populate in the next row. code:

Sub autoFill_Trial()

   Dim A As String
   Dim B As String
   Dim C As String
   Dim D As String
   Dim E As String
   Dim F As String
   Dim G As String
   Dim H As String
   Dim I As String
   Dim J As String
   Dim K As String
   Dim L As String


   A = Sheets("Log_Master").Cells(1, 12).Value
   B = Sheets("Log_Master").Cells(1, 14).Value
   C = Sheets("Log_Master").Cells(1, 16).Value
   D = Sheets("Log_Master").Cells(45, 6).Value
   E = Sheets("Log_Master").Cells(45, 7).Value
   F = Sheets("Log_Master").Cells(45, 8).Value
   G = Sheets("Log_Master").Cells(45, 9).Value
   H = Sheets("Log_Master").Cells(47, 6).Value
   I = Sheets("Log_Master").Cells(47, 7).Value
   J = Sheets("Log_Master").Cells(47, 8).Value
   K = Sheets("Log_Master").Cells(47, 9).Value
   L = Sheets("Log_Master").Cells(47, 10).Value



  lastRow = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row + 1
   Sheets("Summary").Cells(lastRow + 1).Value = A
   Sheets("Summary").Cells(lastRow + 2).Value = B
   Sheets("Summary").Cells(lastRow + 3).Value = C
   Sheets("Summary").Cells(lastRow + 4).Value = D
   Sheets("Summary").Cells(lastRow + 5).Value = E
   Sheets("Summary").Cells(lastRow + 6).Value = F
   Sheets("Summary").Cells(lastRow + 7).Value = G
   Sheets("Summary").Cells(lastRow + 8).Value = H
   Sheets("Summary").Cells(lastRow + 9).Value = I
   Sheets("Summary").Cells(lastRow + 10).Value = J
   Sheets("Summary").Cells(lastRow + 11).Value = K
   Sheets("Summary").Cells(lastRow + 12).Value = L

  Sheets("Log_Master").Select
   Range("C2").Select
End Sub

If someone could help me I would GREATLY appreciate it!

EDIT::::: Adding the typeScript for "Summary" table

   function main(workbook: ExcelScript.Workbook) {
    // Your code here
    //set names for tables to pull info from sheet Log_Master 
   tbl1=runtime tbl2=levels tblsmy=summary tblmas=runmaster
    let tbl1 = workbook.getTable('runtime')
    let tbl2 = workbook.getTable('levels')
    let tblsmy = workbook.getTable('summary')
    let tblmas = workbook.getTable('runmaster')
    // look for "Summary" worksheet, delete if it's present.
    workbook.getWorksheet('Summary')?.delete();
    // Create a new worksheet named "Summary" for the combined table.
    const newSheet = workbook.addWorksheet('Summary');
    //Assign the "Summary" worksheet to the smy variable
    let smy = workbook.getWorksheet('Summary');
    // Add a new table at range B1 on summary (smy)
    let newTable = workbook.addTable(smy.getRange("B1:M1"), false);
    // Set range B1:L1 on summary (smy)
  smy.getRange("B1:M1").setValues([["Date", "Shift", "Name", "Runtime", "Downtime", "Fineness", "Moisture", "Silo 1", "Silo 2", "Propane 1", "Propane 2", "Water Level"]]);
    // Rename table newTable to "summary" (tblsmy)
    newTable.setName("summary");
    // Auto fit the columns of range E:L on summary (smy)
    //custom size B:D,  Set Date Format (B) set 
     Percent Format (G:M),
    let dateRange=smy.getRange("B:B");
    dateRange.setNumberFormat("m/d/yyyy;@");
    let dtRange=smy.getRange("F:F");
    dtRange.setNumberFormat("HH:MM")
    let percentRange=smy.getRange("G:M");
    percentRange.setNumberFormat("0.00%");
    smy.getRange("E:M").getFormat().autofitColumns();
    smy.getRange("B:D").getFormat().setColumnWidth(76);    
    smy.getFreezePanes().freezeAt(smy.getRange("1:1"));
 ````
   
braX
  • 11,506
  • 5
  • 20
  • 33
IDONTKNOW
  • 1
  • 1
  • Side note: it`s usually a good idea to [avoid using Select](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) in your code. – cybernetic.nomad Apr 06 '23 at 20:28
  • Instead of declaring `A,B,C,D,...` I suggest declaring an array `Dim Values(1 To 12) As String` and then you can save all the values into the array `Values` like `Values(1) = Sheets("Log_Master").Cells(1, 12).Value` – Toddleson Apr 06 '23 at 20:33
  • Also, you can directly assign multi-cell ranges to each other to pass over a set of values. To replace `D,E,F,G` you could do `Sheets("Summary").Cells(lastRow + 4).Resize(4).Value = Sheets("Log_Master").Cells(45, 6).Resize(4).Value` and similar lines for the other two ranges. – Toddleson Apr 06 '23 at 20:36
  • `Cells(lastRow + 1)` is missing the argument for `Column`. By adding +1, +2, +3, you're not moving to a new column, you're moving to a new row. I think this might be your issue. If meant to put all the data into one row, you need to be using the `Column` argument like `Cells(lastRow, 1)` and `Cells(lastRow, 2)` etc. – Toddleson Apr 06 '23 at 20:41
  • You guys are fast! @cybernetic.nomad got it avoid Select! Thanks you for the info – IDONTKNOW Apr 06 '23 at 20:45
  • @Toddelson so create my dim values as I have with 1 to 12 then declare them an array using the code you added? – IDONTKNOW Apr 06 '23 at 20:45
  • To ensure the data goes into the table, you can interact with the table object, and use it to find the destination range, instead of `lastRow`. Example: `Dim NewRow as ListRow: Set NewRow = Sheets("Summary").ListObjects("summary").ListRows.Add` and then you can do `NewRow.Range.Cells(1,1).Value = ...` this would be the first cell of the new row, and you can increment the columns to get the other cells. – Toddleson Apr 06 '23 at 20:47
  • 1
    In response to your question, I think you should avoid declaring multiple variables when they are all used for similar purposes, in this case, they fit well as elements of an array. See [this article](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/using-arrays#storing-variant-values-in-arrays) for how to use arrays in VBA. – Toddleson Apr 06 '23 at 20:52
  • @Toddleson I am currently trying to correct the issues you pointed out. However, each time I run the macros I want it to put the data in the next row of the table. I corrected the Cells(lastRow + 1). it move the information over to the table but only to the first row of the table – IDONTKNOW Apr 06 '23 at 21:11
  • Edit your post to add the new code, I'll take a look but my work-day is almost over. But I'm sure others can provide advice if you ask. – Toddleson Apr 06 '23 at 21:50
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Apr 06 '23 at 22:53

0 Answers0