Simple String Question

jnewlin

New Member
Joined
Aug 2, 2007
Messages
18
Easy question here. I am trying to reference a string from one worksheet and display it on anther worksheet.

Code is below. It works if I use

Name1 = Range("B2").Value

for example, but I want it to use the user input row labeled LSG below.

Name1 = Range("B" & LSG).Value

does not work. Why? I'm guessing it's a simple keyword I'm missing.

Sub SG1()
Dim LSG As Integer
Dim Name1 As String

Sheets("Size").Select
LSG = Range("J2").Value
Name1 = Range("B" & LSG)
Sheets("Sheet1").Select
Range("B13").Value = Name1
...

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
It's not necessary to do all that selecting. LSG should be dimmed as Long as the number of rows available exceeds the amount allowed by the Integer data type.

Assuming J2 on sheet Size holds a valid row number:

Code:
Sub SG1()
Dim LSG As Long
LSG = Sheets("Size").Range("J2")
Sheets("Sheet1").Range("B13") = Sheets("Size").Range("B" & LSG)
End Sub
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,077
i always thought it had to be a string in order for it to work in Range()

dim LSG as string

???

just assuming didn't run any tests or anything
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Hi

No need to select things - usually unnecessary but unfortunately slow. Do you get an error? If so what is it, and where does it occur?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

QuietRiot

No it doesn't need to be a string.:)

And really shouldn't be a string.:)
 

jnewlin

New Member
Joined
Aug 2, 2007
Messages
18
Never a need to select?

So there is never a need to use a line to select a sheet to move throughout the workbook? I can always reference within the same line of code the worksheet I'm using?

When I make combobox selections I see the screen flashing alot. Many values are getting repopulated, but is it because of this select command that the screen flashes and it takes a few seconds to populate all my data?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows
Well I wouldn't say there's never a need, that really depends on what you are doing.

But I think the code you posted doesn't need selecting.:)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,009
Messages
5,834,876
Members
430,325
Latest member
Thony

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
Top