Range in Excel VBA

2

I have the following code snippet:

ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;C:\Users\Contoso\Desktop\ArquivosDoContoso.csv", Destination:=Range _
    ("$A$1"))

In the Destination:=Range _ ("$A$1")) part, how do I put the name of the PivotTable?

For example: I created a PivotTable with the name TBDados , how can I make the Destination:=Range _ ("$A$1") be this table? Regardless of the line that the table starts?

I've tried Destination:=Range _ ("$TBDados$") , but it returns the following error:

  

The 'Range' method of the '_Global' object failed.

    
asked by anonymous 15.04.2015 / 13:38

3 answers

0

Edit:

Sub Main()
    'Altere aqui de acordo com sua necessidade:
    Const FILE_PATH As String = "c:\temp\exemplo.csv"

    Dim oList As Excel.ListObject
    Dim iFF As Integer
    Dim sLine As String
    Dim lRows As Long
    Dim lCols As Long
    Dim lRow As Long
    Dim vLines() As Variant

    'Altere aqui para sua necessidade:
    Set oList = ThisWorkbook.Worksheets("Plan1").ListObjects("Tabela1")

    oList.ListColumns(1).Range.Resize(, 4).ClearContents
    If oList.ListRows.Count > 1 Then
        oList.ListRows(2).Range.Resize(oList.ListRows.Count - 1).Delete
    End If

    iFF = FreeFile
    Open FILE_PATH For Input As #iFF
    Do Until EOF(iFF)
        Line Input #iFF, sLine
        lRows = lRows + 1
        ReDim Preserve vLines(1 To lRows)
        vLines(lRows) = sLine
    Loop
    Close #iFF

    oList.Resize oList.Range.Resize(lRows + 1)

    Application.Calculation = xlCalculationManual
    For lRow = 1 To UBound(vLines)
        oList.ListRows(lRow).Range(1).Resize(, 4) = Split(vLines(lRow), ";")
    Next lRow
    Application.Calculation = xlCalculationAutomatic

End Sub
    
20.04.2015 / 16:57
0

Hi, how are you? I've never used this method, but to select a PivotTable you can use PivotTables . In your case, eg:

Dim pt As PivotTable
Dim tabelarange As Range

Set pt = ActiveSheet.PivotTables("TBDados")
Set tabelarange = pt.TableRange1 'SÓ SELECIONA A TABELA E SEU CABEÇALHO, PARA SELECIONAR OS FILTROS USE TableRange2

ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;C:\Users\Contoso\Desktop\ArquivosDoContoso.csv", Destination:=tabelarange)

See if it works, unfortunately, now I can not test the QueryTables.Add .

    
15.04.2015 / 20:42
0

Pivot tables are reports mounted based on a data source. You can not overwrite it. The logical flow of assembling a report in Excel is: 1 - Define a data source 2 - Point a PivotTable for this data source.

In your case, (1) is represented by you create a QueryTable in a worksheet, say "helper" (which can be hidden) and (2) is to create and configure a PivotTable that points to that data source. / p>     

16.04.2015 / 15:17