0

Hello I am making a macros to plot some data, the point is to receive from the user two columns to plot, i tried first getting this values with two input boxes:

first = InputBox("first column", "Enter a Column")
two = InputBox("second column", "Enter a Column")

The problem with this approach is that i have to do the following in order to plot:

Set rng = .Range(first + "2:" + first + "84," + two + "2:" + two + "84")

The idea is to receive the value to then concatenate it with the "2" and "84" that are my range, I would like to make this macros more flexible since i am always using the same range from 2 to 84, i don't understand very well active columns, I would like to appreciate any suggestion to overcome this situation, my complete code looks as follows:

Sub ALL()
    Dim ws As Worksheet
    Dim rng As Range
    Dim objChrt As ChartObject
    Dim chrt As Chart

    Set ws = ThisWorkbook.Sheets("DATA")

    With ws
        first = InputBox("first column", "Enter a Column")
        two = InputBox("second column", "Enter a Column")
        Set rng = .Range(first + "2:" + first + "84," + two + "2:" + two + "84")
        .Shapes.AddChart
        Set objChrt = .ChartObjects(.ChartObjects.Count)
        Set chrt = objChrt.Chart

        With chrt
            .ChartType = xlColumnClustered
            .SetSourceData Source:=rng
            End With
        End With
End Sub
3
  • I don't fully get the question but maybe you can use Set rng = Application.InputBox(prompt:="Select a cell", Type:=8) msdn.microsoft.com/en-us/library/office/… Commented Aug 15, 2016 at 3:26
  • Thanks, I appreciate the support the main idea is to get the two ranges just selecting the columns on the fly. just selecting two columns with the same dimensions. Commented Aug 15, 2016 at 3:36
  • Did you try: .SetSourceData Source:=Selection Commented Aug 15, 2016 at 5:50

1 Answer 1

1

I still don't understand the question so maybe a picture or example might help.
Another way to get column range is like this

Set rng = ThisWorkbook.Sheets("DATA").Range("C2:C84")
MsgBox rng.Columns("B").Address(0, 0)  ' D2:D84
MsgBox rng.Columns(-1 ).Address(0, 0)  ' A2:A84
MsgBox Union( rng.Columns(2), rng.Columns(4) ).Address(0, 0)  ' D2:D84,F2:F84

If the data is for example in range B2:D4 and there are only blank cells around it, then you can get that range with Range("C3").CurrentRegion

Sign up to request clarification or add additional context in comments.

2 Comments

This was really helpful however I just want to select two columns and then call my macros to plot them,
This is not exactly what I wanted but I find it really helpful I really appreciate the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.