简体   繁体   中英

Reference worksheet based on the cell value

I'm using a data form from this website: http://www.contextures.com/exceldataentryupdateform.html See the workbook download "Data Entry Form - Add/Update". It is a simple data entry form with working macros and it is great. At the moment only PartsData sheet updates when you do any changes in the Input sheet. However, I would like the Input page to then update data in one of the sheets named after the "Parts" field (D6 in Input sheet) eg "Door", "Lens", "Black cap". Obviously I can create these sheets and relevant data with these parts.

I only want to update the sheet with the name the same as the "Parts" field selected in the Input page.

Part of the code is below. I just don't know how to adjust it so that Set historyWks = Worksheets("PartsData") makes a cell reference (D6) in the Input sheet. The rest of the code works perfectly fine so it's only about changing Set historyWks = Worksheets("PartsData") . Any ideas?

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中的值来设置工作表。

Declare a new string to hold the name of the target worksheet and then set historyWks to the string.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM