我正在尝试自动化电子表格,以便根据数据的前3个字符将数据从一个工作表传输到另一个工作表。因此,例如,对于数据NDX 12/31/2012 P2600,我希望将其放在NDX表中。所以我有一个数组(desArr())将数据分成数组的不同位置,这样desArr(0)包含" NDX",desArr(1)包含" 12/31 / 2012"等等。
我遇到问题的部分是将数据移动到相应的工作表。具体来说,我需要对这些电子表格的变量引用。立即拍摄NDX表。我知道我可以做NDX.cells(1,1).Paste或Worksheets(NDX.Name).Cells(1,1).Paste,这会起作用,但如果我想为多张表做什么呢?我显然可以使用If语句来定义每个不同的实例,但我想缩短我的代码。因此,我试图引用sheet对象变量,即desArr(0).Name,但它返回错误(我理解为什么)。任何有关如何实现这一目标的建议?我知道一个解决方案就是使用工作表的name属性,但是如果有人更改了工作表的名称,我想避免代码失败的可能性。
所以也许像:
Dim desArr() As String, desInfo As String, opType As String
Dim rNum As Long, cNum As Long, i As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim sortRng As Range, findRng As Range
Dim j As Integer 'Throw away after testing
Dim test As String 'Throw away after testing
Dim k As Integer 'Throw away after testing
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = ThisWorkbook
Set ws = wb.Worksheets(Import.Name)
With ws
rNum = .Range("C1048576").End(xlUp).Row
cNum = 6 'Number of used columns starting from left
Set sortRng = .Range(.Cells(3, 2), .Cells(rNum, cNum))
'Sort range according to Type and Description
sortRng.Sort _
Key1:=.Range("B1"), _
Key2:=.Range("C1")
'Apply conditional formatting
With sortRng.Columns(2)
.FormatConditions.AddUniqueValues
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).DupeUnique = xlDuplicate
With sortRng.Columns(2).FormatConditions(1)
.Interior.PatternColorIndex = xlAutomatic
.Interior.Color = 13551615
.Interior.TintAndShade = 0
.StopIfTrue = False
End With
End With
For i = 0 To (rNum - 2)
With sortRng.Cells(i + 1, 2)
If .DisplayFormat.Interior.Color = "13551615" Then
j = 0
While (.Value = .Offset(j + 1, 0).Value And .Offset(0, 1).Value = .Offset(j + 1, 1).Value)
j = j + 1
Wend
If (j <> 0) Then 'There are duplicates
End If
End If
'Converting the description to format used for classification
If .Offset(0, -1) = "Ext Option" Then
desArr = Split(.Value, " ")
If Not (Left(.Value, 3) = "SX5" Or Left(.Value, 3) = "UKX") Then
'check if it's a call or put
If Left(desArr(3), 1) = "C" Then
opType = "Call"
ElseIf Left(desArr(3), 1) = "P" Then
opType = "Put"
Else
opType = "N/A"
End If
desInfo = Format(desArr(2), "mmmdd") & " " & Right(Trim(desArr(3)), Len(Trim(desArr(3))) - 1) & " " & opType
Else
'check if it's a call or put
If Left(desArr(2), 1) = "C" Then
opType = "Call"
ElseIf Left(desArr(2), 1) = "P" Then
opType = "Put"
Else
opType = "N/A"
End If
desInfo = Format(desArr(1), "mmmdd") & " " & Right(Trim(desArr(2)), Len(Trim(desArr(2))) - 1) & " " & opType
End If
End If
End With
Next i
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
除了NDX必须是可变的,因为将数据移动到哪个工作表取决于数据。
答案 0 :(得分:2)
您可以使用工作表的代码名称属性。如果使用NDX.Cells(1,1),则NDX是工作表的代号。只需搜索所有工作表,例如:
Function GetWorksheet(byval withCodename as String) as Worksheet
Dim sheetVar as Worksheet
For each sheetVar in ThisWorkbook.Worksheets
If sheetVar.CodeName = withCodename Then
Set GetWorksheet = sheetVar
End if
Next
End Function
答案 1 :(得分:0)
你可以:
您写道:&#34;如果有人更改了工作表的名称,我想避免代码失败的可能性。&#34;
嗯,用户不能这样做:
如果您保护工作簿。您可以在功能区中手动执行此操作(查看&gt;更改&gt;保护工作簿),或者以编程方式执行此操作:
ThisWorkbook.Protect 'optionally, add a password -- see documentation for Protect
这将完全阻止用户更改工作表名称。