如何在 excel 中获取调用函数的值

How to get a value to call a function in excel

我正在尝试获取一个值 return 到 运行 Excel

中的一个调用函数

Sub ID_Num 是我想在 Sub SubmitButton 的第一个 ws.range 中调用的内容,但我不知道如何调用函数,所以我尝试从中提取代码功能如下图。这工作一次并添加一个数字但之后它停止 - 与 ActiveCell.Row 部分有关

ws.Range("A" & LastRow).Value = "=MAX(A2:A" + CStr(ActiveCell.Row - 1) + ")+1"    'Adds the ID into Col A & Last Blank Row"

我当前的代码是:

Sub ID_Num()

 Dim LastRow As Long, ws As Worksheet

Set ws = Sheets("Data")

ActiveCell.Formula = "=MAX(A1:A" + CStr(ActiveCell.Row - 1) + ")+1"

ActiveCell.Formula = ActiveCell.Value 'Convert formula to a value

End Sub



Sub SubmitButton()

Dim LastRow As Long, ws As Worksheet

Set ws = Sheets("Data")

LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row

ws.Range("A" & LastRow).Value = "=MAX(A2:A" + CStr(ActiveCell.Row - 1) + ")+1"    'Adds the ID into Col A & Last Blank Row"
ws.Range("B" & LastRow).Value = Environ("userdomain") & "\" & Environ("username") 'Adds the UserName into Col B & Last Blank Row
ws.Range("C" & LastRow).Value = Format(Now(), "dd/mm/yyyy hh:mm") 'Adds the Date_Time into Col C & Last Blank Row
ws.Range("D" & LastRow).Value = Sheets("Form").Range("C5") 'Adds the Name into Col D & Last Blank Row
ws.Range("E" & LastRow).Value = Sheets("Form").Range("C7") 'Adds the Service into Col E & Last Blank Row
ws.Range("F" & LastRow).Value = Sheets("Form").Range("F7") 'Adds the Team into Col F & Last Blank Row
ws.Range("G" & LastRow).Value = Sheets("Form").Range("C9") 'Adds the Cycle into Col G & Last Blank Row
ws.Range("H" & LastRow).Value = Sheets("Form").Range("F5") 'Adds the Date into Col H & Last Blank Row
ws.Range("I" & LastRow).Value = Sheets("Form").Range("C11") 'Adds the Section into Col I & Last Blank Row
ws.Range("J" & LastRow).Value = Sheets("Form").Range("C13") 'Adds the Functionality into Col J & Last Blank Row
ws.Range("K" & LastRow).Value = Sheets("Form").Range("C15") 'Adds the Form into Col K & Last Blank Row
ws.Range("L" & LastRow).Value = Sheets("Form").Range("C17") 'Adds the Report into Col L & Last Blank Row
ws.Range("M" & LastRow).Value = Sheets("Form").Range("C19") 'Adds the System1_ID into Col M & Last Blank Row
ws.Range("N" & LastRow).Value = Sheets("Form").Range("F19") 'Adds the System2_ID into Col N & Last Blank Row
ws.Range("O" & LastRow).Value = Sheets("Form").Range("B22") 'Adds the Comment into Col O & Last Blank Row


End Sub

如有任何帮助,我们将不胜感激。

CStr(ActiveCell.Row - 1)替换为CStr(LastRow - 1)