I need to insert data from a VB6 Textbox into a designated Cell on a specific page in a specific Excel Worksheet. Additionally, I would like to be able to insert the information into another designated Cell if the original targeted Cell contained any data. My guess is that I would need to open the specific Excel Worksheet from my VB Project and then instruct the procedure to insert the TextBox.text data into the designated Cell on the specific page of the Worksheet. I would greatly appreciate any help. The people on this site have been very wonderful to me! Thank you!
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Inserting VB Data Into Excel (VB6)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Inserting VB Data Into Excel (VB6)
- This topic has 5 replies, 4 voices, and was last updated 20 years, 4 months ago.
AuthorTopicWSRobertENichols
AskWoody LoungerJanuary 16, 2005 at 2:54 am #414607Viewing 1 reply threadAuthorReplies-
WSAlanMiller
AskWoody LoungerJanuary 16, 2005 at 11:50 am #922323This is an adaptation of code found on VBForums.com, which I’ve used for other purposes, so the following is untested. It assumes rngTarget1 is the first choice to insert the value of Textbox1, and rngTarget2 is the alternative. I hope it works, or is at least a start.
Option Explicit'Add reference to MS Excel xx.0 Object Library
Private moApp As Excel.ApplicationPrivate Sub Command1_Click()
Dim oWB As Excel.Workbook
Dim rngTarget1 As Range
Dim rngTarget2 As RangemoApp.Visible = True
Set oWB = moApp.Workbooks.Open("D:My DocumentsBook1.xls")' For example:
Set rngTarget1 = oWB.Sheets("Sheet1").Cells(1, 2)
Set rngTarget2 = oWB.Sheets("Sheet1").Cells(3, 4)If rngTarget1.Value "" Then
rngTarget2.Value = Textbox1.Value
Else
rngTarget1.Value = Textbox1.Value
End IfSet oWB = Nothing
End Sub
Private Sub Form_Load()
Set moApp = New Excel.Application
End Sub
Looking at this again, “off the fly”, it might be less messy to use the actual cell references in the IF block and avoid the range objects. e.g. oWB.Sheets(“Sheet1”).Range(“A5”).Value
Alan
-
WSRobertENichols
AskWoody LoungerJanuary 16, 2005 at 4:18 pm #922378Alan:
Thank you very much for your help. Unfortunately, I cannot quite get it to work. I am sure it is me and not the Code. I am sure that it is obvious that I am not a computer programmer. I am an attorney trying to assist in a pro bono child support project. Perhaps I bit off more than I could comprehend in one setting with my initial question. If you do not mind I would like to simplify it and later build upon that Code after I get it to work. Thus, my revised question is:
I would like to merely take textbox data found in Text1.Text from a VB6 form and insert it into cell C3 contained on Sheet1 of Book1 in Excel (found at C:Book1.xls). Any help you can give would be appreciated more than you can imagine. Thank you! Bob
-
WSHansV
AskWoody LoungerJanuary 16, 2005 at 8:15 pm #922400First, select Project | References, and tick the check box for Microsoft Excel n.0 Object Library. The value of n depends on the version of Microsoft Office installed on your PC (Excel 97 = 8, Excel 2000 = 9, Excel 2002 = 10 and Excel 2003 = 11).
Say that you have a command button cmdExport on the form containing the text box Text1. The On Click event procedure for this command button could look like this:
Private Sub cmdExport_Click()
Dim objXl As New Excel.Application
Dim objWb As Excel.WorkbookOn Error GoTo ErrHandler
Set objWb = objXl.Workbooks.Open(“C:Book1.xls”)
objWb.Worksheets(“Sheet1”).Range(“C3”) = Me.Text1.Text
objWb.Close SaveChanges:=TrueExitHandler:
On Error Resume Next
Set objWb = Nothing
objXl.Quit
Set objXl = Nothing
Exit subErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub -
WSRobertENichols
AskWoody Lounger
-
-
-
-
Anonymous
InactiveJanuary 16, 2005 at 3:14 pm #922373Here is some sample code from an actual program:
Public Sub GetXLitems(Index As Integer)
‘**************************************************************************
‘Purpose: Invoke Excel,
‘ call routines to load PIA spreadsheet
‘ and Factors spreadsheet
‘Inputs: N/A
‘Returns: N/A
‘Assumes: Excel is installed on computer where BenCalc runs
‘Effects: Invokes “extra” copy of Excel, alters various
‘ properties temporarily, then resets them
‘**************************************************************************Dim loXL As Object
‘Opens Excel
Set loXL = CreateObject(“Excel.Application”)
loXL.Visible = TrueCall GetFactorsXL(Index, loXL)
‘Now we can close the Excel application entirely
loXL.Quit
Set loXL = NothingEnd Sub
Private Sub GetFactorsXL(Index As Integer, loXL As Object)
‘**************************************************************************
‘Purpose: Load Factors spreadsheet,
‘ return factor arrays to VB program
‘Inputs: N/A
‘Returns: N/A
‘Assumes: Excel invoked by calling routine
‘Effects: Sets value of various global arrays
‘**************************************************************************Dim loXLwb As Object
Dim lsPath As String
Dim lvLSFac As Variant
Dim liCol As Integer
Dim liRow As Integer‘Loads workbook
Call FixAppPath(lsPath)
Set loXLwb = loXL.Workbooks.Open (filename:=lsPath & “Factors2.xls”)loXL.Sheets(“LS factors”).Select
‘This sets up variant array as lvLSFac(48,4) RxC
lvLSFac = loXL.Range(“B7:E54”).ValueFor liRow = 23 To 70
gfLSFac(liRow, 1) = CSng(lvLSFac(liRow – 22, 1))
gfLSFac(liRow, 2) = CSng(lvLSFac(liRow – 22, 4))
Next liRow‘Close current workbook, don’t save changes
loXLwb.Close savechanges:=False
Set loXLwb = NothingEnd Sub
Public Sub FixAppPath(asMyPath As String)
‘**************************************************************************
‘Purpose: Allow for handling of final backslash in App.Path
‘ If located in root, get the final backslash
‘ If not, don’t get the final backslash
‘Inputs: Empty string variable asMyPath
‘Returns: Run-time directory with trailing backslash
‘Assumes:
‘Effects:
‘**************************************************************************asMyPath = App.Path
If Not Right$(asMyPath, 1) = Chr$(92) Then
asMyPath = asMyPath + Chr$(92)
End IfEnd Sub
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Does windows update component store “self heal”?
by
Mike Cross
1 hour, 30 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
2 hours, 31 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
1 hour, 56 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
2 hours, 37 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
5 hours, 12 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
5 hours, 14 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
12 hours, 15 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
13 hours, 21 minutes ago -
0Patch, where to begin
by
cassel23
7 hours, 23 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 2 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
14 hours, 45 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 11 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day, 2 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
13 hours, 38 minutes ago -
Installer program can’t read my registry
by
Peobody
8 hours, 36 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
1 day ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 7 hours ago -
False error message from eMClient
by
WSSebastian42
1 day, 22 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 7 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 8 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
1 day, 5 hours ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 12 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
1 day, 13 hours ago -
Outdated Laptop
by
jdamkeene
2 days, 17 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
2 days, 23 hours ago -
Another big Microsoft layoff
by
Charlie
2 days, 23 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
1 hour, 7 minutes ago -
May 2025 updates are out
by
Susan Bradley
2 hours, 49 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 days, 5 hours ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
3 days, 5 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.