I can´t range the cells interval

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
I created a code so that each time a record was inserted into a line the program would detect the last line with values (assigned to the variable "k") and select a specific interval for the graph to update, but an error occurs, could someone help me ?



VBA Code:
Public Sub Chart_Update()

Dim k As Long

k = Cells(Rows.Count, "B").End(xlUp).Row

Error
------------------------------------------------------------------
Range("B2:B&k,E2:E&k,I2:I&k,J2:J&k,K2:K&k").Select
--------------------------------------------------------------
ActiveChart.SetSourceData Source:=Range( _
"AC_Offset_Registers!$B$2:$B$&k,AC_Offset_Registers!$E$2:$E$&k,AC_Offset_Registers!$I$2:$I$&k,AC_Offset_Registers!$J$2:$J$&k,AC_Offset_Registers!$K$2:$K$&k" _
)
End Sub



Error:

Capturar.JPG
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
@KhallP You are not concatenating the range string correctly to incorporate the several instances of variable k.

Try.
VBA Code:
Range("B2:B" & k & ",E2:E" & k & ",I2:I" & k & ",J2:J" & k & ",K2:K" & k).Select
 
Upvote 0
Instead of all that concatenation you could use Intersect to get the range.
VBA Code:
Dim rng As Range
Dim k As Long

    With Sheets("AC_Offset_Registers")
        k = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rng = .Range("B2,E2,I2:K2")
        Set rng = Intersect(rng.EntireColumn, .Range("2:" & k)).Address
    End With

    ActiveChart.SetSourceData Source:=rng
 
Upvote 0
@KhallP You are not concatenating the range string correctly to incorporate the several instances of variable k.

Try.
VBA Code:
Range("B2:B" & k & ",E2:E" & k & ",I2:I" & k & ",J2:J" & k & ",K2:K" & k).Select


That error as ocurred after that:

Capturar.JPG




Code:

VBA Code:
Public Sub Chart_Update()

Dim k As Long

k = Cells(Rows.Count, "B").End(xlUp).Row

    Range("B2:B" & k, "E2:E" & k, "I2:I" & k, "J2:J" & k, "K2:K" & k).Select
    ActiveChart.SetSourceData Source:=Range( _
        "AC_Offset_Registers!$B$2:$B$&k,AC_Offset_Registers!$E$2:$E$&k,AC_Offset_Registers!$I$2:$I$&k,AC_Offset_Registers!$J$2:$J$&k,AC_Offset_Registers!$K$2:$K$&k" _
        )
End Sub
 
Upvote 0
Compare what Snakehips showed you:
VBA Code:
Range("B2:B" & k & ",E2:E" & k & ",I2:I" & k & ",J2:J" & k & ",K2:K" & k).Select
to what you last wrote:
VBA Code:
Range("B2:B" & k, "E2:E" & k, "I2:I" & k, "J2:J" & k, "K2:K" & k).Select

Note that the commas must be within the quotes. You have placed them outside.
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,112
Members
449,096
Latest member
provoking

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