1

Below is my code to run and post my query results in Excel. The code pastes NULL as 0. I would like the NULLs to be a pasted as "NULL". I think some type of paste values would work or changing my table from a ".QueryTable" may help. I am not sure what to do. Any suggestions?

    'Import Data
    With queryOutputWS.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
        "ODBC;DRIVER=SQL Server;SERVER=W51SQP-********;Trusted_Connection=Yes;APP=Microsoft Office 2010" _
        ), Array(";DATABASE=*****")), Destination:=queryOutputWS.Range("A1")).QueryTable
        .CommandText = myQuery
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 0
        .PreserveColumnInfo = False
        .ListObject.DisplayName = "Query"
        .Refresh BackgroundQuery:=False
    End With
    queryOutputWS.ListObjects("Query").Unlist
1
  • Maybe try to change the column properties for those cells to text/generic (i.e not number). Commented Apr 30, 2018 at 20:51

1 Answer 1

1

I think it is easier to change the SQL query rather than the VBA code. If you later post the code of your query I could see if what I'm thinking applies, but nevertheless, you could try this:

Suppose the column you want to query and change the values is called Column1

In your query, change the SELECT [...,] Column1 [,...] FROM ... to:

SELECT [...,] IIF(Column1 IS NULL, 'NULL', Column1) [,...] FROM...

Let me know if that helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.