ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 487|回复: 0

在数据透视表旁复制链接

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-9-20 07:31 | 显示全部楼层 |阅读模式
较之前帖稍微简化了下代码
想把sheet1(明细)中的F列链接复制到sheet2(汇总)“数据透视表”对应行的右侧,可试了几次,以下这段代码在模块里单独运行OK,但放到了thisworkbook的open里,就是运行不起来,不知能否解决,请赐教,谢谢
  1. '数据透视表上做链接**************************************************************************
  2. Sheet2.Range("C3").Value = "链接"
  3. For j = 1 To Sheet2.[A65536].End(3).Row
  4. Set fcell = Sheet1.Range("e:e").Find(Sheet2.Cells(j, 1))
  5. If Not fcell Is Nothing Then
  6. Sheet1.Range("f" & fcell.Row).Copy Sheet2.Cells(j, 3)
  7. Else
  8. Sheet2.Cells(j, 3) = ""
  9. End If
  10. Set rng = Nothing
  11. Next
复制代码
以下是所有代码
  1. Private Sub Workbook_Open()
  2. Application.ScreenUpdating = False
  3. Application.DisplayAlerts = False
  4. On Error Resume Next
  5. Dim j%, fcell As Range
  6.     '定义名称************************************************************************************
  7.         ActiveWorkbook.Names("数据").RefersToR1C1 = _
  8.         "=OFFSET(明细!R1C1,,,COUNTA(明细!C1),COUNTA(明细!R1))"
  9.     '数透表************************************************************************
  10.     Sheets.Add
  11.     ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="数据" _
  12.         , Version:=6).CreatePivotTable TableDestination:="Sheet1!R3C1", TableName _
  13.         :="数据透视表1", DefaultVersion:=6
  14.     Sheets("Sheet1").Select
  15.     Cells(3, 1).Select
  16.     With ActiveSheet.PivotTables("数据透视表1")
  17.         .ColumnGrand = True
  18.         .HasAutoFormat = True
  19.         .DisplayErrorString = False
  20.         .DisplayNullString = True
  21.         .EnableDrilldown = True
  22.         .ErrorString = ""
  23.         .MergeLabels = False
  24.         .NullString = ""
  25.         .PageFieldOrder = 2
  26.         .PageFieldWrapCount = 0
  27.         .PreserveFormatting = True
  28.         .RowGrand = True
  29.         .SaveData = True
  30.         .PrintTitles = False
  31.         .RepeatItemsOnEachPrintedPage = True
  32.         .TotalsAnnotation = False
  33.         .CompactRowIndent = 1
  34.         .InGridDropZones = False
  35.         .DisplayFieldCaptions = True
  36.         .DisplayMemberPropertyTooltips = False
  37.         .DisplayContextTooltips = True
  38.         .ShowDrillIndicators = True
  39.         .PrintDrillIndicators = False
  40.         .AllowMultipleFilters = False
  41.         .SortUsingCustomLists = True
  42.         .FieldListSortAscending = False
  43.         .ShowValuesRow = False
  44.         .CalculatedMembersInFilters = False
  45.         .RowAxisLayout xlCompactRow
  46.     End With
  47.     With ActiveSheet.PivotTables("数据透视表1").PivotCache
  48.         .RefreshOnFileOpen = False
  49.         .MissingItemsLimit = xlMissingItemsDefault
  50.     End With
  51.     ActiveSheet.PivotTables("数据透视表1").RepeatAllLabels xlRepeatLabels
  52.     ActiveWorkbook.ShowPivotTableFieldList = True
  53.     With ActiveSheet.PivotTables("数据透视表1").PivotFields("学期")
  54.         .Orientation = xlRowField
  55.         .Position = 1
  56.     End With
  57.     With ActiveSheet.PivotTables("数据透视表1").PivotFields("年级")
  58.         .Orientation = xlRowField
  59.         .Position = 2
  60.     End With
  61.     With ActiveSheet.PivotTables("数据透视表1").PivotFields("课程")
  62.         .Orientation = xlRowField
  63.         .Position = 3
  64.     End With
  65.     With ActiveSheet.PivotTables("数据透视表1").PivotFields("单元")
  66.         .Orientation = xlRowField
  67.         .Position = 4
  68.     End With
  69.     ActiveSheet.PivotTables("数据透视表1").AddDataField ActiveSheet.PivotTables("数据透视表1" _
  70.         ).PivotFields("课名"), "计数项:课名", xlCount
  71.     With ActiveSheet.PivotTables("数据透视表1").PivotFields("课名")
  72.         .Orientation = xlRowField
  73.         .Position = 5
  74.     End With
  75.     Range("B4").Select
  76.     ActiveWindow.FreezePanes = True
  77.     ActiveWorkbook.ShowPivotTableFieldList = False
  78.     Sheets("Sheet1").Select
  79.     Sheets("Sheet1").Name = "汇总"
  80.     '数据透视表上做链接**************************************************************************
  81.     Sheet2.Range("C3").Value = "链接"
  82.     For j = 1 To Sheet2.[A65536].End(3).Row
  83.         Set fcell = Sheet1.Range("e:e").Find(Sheet2.Cells(j, 1))
  84.         If Not fcell Is Nothing Then
  85.                   Sheet1.Range("f" & fcell.Row).Copy Sheet2.Cells(j, 3)
  86.             Else
  87.                   Sheet2.Cells(j, 3) = ""
  88.         End If
  89.         Set rng = Nothing
  90.     Next
  91.         '************************************************************************************
  92.     Application.DisplayAlerts = True
  93.     Application.ScreenUpdating = True
  94. End Sub


  95. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  96.     Application.ScreenUpdating = False
  97.     Application.DisplayAlerts = False
  98.     On Error Resume Next
  99.     Sheets("汇总").Select
  100.     ActiveWindow.SelectedSheets.Delete
  101.     Application.DisplayAlerts = True
  102.     Application.ScreenUpdating = True
  103.     ActiveWorkbook.Save
  104. End Sub

复制代码


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-25 23:51 , Processed in 0.036048 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表