|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
较之前帖稍微简化了下代码
想把sheet1(明细)中的F列链接复制到sheet2(汇总)“数据透视表”对应行的右侧,可试了几次,以下这段代码在模块里单独运行OK,但放到了thisworkbook的open里,就是运行不起来,不知能否解决,请赐教,谢谢
- '数据透视表上做链接**************************************************************************
- Sheet2.Range("C3").Value = "链接"
- For j = 1 To Sheet2.[A65536].End(3).Row
- Set fcell = Sheet1.Range("e:e").Find(Sheet2.Cells(j, 1))
- If Not fcell Is Nothing Then
- Sheet1.Range("f" & fcell.Row).Copy Sheet2.Cells(j, 3)
- Else
- Sheet2.Cells(j, 3) = ""
- End If
- Set rng = Nothing
- Next
复制代码 以下是所有代码
- Private Sub Workbook_Open()
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- On Error Resume Next
- Dim j%, fcell As Range
- '定义名称************************************************************************************
- ActiveWorkbook.Names("数据").RefersToR1C1 = _
- "=OFFSET(明细!R1C1,,,COUNTA(明细!C1),COUNTA(明细!R1))"
- '数透表************************************************************************
- Sheets.Add
- ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="数据" _
- , Version:=6).CreatePivotTable TableDestination:="Sheet1!R3C1", TableName _
- :="数据透视表1", DefaultVersion:=6
- Sheets("Sheet1").Select
- Cells(3, 1).Select
- With ActiveSheet.PivotTables("数据透视表1")
- .ColumnGrand = True
- .HasAutoFormat = True
- .DisplayErrorString = False
- .DisplayNullString = True
- .EnableDrilldown = True
- .ErrorString = ""
- .MergeLabels = False
- .NullString = ""
- .PageFieldOrder = 2
- .PageFieldWrapCount = 0
- .PreserveFormatting = True
- .RowGrand = True
- .SaveData = True
- .PrintTitles = False
- .RepeatItemsOnEachPrintedPage = True
- .TotalsAnnotation = False
- .CompactRowIndent = 1
- .InGridDropZones = False
- .DisplayFieldCaptions = True
- .DisplayMemberPropertyTooltips = False
- .DisplayContextTooltips = True
- .ShowDrillIndicators = True
- .PrintDrillIndicators = False
- .AllowMultipleFilters = False
- .SortUsingCustomLists = True
- .FieldListSortAscending = False
- .ShowValuesRow = False
- .CalculatedMembersInFilters = False
- .RowAxisLayout xlCompactRow
- End With
- With ActiveSheet.PivotTables("数据透视表1").PivotCache
- .RefreshOnFileOpen = False
- .MissingItemsLimit = xlMissingItemsDefault
- End With
- ActiveSheet.PivotTables("数据透视表1").RepeatAllLabels xlRepeatLabels
- ActiveWorkbook.ShowPivotTableFieldList = True
- With ActiveSheet.PivotTables("数据透视表1").PivotFields("学期")
- .Orientation = xlRowField
- .Position = 1
- End With
- With ActiveSheet.PivotTables("数据透视表1").PivotFields("年级")
- .Orientation = xlRowField
- .Position = 2
- End With
- With ActiveSheet.PivotTables("数据透视表1").PivotFields("课程")
- .Orientation = xlRowField
- .Position = 3
- End With
- With ActiveSheet.PivotTables("数据透视表1").PivotFields("单元")
- .Orientation = xlRowField
- .Position = 4
- End With
- ActiveSheet.PivotTables("数据透视表1").AddDataField ActiveSheet.PivotTables("数据透视表1" _
- ).PivotFields("课名"), "计数项:课名", xlCount
- With ActiveSheet.PivotTables("数据透视表1").PivotFields("课名")
- .Orientation = xlRowField
- .Position = 5
- End With
- Range("B4").Select
- ActiveWindow.FreezePanes = True
- ActiveWorkbook.ShowPivotTableFieldList = False
- Sheets("Sheet1").Select
- Sheets("Sheet1").Name = "汇总"
- '数据透视表上做链接**************************************************************************
- Sheet2.Range("C3").Value = "链接"
- For j = 1 To Sheet2.[A65536].End(3).Row
- Set fcell = Sheet1.Range("e:e").Find(Sheet2.Cells(j, 1))
- If Not fcell Is Nothing Then
- Sheet1.Range("f" & fcell.Row).Copy Sheet2.Cells(j, 3)
- Else
- Sheet2.Cells(j, 3) = ""
- End If
- Set rng = Nothing
- Next
- '************************************************************************************
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- End Sub
- Private Sub Workbook_BeforeClose(Cancel As Boolean)
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- On Error Resume Next
- Sheets("汇总").Select
- ActiveWindow.SelectedSheets.Delete
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- ActiveWorkbook.Save
- End Sub
复制代码
|
|