簡體   English   中英

基於單元格值的參考工作表

[英]Reference worksheet based on the cell value

我正在使用此網站上的數據表單: http : //www.contextures.com/exceldataentryupdateform.html請參閱工作簿下載“數據輸入表單-添加/更新”。 它是帶有工作宏的簡單數據輸入表單,非常好。 目前,在“輸入”表中進行任何更改時,只有PartsData表會更新。 但是,我希望“輸入”頁面可以更新以“零件”字段命名的表單之一(輸入表單中的D6)中的數據,例如“門”,“鏡片”,“黑帽”。 顯然,我可以使用這些部分創建這些工作表和相關數據。

我只想使用與在“輸入”頁面中選擇的“零件”字段相同的名稱來更新工作表。

下面是部分代碼。 我只是不知道如何調整它,以使Set historyWks = Worksheets("PartsData") )在輸入表中成為單元格引用(D6)。 其余代碼工作得很好,因此只需要更改Set historyWks = Worksheets("PartsData") 有任何想法嗎?

Sub UpdateLogWorksheet()

Dim historyWks As Worksheet
Dim inputWks As Worksheet

Dim nextRow As Long
Dim oCol As Long

Dim myCopy As Range
Dim myTest As Range

Dim lRsp As Long

Set inputWks = Worksheets("Input")
Set historyWks = Worksheets("PartsData")
oCol = 3 'order info is pasted on data sheet, starting in this column

'check for duplicate order ID in database
If inputWks.Range("CheckID") = True Then
  lRsp = MsgBox("Order ID already in database. Update record?", vbQuestion + vbYesNo, "Duplicate ID")
  If lRsp = vbYes Then
    UpdateLogRecord
  Else
    MsgBox "Please change Order ID to a unique number."
  End If
Else
  'cells to copy from Input sheet - some contain formulas
  Set myCopy = inputWks.Range("OrderEntry")

  With historyWks
      nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
  End With

  With inputWks
      'mandatory fields are tested in hidden column
      Set myTest = myCopy.Offset(0, 2)

      If Application.Count(myTest) > 0 Then
          MsgBox "Please fill in all the cells!"
          Exit Sub
      End If
  End With

  With historyWks
      'enter date and time stamp in record
      With .Cells(nextRow, "A")
          .Value = Now
          .NumberFormat = "mm/dd/yyyy hh:mm:ss"
      End With
      'enter user name in column B
      .Cells(nextRow, "B").Value = Application.UserName
      'copy the order data and paste onto data sheet
      myCopy.Copy
      .Cells(nextRow, oCol).PasteSpecial Paste:=xlPasteValues, Transpose:=True
      Application.CutCopyMode = False
  End With

  'clear input cells that contain constants
  ClearDataEntry
End If

End Sub

將該行從Set historyWks = Worksheets("PartsData")更改為Set historyWks = Worksheets(Range("D6").Value)這將使用D6中的值來設置工作表。

聲明一個新字符串以保存目標工作表的名稱,然后將historyWks設置為該字符串。

dim strWksTarget as string
strWksTarget = sheets("Input").Range("D6")
set historyWks = sheets(strWksTarget)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM