VBA Convert string to range

cameronb

Board Regular
Joined
Feb 13, 2009
Messages
146
I am designing a from to create a graph based on data which is pasted into a specific sheet in a workbook. The form has two cells which has the letter reference for the data source for a graph.

In my code, I am attempting to convert the letter stored as a string into a range. The code has an error at line:

Nalastrow = Sheets("Datadrop").Range(" & Na & 65536 ").End(xlUp).Row

Can anyone assist with the correct syntax for converting strings to ranges?

Code:
Sub Chartgraph()
Dim Na As String
Dim Ca As String
Dim Nalastrow As Long
Dim Calastrow As Long
Dim Nar As Range
Dim Car As Range
 
Na = Application.WorksheetFunction.Trim(Sheets("Key Stats").Range("B8").Value) 'cell with column reference
Ca = Application.WorksheetFunction.Trim(Sheets("Key Stats").Range("B9").Value) 'cell with column reference
 

Nalastrow = Sheets("Datadrop").Range(" & Na & 65536 ").End(xlUp).Row
Calastrow = Sheets("Datadrop").Range(" & Ca & 65536 ").End(xlUp).Row

Na = Na & 2
Ca = Ca & 2
Set Nar = Sheets("Datadrop").Range(" & Na & : & Na & Nalastrow")
Set Car = Sheets("Datadrop").Range(" & Ca & : & Ca & Calastrow")
[CODE\]
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try

Code:
Nalastrow = Sheets("Datadrop").Range(" & Na & ", Rows.Count).End(xlUp).Row
 
Upvote 0
Na and Ca are the declared strings which correspond to the columns which will form data source for the chart.

e.g. Na = "H" - meaning column H
Ca = "J" - meaning column J


For

Nalastrow = Sheets("Datadrop").Range(" & Na & ", Rows.Count).End(xlUp).Row

I get an error showing Nalastrow as o?
 
Upvote 0
Sorry, it should be

Code:
Nalastrow = Sheets("Datadrop").Range(" & Na & " & Rows.Count).End(xlUp).Row
 
Upvote 0
Nalastrow = Sheets("Datadrop").Cells(Na, Rows.Count).End(xlUp).Row
 
Upvote 0
I am still having a code error with line :

Nalastrow = Sheets("Datadrop").Range(" & Na & " & Rows.Count).End(xlUp).Row

Any suggestions?

Code:
Sub Chartgraph()
Dim Na As String
Dim Ca As String
Dim Nalastrow As Long
Dim Calastrow As Long
Dim Nar As Range
Dim Car As Range
 
Na = Application.WorksheetFunction.Trim(Sheets("Key Stats").Range("B8").Value) 'cell with column reference
Ca = Application.WorksheetFunction.Trim(Sheets("Key Stats").Range("B9").Value) 'cell with column reference
 
Nalastrow = Sheets("Datadrop").Range(" & Na & " & Rows.Count).End(xlUp).Row
Calastrow = Sheets("Datadrop").Range(" & Ca & " & Rows.Count).End(xlUp).Row
 

Set Nar = Sheets("Datadrop").Range(" & Na & 2 & : & Na & Nalastrow")
Set Car = Sheets("Datadrop").Range(" & Ca & 2 & : & Ca & Calastrow")
 
Upvote 0
This

Nalastrow = Sheets("Datadrop").Cells(Na, Rows.Count).End(xlUp).Row
should be
Nalastrow = Sheets("Datadrop").Cells(Rows.Count, Na).End(xlUp).Row

The syntax for Cells is

Cells(Row#, Column # OR Letter)


Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,749
Members
452,940
Latest member
rootytrip

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