Import a cell in Google sheets as a VBA variable (in Word)

stuff84

New Member
Joined
Apr 24, 2022
Messages
3
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi everyone.
I am relatively new to VBA, and while I have found several threads to import entire ranges from a Google sheet to MS Excel, I haven't been able to find this answer.
I want to be able to produce a form in Google sheets, and use it to populate content control in a Word document.
The part I am getting stuck on is how do i import a single cell from a Google Sheet (A1), and use it as a string variable in VBA (for Word).
Thank you very much!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
To elaborate a little more I was able to figure this out in Google Sheets Scripting, but I am not as familiar with VBA.

In the following, a variable "NewVariable" will be pulled from the Google Sheet located at Variable to Import

Does anyone know how to do this in VBA (for Word, not Excel)?

Thank you!!

Code:
var NewVariable = ('Variable_from_Google_Sheet');
NewVariable = SpreadsheetApp.open(DriveApp.getFileById("1voJ22SzX10iJWTLMG9BEHELBgH2ShzU_BBklOGOZd4g")).getRange('A1').getValue();
 
Upvote 0
Me again, lol.
I am such a noob. I am hoping it is not just me, and there isn't a way that I can edit my posts to add more details (rather than me talking to myself in 3 posts, lol).
Anyways, I wanted to add, that I found this method that gets me 90% of the way there, using Query tables.
However, with this method, you must define a destination range for the imported data, when what I want is to use the range (of a single cell) and have it used as a variable.

Thank you all again for any suggestions.

Taken from: How to import data from Google Sheets into Excel with VBA

VBA Code:
Sub importGsheetLads()

t
 Dim keyString As String
  
 Dim gidString As String
 
 
 
 '' change the below keyString and gidString values as required
 
 
 keyString = "14hOICbxkYfCPpwpf8E6kwQHjrp2b33KjWfjMzjqGG8E"
 
 gidString = "493421964"
 
 
 
 
 
  
     With ActiveSheet.QueryTables.Add(Connection:="URL;https://spreadsheets.google.com/tq?tqx=out:html&tq=&key=" & keyString & "&gid=" & gidString, Destination:=Range("$A$1"))
        .Name = "q?s=goog_2"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "1,2"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
        
        
 End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top