VBA extract data from Power Queries of PDF file - vba

I want to run a macros to get data from a PDF file, and after extract data from PDF file, I also need a code to get data from queries like this, See screenshot below.
queuries
***Everytime, the data from PDF has different tables and pages, so how can I run the macros for different pdf file with different number of tables and pages
Sub Macro1()
'
' Macro1 Macro
'
'
ActiveWorkbook.Queries.Add Name:="Table001 (Page 1)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table001 = Source{[Id=""Table001""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table001,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table002 (Page 1)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table002 = Source{[Id=""Table002""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table002,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table003 (Page 1)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table003 = Source{[Id=""Table003""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table003,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type duration}, {""Column6""," & _
" type text}, {""Column7"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table004 (Page 1)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table004 = Source{[Id=""Table004""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table004,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table005 (Page 2)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table005 = Source{[Id=""Table005""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table005,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table006 (Page 3)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table006 = Source{[Id=""Table006""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table006,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table007 (Page 4)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table007 = Source{[Id=""Table007""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table007,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table008 (Page 4)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table008 = Source{[Id=""Table008""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table008,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table009 (Page 4)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table009 = Source{[Id=""Table009""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table009,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type duration}, {""Column6""," & _
" type text}, {""Column7"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table010 (Page 4)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table010 = Source{[Id=""Table010""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table010,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table011 (Page 5)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table011 = Source{[Id=""Table011""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table011,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table012 (Page 5)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table012 = Source{[Id=""Table012""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table012,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table013 (Page 5)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table013 = Source{[Id=""Table013""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table013,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type duration}, {""Column6""," & _
" type text}, {""Column7"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table014 (Page 5)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table014 = Source{[Id=""Table014""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table014,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table015 (Page 6)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table015 = Source{[Id=""Table015""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table015,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table016 (Page 7)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table016 = Source{[Id=""Table016""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table016,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Queries.Add Name:="Table017 (Page 8)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""Z:\Planning\Invoices 21442382.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table017 = Source{[Id=""Table017""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table017,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", typ" & _
"e text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table001 (Page 1)", _
"Connection to the 'Table001 (Page 1)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table001 (Page 1);Extended Properties=" _
, """Table001 (Page 1)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table002 (Page 1)", _
"Connection to the 'Table002 (Page 1)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table002 (Page 1);Extended Properties=" _
, """Table002 (Page 1)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table003 (Page 1)", _
"Connection to the 'Table003 (Page 1)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table003 (Page 1);Extended Properties=" _
, """Table003 (Page 1)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table004 (Page 1)", _
"Connection to the 'Table004 (Page 1)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table004 (Page 1);Extended Properties=" _
, """Table004 (Page 1)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table005 (Page 2)", _
"Connection to the 'Table005 (Page 2)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table005 (Page 2);Extended Properties=" _
, """Table005 (Page 2)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table006 (Page 3)", _
"Connection to the 'Table006 (Page 3)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table006 (Page 3);Extended Properties=" _
, """Table006 (Page 3)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table007 (Page 4)", _
"Connection to the 'Table007 (Page 4)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table007 (Page 4);Extended Properties=" _
, """Table007 (Page 4)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table008 (Page 4)", _
"Connection to the 'Table008 (Page 4)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table008 (Page 4);Extended Properties=" _
, """Table008 (Page 4)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table009 (Page 4)", _
"Connection to the 'Table009 (Page 4)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table009 (Page 4);Extended Properties=" _
, """Table009 (Page 4)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table010 (Page 4)", _
"Connection to the 'Table010 (Page 4)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table010 (Page 4);Extended Properties=" _
, """Table010 (Page 4)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table011 (Page 5)", _
"Connection to the 'Table011 (Page 5)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table011 (Page 5);Extended Properties=" _
, """Table011 (Page 5)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table012 (Page 5)", _
"Connection to the 'Table012 (Page 5)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table012 (Page 5);Extended Properties=" _
, """Table012 (Page 5)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table013 (Page 5)", _
"Connection to the 'Table013 (Page 5)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table013 (Page 5);Extended Properties=" _
, """Table013 (Page 5)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table014 (Page 5)", _
"Connection to the 'Table014 (Page 5)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table014 (Page 5);Extended Properties=" _
, """Table014 (Page 5)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table015 (Page 6)", _
"Connection to the 'Table015 (Page 6)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table015 (Page 6);Extended Properties=" _
, """Table015 (Page 6)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table016 (Page 7)", _
"Connection to the 'Table016 (Page 7)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table016 (Page 7);Extended Properties=" _
, """Table016 (Page 7)""", 6, True, False
Workbooks("UK Invoice.xlsm").Connections.Add2 "Query - Table017 (Page 8)", _
"Connection to the 'Table017 (Page 8)' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table017 (Page 8);Extended Properties=" _
, """Table017 (Page 8)""", 6, True, False
ActiveWorkbook.Queries.Add Name:="Append1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Table.Combine({#""Table001 (Page 1)"", #""Table002 (Page 1)"", #""Table003 (Page 1)"", #""Table004 (Page 1)"", #""Table005 (Page 2)"", #""Table006 (Page 3)"", #""Table007 (Page 4)"", #""Table008 (Page 4)"", #""Table009 (Page 4)"", #""Table010 (Page 4)"", #""Table011 (Page 5)"", #""Table012 (Page 5)"", #""Table013 (Page 5)"", #""Table014 (Page 5)""," & _
" #""Table015 (Page 6)"", #""Table016 (Page 7)"", #""Table017 (Page 8)""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source" & _
""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Append1;Extended Properties=""""" _
, Destination:=Range("$A$2")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Append1]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Append1"
.Refresh BackgroundQuery:=False
End With
End Sub

Related

If Then to un-protect and protect document

I have a userform that unprotects a document to let info be entered then protects the document. All of my subs work aside one.
Ranges with if/thens don't work but basic if then works.
Example of sub that works:
Private Sub ComboBox5_Change()
ActiveDocument.Unprotect "password"
Dim ComboBox5 As Range
Set ComboBox5 = ActiveDocument.Bookmarks("bmragpd").Range
ComboBox5.Text = Me.ComboBox5.Value
If Me.ComboBox5.Value = "No" Then
ComboBox5.Text = "205.55a"
End If
If Me.ComboBox5.Value = "Yes" Then
ComboBox5.Text = ""
End If
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, Password:="password"
End Sub
This sub will say the document is already unprotected.
I tried removing the unprotect on combobox6:
Private Sub ComboBox6_Change()
ActiveDocument.Unprotect "password"
Dim rngComboBox6 As Range
Dim sssaText As String
Dim iiia As Integer
Set rngComboBox6 = ActiveDocument.Bookmarks("bmfcs").Range
sssaText = ComboBox6.Value
If Me.ComboBox6.Value = "Yes" Then
For iiia = 1 To 1
sssaText = sssaText & Chr(13) & "200" _
& Chr(13) & "200.1" _
& Chr(13) & "" _
& Chr(13) & "OEBS" _
& Chr(13) & "" _
& Chr(13) & "21c" _
& Chr(13) & "" _
& Chr(13) & "22c" _
& Chr(13) & "Yes" _
& Chr(13) & "" _
& Chr(13) & "Yes" _
& Chr(13) & "Two" _
& Chr(13) & "" _
& Chr(13) & "ES2a.1" _
& Chr(13) & "" _
& Chr(13) & "222" _
& Chr(13) & "" _
& Chr(13) & "222a" _
& Chr(13) & "222b" _
& Chr(13) & "" _
& Chr(13) & "3.a.1" _
& Chr(13) & "" _
& Chr(13) & "NA" _
& Chr(13) & "" _
& Chr(13) & "I. TuuVa"
Next iiia
sssaText = sssaText & Chr(13) & "717217" _
& Chr(13) & "" _
& Chr(13) & "1212" _
& Chr(13) & "" _
& Chr(13) & "D.1" _
& Chr(13) & "F2B-4"
End If
rngComboBox6.Text = sssaText
ActiveDocument.Bookmarks.Add "bmfcs", rngComboBox6
If Me.ComboBox6.Value = "No" Then
ComboBox6.Text = ""
End If
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, Password:="password"
End Sub
The first End If is in the wrong place. You are adding text to the document regardless of the value of the combo box.
It is not good practice to use a control’s change event to commit changes to a document. Apart from anything else it doesn’t allow the user to cancel without making changes.
Instead use an OK/Apply/Finish button.
Then you only need to unprotect/reprotect the document once.

Import text file from drop-down in Excel

I am trying to create a macro in Excel that imports a text file based on values in cells on a worksheet. One cell specifies the path of the directory, a second cell with a drop-down contains the file's name including extension.
I've tried to record a macro wherein I import a file from the directory in question, but I am having trouble changing it so that it uses the two cells with the path and file name instead of constant values.
The original recorded macro looks as follows:
Sub txt_import()
' txt_import Macro
ActiveWorkbook.Queries.Add Name:="testfile", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""D:\testpath\testfile.txt""),[Delimiter="";"", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Change Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Colu" & _
"mn4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Change Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=testfile;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [testfile]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "_testfile"
.Refresh BackgroundQuery:=False
End With
End Sub
The directory path is located on Sheet1 A1 and the drop-down list with the file names is in Sheet3 A2.
Any help is greatly appreciated!
You'll need to modify the Source query string, try:
Sub txt_import()
' txt_import Macro
Dim path as String, filename as String, fullFileName as String
path = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
filename = ThisWorkbook.Worksheets("Sheet3").Range("A2").Value
fullFileName = path & Application.PathSeparator & filename
ActiveWorkbook.Queries.Add Name:="testfile", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(" & fullFileName & "),[Delimiter="";"", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Change Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Colu" & _
"mn4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Change Type"""

Cell reference to a powerquery

I have a Power Query in which I'm trying to get the link in a cell Z13 from "Shift Sched" sheet. The link in Z13 will get me the excel file in which I have the data. Cell name is defined as REFCELL
Here is a wat i got from record macro.
Sub powerquery()
'
' powerquery Macro
'
'
Range("W13").Select
ActiveWorkbook.Queries.Add Name:="Schedules", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(Web.Contents(""https://portal.ant.website.com/sites/wfl/Shift Schedules/Shift Schedules and Emp Details w.e.f Jun 03, 2018-HYD.xlsx""), null, true)," & Chr(13) & "" & Chr(10) & " Schedules_Sheet = Source{[Item=""Schedules"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Schedules_Sheet,{{""Column1"", type text}, {""Column2"", type" & _
" text}, {""Column3"", type text}, {""Column4"", type any}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type any}, {""Column8"", type any}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text}, {""Column14"", type text}, {""Column15"", type text}, {""Column16"", type" & _
" text}, {""Column17"", type text}, {""Column18"", type text}, {""Column19"", type any}, {""Column20"", type text}, {""Column21"", type text}, {""Column22"", type text}, {""Column23"", type any}, {""Column24"", type any}, {""Column25"", type any}, {""Column26"", type any}, {""Column27"", type any}, {""Column28"", type any}, {""Column29"", type any}, {""Column30"", ty" & _
"pe any}, {""Column31"", type text}, {""Column32"", type any}, {""Column33"", type text}, {""Column34"", type text}, {""Column35"", type text}, {""Column36"", type any}, {""Column37"", type text}, {""Column38"", type text}, {""Column39"", type any}, {""Column40"", type any}, {""Column41"", type any}, {""Column42"", type text}, {""Column43"", type any}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""C" & _
"hanged Type"""
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Schedules;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Schedules]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Schedules_2"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End Sub
**This is not working for me because the link keeps changing depending on the date ex. Jun 03, 2018. The link has to be taken from the cell z13 in "Shift Sched" Sheet and load in a new sheet.
It's nearly all related to getting your "" quotation marks in the right place to asemble your query string correctly. Try this:
ActiveWorkbook.Queries.Add _
Name:="Schedules", _
Formula:="let" & vbCr & _
vbTab & "MyURL = Excel.CurrentWorkbook(){[Name=""REFCELL""]}[Content][Column1]{0}," & vbCr & _
vbTab & "Source = Web.Page(Web.Contents(MyURL))" & vbCr & _
"in Source"

Get table from website using VBA with dynamic web address

I'm trying to insert a table from a website into excel, specifically off of WSJ futures. There's a calendar feature I can get around by changing the web address. I would like to make the website in the code a cell reference so I can make the address dynamic. Below is the code for the regular macro with no dynamic address. Suggestions on how to make this change? I've tried taking out the URL and making it a cell reference, but it did not work.
ActiveWorkbook.Queries.Add Name:=Range("A1"), Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""http://www.wsj.com/mdc/public/page/2_3023-fut_metal-futures-20170901.html?mod=mdc_pastcalendar""))," & Chr(13) & "" & Chr(10) & " Data5 = Source{5}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data5,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Colu" & _
"mn6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})," & Chr(13) & "" & Chr(10) & " #""Removed Top Rows"" = Table.Skip(#""Changed Type"",1)," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(#""Removed Top Rows"", [PromoteAllScalars=true])" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Promoted Headers"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 5"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 5]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_5"
.Refresh BackgroundQuery:=False
End With
End Sub

Excel VBA Type Mismatch for #N/A

I have this chunk of code
With Data.Cells(rowMatch, GWECol)
.Value = Cmp.Cells(i, GWENetPr)
.AddComment
.Comment.Text Text:=UCase(Environ("UserName")) & ":" & vbNewLine _
& "Comment: " & Cmp.Cells(i, CommCol) & vbNewLine _
& "Transaction: " & Cmp.Cells(i, QRTran) & vbNewLine _
& "QR Pr: " & Cmp.Cells(i, QRPr) & vbNewLine _
& "QR WD: " & Cmp.Cells(i, QRWD) & vbNewLine _
& "QR WD All: " & Cmp.Cells(i, QRWDA) & vbNewLine _
& "QR XPr: " & Cmp.Cells(i, QRXPr) & vbNewLine _
& "QR XAll: " & Cmp.Cells(i, QRXAll) & vbNewLine _
& "GWE Pr: " & Cmp.Cells(i, GWEPr) & vbNewLine _
& "GWE All: " & Cmp.Cells(i, GWEAll) & vbNewLine _
& "GWE XPr: " & Cmp.Cells(i, GWEXPr) & vbNewLine _
& "GWE XAll: " & Cmp.Cells(i, GWEXAll)
.Comment.Shape.TextFrame.AutoSize = True
End With
Where the Cmp.Cells(i, X) refers to cells that may have #N/A error (a failed VLOOKUP).
Is it possible to have the code just take in the #N/A as a string or just leave it empty? Right now, whenever one of the cells referenced is #N/A, the chunk will fail and no comment text will be added at all.
Thanks!
You're using the default property of the cell,
Debug.Print Cmp.Cells(i, QRXAll)
For example this always refers to the cells .Value property. The .Value is actually an error type, Error 2042 which I think you could avoid by checking
CLng(Cmp.Cells(i,QRXA11))
But this will result in 2042 instead of the #N/A text.
If you want to get the string #N/A: try using Cmp.Cells(i, QRXAll).Text which relies on the cell's .Text property instead of its .Value.
Debug.Print Cmp.Cells(i, QRXAll).Text
Disclaimer: I have done some VBA programming, but I wouldn't call myself an expert.
This may be overly simplistic, but you could just assign each value to a variable and then assign the variables to the comment. If any one value is N/A, at least the rest of your values will still be assigned to the comment. I perfer this kind of solution as it ensures that a single error will not derail the entire operation.
Dim vComment As String
Dim vTransaction As String
Dim vQRPr As String
Dim vQRWD As String
' Etc.
vComment = Cmp.Cells(i, CommCol).Text
vTransaction = Cmp.Cells(i, QRTran).Text
vQRPr = Cmp.Cells(i, QRPr).Text
vQRWD = Cmp.Cells(i, QRWD).Text
' Etc.
.Comment.Text Text:=UCase(Environ("UserName")) & ":" & vbNewLine _
& "Comment: " & vComment & vbNewLine _
& "Transaction: " & vTransaction & vbNewLine _
& "QR Pr: " & vQRPr & vbNewLine _
& "QR WD: " & vQRWD & vbNewLine
' Etc.
Edited: Thanks to David for pointing out that the .Text property should be used
use IsError to check to see if the cells has #N/A
if IsError(Cmp.Cells(i, GWENetPr)) then
'give it a valid value
else
'use the value int he cell
end if
'start with statement
example
With Data.Cells(rowMatch, GWECol)
If IsError(Cmp.Cells(i, GWENetPr)) Then
.Value = "" 'or #N/A
Else
.Value = Cmp.Cells(i, GWENetPr)
End If
.AddComment
.Comment.Text Text:=UCase(Environ("UserName")) & ":" & vbNewLine _
& "Comment: " & Cmp.Cells(i, CommCol) & vbNewLine _
& "Transaction: " & Cmp.Cells(i, QRTran) & vbNewLine _
& "QR Pr: " & Cmp.Cells(i, QRPr) & vbNewLine _
& "QR WD: " & Cmp.Cells(i, QRWD) & vbNewLine _
& "QR WD All: " & Cmp.Cells(i, QRWDA) & vbNewLine _
& "QR XPr: " & Cmp.Cells(i, QRXPr) & vbNewLine _
& "QR XAll: " & Cmp.Cells(i, QRXAll) & vbNewLine _
& "GWE Pr: " & Cmp.Cells(i, GWEPr) & vbNewLine _
& "GWE All: " & Cmp.Cells(i, GWEAll) & vbNewLine _
& "GWE XPr: " & Cmp.Cells(i, GWEXPr) & vbNewLine _
& "GWE XAll: " & Cmp.Cells(i, GWEXAll)
.Comment.Shape.TextFrame.AutoSize = True
End With
You can use IIf to use a specific value if there is an error:
& "Comment: " & IIf(IsError(Cmp.Cells(i, CommCol)),"",Cmp.Cells(i, CommCol)) & vbNewLine _