Solved

Referring to Cells in Excel

Posted on 2008-10-06
10
157 Views
Last Modified: 2010-04-30
Hello experts,
How can I create the following strings in VB ?

c11
c12
c13
c14
c15
...

?
I can not do a Do While loop using "C" + counter because one is a string and the other is an integer.
Sorry for posting a silly question. I had the same problem in Access and it worked to do a Me("C" & i) but that does not work here.
Thanks
0
Comment
Question by:Mannsi
10 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 22648037
Assuming you have a worksheet object, treat the columns as an integer (C = 3) and use the Cells object:

   TargetWorksheet.Cells(Row, Column)

Kevin
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 22648039
Can you pls expand on your question - it is not apparent what you are wanting to do with these strings, and how Excel fits in

Regards

Dave
0
 

Author Comment

by:Mannsi
ID: 22648072
I've got a few SQL queries that I run in VB, each giving me a single result. I then "paste" the results of the queries in Excel. The strings are supposed to guide VB to where the results should go. They need to be strings because the function that prints the result of the query says so.
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 22648098
Then you can use the Range method:

   TargetWorksheet.Range("C1") = Recordset!Field1

Kevin
0
 

Author Comment

by:Mannsi
ID: 22648152
I don't have a recordset, I use the below function.


Sub SendQuery(sDestRange As String, sODBC As String, sSQL As String)

    With ActiveSheet.QueryTables.Add(Connection:=sODBC,    Destination:=Range(sDestRange))

        .Sql = sSQL

        .FieldNames = False

        .RefreshStyle = xlInsertDeleteCells

        .RowNumbers = False

        .FillAdjacentFormulas = False

        .RefreshOnFileOpen = False

        .HasAutoFormat = False

        .BackgroundQuery = True

        .TablesOnlyFromHTML = True

        .Refresh BackgroundQuery:=False

        .SavePassword = True

        .SaveData = True

    End With

End Sub

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 22648181
And you need to determine how to set sDestRange? Use a simple string:

   SendQuery "C1", "...", "..."

Kevin
0
 

Author Comment

by:Mannsi
ID: 22648201
Yeah, but I have a loop for the SQL query that would be very nice to be able to use for the sDestRange instead of having to write the whole thing 50 times.
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 22648216
Dim Row As Long
For Row = 11 To 60
    SendQuery "C" & Row, "...", "..."
Next Row

Kevin
0
 
LVL 8

Expert Comment

by:mikainz
ID: 22648241
you can convert a integer into a string type by using the STRING function
0
 

Author Comment

by:Mannsi
ID: 22648242
lol, didn't try doing a "c" & i for my strings, just assumed that since "c" + i didn't work, neither would the other one. Thanks for the answer.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now