Using of Indirect in Excel VBA, I think...

Emperor

Board Regular
Joined
Mar 25, 2010
Messages
225
Hi all,

I have, hopefuly, a simple question;

In a string I have the column letter in which the data needs to come

So I need something like;

Code:
dim ws as Worksheets
dim ws2 as Worksheets
dim StartCLM as string

set ws = Worksheets("Sheet1")
set ws2 = Worksheets("Sheet2")

StartCLM = ws.range("A1").value
'Now StartCLM is "D" (in text)

ws2.range(StartCLM & "2").value = ws.range("A1").value
'So I would want to change the value of WS2.range("D2")

I think I need to use Indirect but I wouldnt know how.
Any help will be welcome!

Mathijs.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
What you have should work, but you should change

Dim ws As Worksheets
Dim ws2 As Worksheets

to just

Dim ws As Worksheet
Dim ws2 As Worksheet
 
Upvote 0
You don't need indirect in code

JONMO is right you've used the wrong object
Code:
dim ws as Worksheet
dim ws2 as Worksheet
dim StartCLM as string
 
set ws = Worksheets("Sheet1")
set ws2 = Worksheets("Sheet2")
 
StartCLM = ws.range("A1").value
'Now StartCLM is "D" (in text)
 
ws2.range(StartCLM & "2").value = ws.range("A1").value
'So I would want to change the value of WS2.range("D2")
[COLOR=red]''' At this point the value of D2 = "D"[/COLOR]

What are you trying to achieve?
 
Upvote 0
I wan't to use the value of cell A1 in a range.
The value is a "D"

So I want to set a range to "D2", problem is I can't set it like this.
I am getting an error on "ws2.range(StartCLM & "2").value"
a method range faillure....

ps. You and Jonmo are right about the Worksheet, I just made a typo in the post, sorry
 
Upvote 0
StartCLM = ws.range("A1").value

Are you sure Sheet1 A1 = D ???

At the time you get the error, and you click Debug
Hover your mouse over the StartCLM variable...
It will popup a window displaying it's current value.
 
Upvote 0
Do you mean something like this?


Rich (BB code):
Sub Test()
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim rg As Range
Dim StartCLM As String
 
Set ws = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
 
StartCLM = ws.Range("A1").Value
'Now StartCLM is "D" (in text)
 
Set rg = ws2.Range(StartCLM & 2)
'So I would want to change the value of WS2.range("D2")
''' At this point the value of D2 = "D"

End Sub
 
Upvote 0
Thank all of you for the replies.
I have got it working (there was a little flaw), sort of.

Maybe you can help me with this;
the data i'm copying is in a range:
ws2.Range("B8:J8")

The place where I am pasting it starts at D2, but when I only define D2 in my code, it doesnt fill E2, F2, etc
So how can I paste a "range" to a "range starting at D2" ?
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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