如何解决VBA取消固定数目的列,就像Power QUery
在这里是相当新的,对于宏编码也很新。发现这个论坛对像我这样的初学者非常有帮助,所以我很高兴得到你们的帮助! ^^
我一直在搜寻我需要做的这段代码,可悲的是,尽管有些代码很接近,但是它实际上并没有满足我的需要,实际上这只是非常简单的情况。
例如,我有130列,其中13列必须是不可透视的。这么简单。 当然,行数可能会不时变化。
这与Power Query完美配合,但我很傻,vba不会执行Power Query。哈哈8o
我附上了一个示例文件,该文件可以显示如何显示我的数据,
其中A到E列是固定数组,F往后将是不可透视的主题。
希望有人真的可以在这方面为我提供帮助,因为我现在好像头发松了。大声笑。
非常感谢您!
让我知道我该如何回报你的好意。
编辑:所以我发现Powerquery也可以进行VBA,但是不幸的是,由于某种原因,我在这里遇到错误:
'Sub Macro10()
ActiveWorkbook.Queries.Add Name:="ProjectDetails (4)",Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.CurrentWorkbook(){[Name=""ProjectDetails""]}[Content]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Project"",Int64.Type},{""Project2"",{""Project Name"",type text},{""Strategic Business Unit"",{""Area"",{""Sub Area"",{""Business Line"",{""Department"",ty" & _
"pe text},{""AECOM Project Review Category"",{""Customer Name"",{""Customer Type"",{""Project Manager"",{""Project Accountant"",{""Project Biller"",{""Regional Director"",{""Project Director"",{""Project Status"",{""End-Client Sector"",{" & _
"""Global Business Line"",{""AECOM Work"",{""AECOM Funding Source"",{""Distribution Rule"",{""Agreement"",{""CRM Tracking #"",{""Last Invoiced Date"",type any},{""Last EAC Date"",{""Scheduled Start Date"",type datetime},{""Scheduled Finish Date"",{""Forecast" & _
" End Date"",{""AECOM Contract Type"",{""MTD Revenue"",type number},{""MTD Total Costs"",{""MTD DL"",{""MTD Fringe"",{""MTD Overhead"",{""MTD ODC"",{""MTD Subs"",{""NSR MTD"",{""MTD GM"",{""MTD GM % of Revenues"",typ" & _
"e number},{""MTD NM"",{""Direct Labor Multiplier MTD"",{""Salary Cost Multiplier MTD"",{""MTD Bookings"",{""YTD Revenue"",{""YTD Total Costs"",{""YTD DL"",{""YTD Fringe"",{""YTD Overhead"",{""YTD ODC"",{""YTD Subs""," & _
"type number},{""YTD NSR"",{""YTD GM"",{""YTD GM % of Revenue"",{""YTD NM"",{""YTD DLM"",{""YTD SCM"",{""YTD Bookings"",{""ITD Revenue"",{""ITD Total Costs"",{""ITD DL"",{""ITD Fringe"",{""ITD Overhead""" & _
",{""ITD ODC"",{""ITD Subs"",{""ITD NSR"",{""ITD GM"",{""ITD GM% of Revenue"",{""ITD NM"",{""ITD DLM"",{""ITD SCM"",{""Funded Value"",{""Backlog"",{""Unbilled >30"",{""Unbilled (BIEE)"",ty" & _
"pe number},{""ITD Billed"",{""Billing > 60"",{""Trade AR"",{""Retention"",{""Appr Revenue Budget"",{""Appr ODC Budget"",{""Appr Subs Budget"",{""Appr Contingency Budget"",{""Appr DL Budget"",{""Appr Fringe Budget"",{""" & _
"Appr Cost Budget"",{""Appr DLM Budget"",{""Appr SCM Budget"",{""Frcst Revenue Budget"",{""Frcst ODC Budget"",{""Frcst Subs Budget"",{""Frcst Contingency Budget"",{""Frcst DL Budget"",{""Frcst Fringe Budget"",{""Frcst Overhead Budget" & _
""",{""Frcst Cost Budget"",{""Frcst DLM"",{""Frcst SCM"",{""Source System Description"",{""Source System ID"",{""Valid for Review?"",{""Project Accountant Required"",{""Invoiced Over Contract "",{""Est. Collections over Contact "",type numb" & _
"er},{""Current ETC "",{""Trended Costs"",{""Est. ETC at Month End"",{""MTD NM% of NSR"",{""YTD NM% of NSR"",{""ITD NM% of NSR"",{""Percent Complete"",{""Backlog Burn Rate"",{""Billing Backlog"",{""Unbilled Reserve"",{""AR Reserve"",{""YTD Lost Margin"",{""Project Manager Location"",{""ROCV"",{""Low to Zero Backlog"",{""Negative Margin MTD"",{""Negative Margin YTD"",{""Negative Margin ITD"",type text}})," & Chr(13) & "" & Chr(10) & " #""Unpivoted Columns"" = Table.UnpivotOtherColumns(#""Changed Type"",{""Project"",""Project" & _
"2"",""Project Name"",""Strategic Business Unit"",""Area"",""Sub Area"",""Business Line"",""Department"",""AECOM Project Review Category"",""Customer Name"",""Customer Type"",""Project Manager"",""Project Accountant"",""Project Biller"",""Regional Director"",""Project Director"",""Project Status"",""End-Client Sector"",""Global Business Line"",""AEC" & _
"OM Work"",""AECOM Funding Source"",""Distribution Rule"",""Agreement"",""CRM Tracking #"",""Last Invoiced Date"",""Last EAC Date"",""Scheduled Start Date"",""Scheduled Finish Date"",""Forecast End Date"",""AECOM Contract Type"",""MTD Revenue"",""MTD Total Costs"",""MTD DL"",""MTD Fringe"",""MTD Overhead"",""MTD ODC"",""MTD Subs"",""NSR MTD"",""MTD" & _
" GM"",""MTD GM % of Revenues"",""MTD NM"",""Direct Labor Multiplier MTD"",""Salary Cost Multiplier MTD"",""MTD Bookings"",""YTD Revenue"",""YTD Total Costs"",""YTD DL"",""YTD Fringe"",""YTD Overhead"",""YTD ODC"",""YTD Subs"",""YTD NSR"",""YTD GM"",""YTD GM % of Revenue"",""YTD NM"",""YTD DLM"",""YTD SCM"",""YTD Bookings"",""ITD Revenue"",""ITD " & _
"Total Costs"",""ITD DL"",""ITD Fringe"",""ITD Overhead"",""ITD ODC"",""ITD Subs"",""ITD NSR"",""ITD GM"",""ITD GM% of Revenue"",""ITD NM"",""ITD DLM"",""ITD SCM"",""Funded Value"",""Backlog"",""Unbilled >30"",""Unbilled (BIEE)"",""ITD Billed"",""Billing > 60"",""Trade AR"",""Retention"",""Appr Revenue Budget"",""Appr ODC Budget"",""Appr Subs Bu" & _
"dget"",""Appr Contingency Budget"",""Appr DL Budget"",""Appr Fringe Budget"",""Appr Cost Budget"",""Appr DLM Budget"",""Appr SCM Budget"",""Frcst Revenue Budget"",""Frcst ODC Budget"",""Frcst Subs Budget"",""Frcst Contingency Budget"",""Frcst DL Budget"",""Frcst Fringe Budget"",""Frcst Overhead Budget"",""Frcst Cost Budget"",""Frcst DLM"",""Frcst SCM" & _
""",""Source System Description"",""Source System ID"",""Valid for Review?"",""Project Accountant Required"",""Invoiced Over Contract "",""Est. Collections over Contact "",""Current ETC "",""Trended Costs"",""Est. ETC at Month End"",""MTD NM% of NSR"",""YTD NM% of NSR"",""ITD NM% of NSR"",""Percent Complete"",""Backlog Burn Rate"",""Billing Backlog""," & _
"""Unbilled Reserve"",""AR Reserve"",""YTD Lost Margin"",""Project Manager Location""},""Attribute"",""Value"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Unpivoted Columns"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0,Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""ProjectDetails (4)"";Extended Properties=""""" _,Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [ProjectDetails (4)]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "ProjectDetails__4"
.Refresh BackgroundQuery:=False
End With
Application.CommandBars("Queries and Connections").Visible = False
结束字幕 '
[![在此处输入图片描述] [1]] [1] [1]:https://i.stack.imgur.com/SQsdQ.png
解决方法
我发布的链接中的$ pip3 install matplotlib
Collecting matplotlib
Using cached matplotlib-3.3.2.tar.gz (37.9 MB)
Requirement already satisfied: certifi>=2020.06.20 in ./files/usr/lib/python3.9/site-packages (from matplotlib) (2020.6.20)
Requirement already satisfied: cycler>=0.10 in ./files/usr/lib/python3.9/site-packages (from matplotlib) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in ./files/usr/lib/python3.9/site-packages (from matplotlib) (1.2.0)
Requirement already satisfied: numpy>=1.15 in ./files/usr/lib/python3.9/site-packages (from matplotlib) (1.19.3)
Requirement already satisfied: pillow>=6.2.0 in ./files/usr/lib/python3.9/site-packages (from matplotlib) (8.0.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in ./files/usr/lib/python3.9/site-packages (from matplotlib) (2.4.7)
Requirement already satisfied: python-dateutil>=2.1 in ./files/usr/lib/python3.9/site-packages (from matplotlib) (2.8.1)
Requirement already satisfied: six in ./files/usr/lib/python3.9/site-packages (from cycler>=0.10->matplotlib) (1.15.0)
Building wheels for collected packages: matplotlib Building wheel for matplotlib (setup.py) ... error ERROR: Command errored out with exit status 1: command: /data/data/com.termux/files/usr/bin/python3 -u -c 'import sys,setuptools,tokenize; sys.argv[0] = '"'"'/data/data/com.termux/files/usr/tmp/pip-install-zejjxir0/matplotlib/setup.py'"'"'; __file__='"'"'/data/data/com.termux/files/usr/tmp/pip-install-zejjxir0/matplotlib/setup.py'"'"';f=getattr(tokenize,'"'"'open'"'"',open)(__file__);code=f.read().replace('"'"'\r\n'"'"','"'"'\n'"'"');f.close();exec(compile(code,__file__,'"'"'exec'"'"'))' bdist_wheel -d /data/data/com.termux/files/usr/tmp/pip-wheel-6cp2oysk cwd: /data/data/com.termux/files/usr/tmp/pip-install-zejjxir0/matplotlib/ Complete output (580 lines): Edit setup.cfg to change the build options; suppress output with --quiet.
BUILDING MATPLOTLIB matplotlib: yes [3.3.2] python: yes [3.9.0 (default,Oct 8 2020,15:54:02) [Clang 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 98c855489] platform: yes [linux] sample_data: yes [installing] tests: no [skipping due to configuration] macosx: no [Mac OS-X only] running bdist_wheel running build running build_py creating build creating build/lib.linux-aarch64-3.9 copying lib/pylab.py -> build/lib.linux-aarch64-3.9 creating build/lib.linux-aarch64-3.9/matplotlib copying lib/matplotlib/__init__.py -> build/lib.linux-aarch64-3.9/matplotlib copying lib/matplotlib/_animation_data.py -> build/lib.linux-aarch64-3.9/matplotlib copying lib/matplotlib/_cm.py -> build/lib.linux-aarch64-3.9/matpl
...
...
copying lib/matplotlib/mpl-data/sample_data/membrane.dat -> build/lib.linux-aarch64-3.9/matplotlib/mpl-data/sample_data
UPDATING build/lib.linux-aarch64-3.9/matplotlib/_version.py
set build/lib.linux-aarch64-3.9/matplotlib/_version.py to '3.3.2'
running build_ext
creating data
creating data/data
creating data/data/com.termux
creating data/data/com.termux/files
creating data/data/com.termux/files/usr
creating data/data/com.termux/files/usr/tmp
aarch64-linux-android-clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fstack-protector-strong -O3 -fstack-protector-strong -O3 -Wno-deprecated-declarations -Wno-unreachable-code -fPIC -I/data/data/com.termux/files/usr/include/python3.9 -c /data/data/com.termux/files/usr/tmp/tmpf47ko78k.cpp -o data/data/com.termux/files/usr/tmp/tmpf47ko78k.o -fvisibility=hidden
aarch64-linux-android-clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fstack-protector-strong -O3 -fstack-protector-strong -O3 -Wno-deprecated-declarations -Wno-unreachable-code -fPIC -I/data/data/com.termux/files/usr/include/python3.9 -c /data/data/com.termux/files/usr/tmp/tmp4auaqklr.cpp -o data/data/com.termux/files/usr/tmp/tmp4auaqklr.o -fvisibility-inlines-hidden
Extracting freetype-2.6.1.tar.gz
Building freetype in build/freetype-2.6.1
error: [Errno 13] Permission denied: './configure'
----------------------------------------
ERROR: Failed building wheel for matplotlib
Running setup.py clean for matplotlib
Failed to build matplotlib
Installing collected packages: matplotlib
Running setup.py install for matplotlib ... error
ERROR: Command errored out with exit status 1:
command: /data/data/com.termux/files/usr/bin/python3 -u -c 'import sys,tokenize; sys.argv[0] = '"'"'/data/data/com.termux/files/usr/tmp/pip-install-3w4igc1k/matplotlib/setup.py'"'"'; __file__='"'"'/data/data/com.termux/files/usr/tmp/pip-install-3w4igc1k/matplotlib/setup.py'"'"';f=getattr(tokenize,'"'"'exec'"'"'))' install --record /data/data/com.termux/files/usr/tmp/pip-record-jffs1p2w/install-record.txt --single-version-externally-managed --compile --install-headers /data/data/com.termux/files/usr/include/python3.9/matplotlib
cwd: /data/data/com.termux/files/usr/tmp/pip-install-3w4igc1k/matplotlib/
Complete output (572 lines):
Edit setup.cfg to change the build options; suppress output with --quiet.
BUILDING MATPLOTLIB
matplotlib: yes [3.3.2]
python: yes [3.9.0 (default,15:54:02) [Clang 9.0.8
(https://android.googlesource.com/toolchain/llvm-project
98c855489]
platform: yes [linux]
sample_data: yes [installing]
tests: no [skipping due to configuration]
macosx: no [Mac OS-X only]
running install
running build
running build_py
creating build/lib.linux-aarch64-3.9
copying lib/pylab.py -> build/lib.linux-aarch64-3.9
creating build/lib.linux-aarch64-3.9/matplotlib
copying lib/matplotlib/__init__.py -> build/lib.linux-aarch64-3.9/matplotlib
copying lib/matplotlib/_animation_data.py -> build/lib.linux-aarch64-3.9/matplotlib
copying lib/matplotlib/_cm.py -> build/lib.linux-aarch64-3.9/matplotlib
...
...
copying lib/matplotlib/mpl-data/images/zoom_to_rect.png -> build/lib.linux-aarch64-3.9/matplotlib/mpl-data/images
copying lib/matplotlib/mpl-data/sample_data/README.txt -> build/lib.linux-aarch64-3.9/matplotlib/mpl-data/sample_data
UPDATING build/lib.linux-aarch64-3.9/matplotlib/_version.py set build/lib.linux-aarch64-3.9/matplotlib/_version.py to '3.3.2'
running build_ext aarch64-linux-android-clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fstack-protector-strong -O3 -fstack-protector-strong -O3 -Wno-deprecated-declarations -Wno-unreachable-code -fPIC -I/data/data/com.termux/files/usr/include/python3.9 -c /data/data/com.termux/files/usr/tmp/tmp8gd5nxqf.cpp -o data/data/com.termux/files/usr/tmp/tmp8gd5nxqf.o -fvisibility=hidden
aarch64-linux-android-clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fstack-protector-strong -O3 -fstack-protector-strong -O3 -Wno-deprecated-declarations -Wno-unreachable-code -fPIC -I/data/data/com.termux/files/usr/include/python3.9 -c /data/data/com.termux/files/usr/tmp/tmpfcioek2o.cpp -o data/data/com.termux/files/usr/tmp/tmpfcioek2o.o -fvisibility-inlines-hidden Building freetype in build/freetype-2.6.1
error: [Errno 13] Permission denied: './configure'
---------------------------------------- ERROR: Command errored out with exit status 1: /data/data/com.termux/files/usr/bin/python3 -u -c 'import sys,'"'"'exec'"'"'))' install --record /data/data/com.termux/files/usr/tmp/pip-record-jffs1p2w/install-record.txt --single-version-externally-managed --compile --install-headers /data/data/com.termux/files/usr/include/python3.9/matplotlib Check the logs for full command output.
函数没有任何更改,这应该根据您发布的文件执行您想要的操作:
UnPivotData
编辑-添加Sub Tester()
Dim p
'get the unpivoted data as a 2-D array
p = UnPivotData(ThisWorkbook.Sheets("data source").Range("A2").CurrentRegion,_
118,True,True)
'I added a new sheet for the output
With Sheets("output2")
.Cells.ClearContents
.Range("A1").Resize(UBound(p,1),UBound(p,2)).Value = p 'populate array to sheet
End With
'EDIT: alternative (slower) method to populate the sheet
' from the pivoted dataset. Might need to use this
' if you have a large amount of data
' Dim r As Long,c As Long
' For r = 1 To UBound(p,1)
' For c = 1 To UBound(p,2)
' Sheets("Sheet2").Cells(r,c).Value = p(r,c)
' Next c
' Next r
End Sub
函数:
UnPivotData
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。