ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem referencing a cell using 2 variables (https://www.excelbanter.com/excel-programming/415361-problem-referencing-cell-using-2-variables.html)

[email protected]

Problem referencing a cell using 2 variables
 
I am having trouble referencing a cell using two variables (in this
case the variables are tbindex1 and chindex1). II have included the
code below. Anyone know a solution?

Sub PrintMacro()

Dim wks As Worksheet
Dim N As Long
Dim TableRange1 As String
Dim TableRange2 As String
Dim TableRange3 As String
Dim TableRange4 As String
Dim TableRange5 As String
Dim TableRange6 As String
Dim ChartRange1 As String
Dim ChartRange2 As String
Dim ChartRange3 As String
Dim ChartRange4 As String




'Define ranges
mdindex1 = Sheets("Cover").range("E13").Value
modelrange = "B5:" + mdindex1 + "$36,B38:" + mdindex1 + "83"
tbindex1 = Sheets("Cover").range("E14").Value
chindex1 = Sheets("Cover").range("D18").Value
chindex2 = Sheets("Cover").range("D19").Value
chindex3 = Sheets("Cover").range("D20").Value
chindex4 = Sheets("Cover").range("D21").Value


TableRange1 = "$A$4:$" + tbindex1 + "$45"
TableRange2 = "$A$47:$" + tbindex1 + "$88"
TableRange3 = "$A$91:$" + tbindex1 + "$132"
TableRange4 = "$A$135:$" + tbindex1 + "$176"
TableRange5 = "$A$179:$" + tbindex1 + "$220"
TableRange6 = "$A$223:$" + tbindex1 + "$264"


ChartRange1 = "$c$4:$" + tbindex1 + "$" + chindex1
ChartRange2 = "$c$47:$" + tbindex1 + "$" chindex2
ChartRange3 = "$c$91:$" + tbindex1 + "$" chindex3
ChartRange4 = "$c$135:$" + tbindex1 + "$" chindex4



joel

Problem referencing a cell using 2 variables
 
Change plus signs to Ampersand ("&"). String are combined with & not plus
signs. When you use plus signs VBA thinks yo are combining numbers not
strings.

modelrange = "B5:" & mdindex1 & "$36,B38:" & mdindex1 & "83"

" wrote:

I am having trouble referencing a cell using two variables (in this
case the variables are tbindex1 and chindex1). II have included the
code below. Anyone know a solution?

Sub PrintMacro()

Dim wks As Worksheet
Dim N As Long
Dim TableRange1 As String
Dim TableRange2 As String
Dim TableRange3 As String
Dim TableRange4 As String
Dim TableRange5 As String
Dim TableRange6 As String
Dim ChartRange1 As String
Dim ChartRange2 As String
Dim ChartRange3 As String
Dim ChartRange4 As String




'Define ranges
mdindex1 = Sheets("Cover").range("E13").Value
modelrange = "B5:" + mdindex1 + "$36,B38:" + mdindex1 + "83"
tbindex1 = Sheets("Cover").range("E14").Value
chindex1 = Sheets("Cover").range("D18").Value
chindex2 = Sheets("Cover").range("D19").Value
chindex3 = Sheets("Cover").range("D20").Value
chindex4 = Sheets("Cover").range("D21").Value


TableRange1 = "$A$4:$" + tbindex1 + "$45"
TableRange2 = "$A$47:$" + tbindex1 + "$88"
TableRange3 = "$A$91:$" + tbindex1 + "$132"
TableRange4 = "$A$135:$" + tbindex1 + "$176"
TableRange5 = "$A$179:$" + tbindex1 + "$220"
TableRange6 = "$A$223:$" + tbindex1 + "$264"


ChartRange1 = "$c$4:$" + tbindex1 + "$" + chindex1
ChartRange2 = "$c$47:$" + tbindex1 + "$" chindex2
ChartRange3 = "$c$91:$" + tbindex1 + "$" chindex3
ChartRange4 = "$c$135:$" + tbindex1 + "$" chindex4





All times are GMT +1. The time now is 03:12 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com