![](/img/trans.png)
[英]How to reference another cell as a wildcard from a separate worksheet, within the same workbook, when using SUMIF function?
[英]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.