希望你能帮助解决过去几天困扰我的大脑的问题。
我特别擅长marcos和Excel Power Query,我正在尝试合并工作簿中的许多非相邻列,并且能够在Power Query中成功完成此操作。我的目标是尽可能地自动化这个,所以我决定录制一个宏。
运行录制的宏时,我得到运行时错误1004。
我的宏代码如下:
Sub PQ_CSATMerge()
'
' PQ_CSATMerge Macro
'
'
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.CurrentRegion.Select
Application.CutCopyMode = False
ActiveSheet.ListObjects.Add(xlSrcRange,Range(" $ A $ 1:$ CS $ 816"),,, xlYes).Name = _ "表3"
Range("Table3[#All]").Select
ActiveWorkbook.Queries.Add Name:="Table3", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.CurrentWorkbook(){[Name=""Table3""]}[Content]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type any}, {""Start (UTC)"", type datetime}, {""Visitor ID"", type text}, {""Visitor Name"", type text}, {""MCS"", Int64.Type}, {""Skill"", type text}, {""Agent Name"", type text}, {""Agent Login Name"", type text}, {""Agen" & _
"t Full Name"", type text}, {""Agent Group"", type text}, {""Chat Start Reason"", type text}, {""Chat End Reason"", type text}, {""Chat Requested Time (UTC)"", type datetime}, {""Length (seconds)"", Int64.Type}, {""Interactive"", type logical}, {""Engagement ID"", Int64.Type}, {""Goal"", type text}, {""Campaign"", type text}, {""Target Audience"", type text}, {""Enga" & _
"gement Name"", type text}, {""Visitor Behavior"", type text}, {""Country"", type text}, {""State"", type text}, {""City"", type text}, {""ISP"", type text}, {""Organization"", type text}, {""IP Address"", type text}, {""Device"", type text}, {""Browser"", type text}, {""Operating System"", type text}, {""Chat Start Page"", type text}, {""Chat Start URL"", type text}" & _
", {""Transcript"", type text}, {""Personal Info Exists"", type logical}, {""Customer Info Exists"", type logical}, {""Marketing Source Exists"", type logical}, {""Lead Exists"", type logical}, {""Lead - Topic"", type text}, {""Visitor Error Exists"", type logical}, {""Service Activity Exists"", type logical}, {""Viewed Product Exists"", type logical}, {""Viewed Prod" & _
"uct - Products"", type any}, {""Transaction Exists"", type logical}, {""Transaction - Total"", type any}, {""Transaction - Order ID"", type any}, {""Cart Update Exists"", type logical}, {""Cart Update - Total"", Int64.Type}, {""Cart Update - Number of Items"", Int64.Type}, {""Cart Update - Products"", type any}, {""Pre-Chat Survey Exists"", type logical}, {""Post-Ch" & _
"at Survey Exists"", type logical}, {""Agent Survey Exists"", type logical}, {""Chat Data Enriched"", type logical}, {""Line of Business"", type text}, {""Search Content Exists"", type logical}, {""CoBrowse - Num Sessions"", Int64.Type}, {""CoBrowse - Num Interactive Sessions"", Int64.Type}, {""Alerted MCS"", Int64.Type}, {""Chat MCS"", Int64.Type}, {""Exit Survey - " & _
"France - Exit Survey - Voulez-vous recevoir une transcription de ce chat?"", type text}, {""Exit Survey - France - Exit Survey - Adresse e-mail"", type text}, {""Exit Survey - France - Exit Survey - L'agent du chat était-il capable de résoudre votre requête ou de vous diriger vers une personne capable de vous aider ?"", type text}, {""Exit Survey - France - Exit Sur" & _
"vey - Quelle est la probabilité de recommander SAS à un ami ou à un collègue?"", type any}, {""French_CSAT"", type text}, {""Exit Survey - France - Exit Survey - Dans l'ensemble, était-il facile d'obtenir l'information ou l'aide dont vous avez eu besoin aujourd'hui?"", type text}, {""Exit Survey - France - Exit Survey - Veuillez nous faire part de vos commentaires o" & _
"u suggestions sur la meilleure façon de vous servir"", type text}, {""Exit Survey - Italy - Exit Survey - Desidera ricevere per email una copia della chat?"", type text}, {""Exit Survey - Italy - Exit Survey - Se sì, la preghiamo di fornire la sua email:"", type text}, {""Exit Survey - Italy - Exit Survey - Raccomanderebbe SAS ad un amico o un collega?"", type any}," & _
" {""Italian_CSAT"", type text}, {""Exit Survey - Italy - Exit Survey - Nel complesso, quanto è stato facile ottenere le informazioni o l’aiuto di cui aveva bisogno oggi?"", type text}, {""Exit Survey - Italy - Exit Survey - La preghiamo di fornire dei suggerimenti al fine di poter migliorare il nostro servizio:"", type text}, {""Exit Survey - Netherlands - Exit Surv" & _
"ey - Wilt u dat wij u een kopie van deze chat emailen?"", type text}, {""Exit Survey - Netherlands - Exit Survey - Email adres"", type text}, {""Exit Survey - Netherlands - Exit Survey - Was de chatagent in staat om uw vraag op te lossen of u te leiden naar iemand die kan helpen?"", type text}, {""Exit Survey - Netherlands - Exit Survey - Hoe waarschijnlijk zou u SA" & _
"S aan een vriend of collega aanbevelen?"", Int64.Type}, {""Dutch_CSAT"", type text}, {""Exit Survey - Netherlands - Exit Survey - Hoe makkelijk was het om de informatie of hulp die u nodig had vandaag te verkrijgen?"", type text}, {""Exit Survey - Netherlands - Exit Survey - Geef ons alstublieft feedback of suggesties over hoe we u beter kunnen bedienen. "", type te" & _
"xt}, {""Exit Survey - SAS Exit Survey - English - Would you like us to email you a transcript of this chat?"", type text}, {""Exit Survey - SAS Exit Survey - English - Email Address"", type text}, {""Exit Survey - SAS Exit Survey - English - Was the chat representative able to resolve your inquiry or direct you to someone#(cr)#(lf)who could help?"", type text}, {""E" & _
"nglish_CSAT"", type text}, {""Exit Survey - SAS Exit Survey - English - How likely would you be to recommend SAS to a friend or colleague?"", type any}, {""Exit Survey - SAS Exit Survey - English - Overall, how easy was it to get the information or help you needed today?"", type any}, {""Exit Survey - SAS Exit Survey - English - Please provide us with any feedback o" & _
"r suggestions for how we can serve you better."", type text}, {""Exit Survey - Spain - Exit Survey - ¿Le gustaría que le enviemos por correo email una copia de este chat?"", type text}, {""Exit Survey - Spain - Exit Survey - Email"", type text}, {""Exit Survey - Spain - Exit Survey - El agente del chat fue capaz de resolver tu consulta o dirigirte a alguien que podr" & _
"ía ayudarte?"", type text}, {""Exit Survey - Spain - Exit Survey - ¿Qué probabilidad hay que le recomiende SAS a un amigo o compañero?"", type text}, {""Spanish_CSAT"", type text}, {""Exit Survey - Spain - Exit Survey - En general, con que facilidad ha recibido la información o ayuda que necesitaba hoy?"", type text}, {""Exit Survey - Spain - Exit Survey - Por favor" & _
", facilítanos cualquier comentario o sugerencia sobre cómo podemos mejorar el servicio."", type any}, {""Chat Operator Survey - SAS Operator Survey - English - What was the nature of the chat?"", type text}, {""Chat Operator Survey - SAS Operator Survey - English - Referred or Resolved?"", type text}, {""Chat Operator Survey - SAS Operator Survey - English - Area""," & _
" type text}, {""Chat Operator Survey - SAS Operator Survey - English - If further explanation needed, please provide here:"", type text}})," & Chr(13) & "" & Chr(10) & " #""Merged Columns"" = Table.CombineColumns(Table.TransformColumnTypes(#""Changed Type"", {{""Column1"", type text}}, ""en-GB""),{""Column1"", ""French_CSAT"", ""Italian_CSAT"", ""Dutch_CSAT"", ""English_CSAT"", ""Spanish_CS" & _
"AT""},Combiner.CombineTextByDelimiter("""", QuoteStyle.None),""Merged CSAT"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Merged Columns"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table3;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table3]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table3_2"
.Refresh BackgroundQuery:=False
End With
End Sub