Dynamic range condensed into one line of code - vba

I am trying to figure out the cleanest way to create a dynamic range and this, I think, is close to what I want it to do but I am not sure how to make it correct. Any thoughts?
Sub Macro1()
Dim RNG As Range
With Sheets("Open Jobs Report") 'Change to your sheet
Set RNG = .Range("A1", .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column))
End With

Try this:
Dim RNG As Range
Set RNG = .Range("A1", .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column))
Because you were using . in front of some of the range objects I assume it is in a With Block like this:
Sub foooooii()
Dim RNG As Range
With Sheets("Sheet3") 'Change to your sheet
Set RNG = .Range("A1", .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column))
End With
Debug.Print RNG.Address
End Sub

building on Scott's solution and just to shorten it a little:
Set RNG = .Range(.Cells(.Rows.Count, "A").End(xlUp), .Cells(1, .Columns.Count).End(xlToLeft))

Related

Selecting a range from a specific cell and then special paste that range using VBA

i have an excel file in which i have data month wise, so i want to select Column F, G and H from the active cell till the last data of that column and then special paste it.
I am using this Code for selecting that range but not able to do that. it is selecting the data from the F1.
Sub selecting_range()
Dim rng As Range
Dim LastRow As Long
currentcell = ActiveCell
LastRow = Cells(Rows.Count, "F" & currentcell).End(xlUp).Row
Set rng = Range("F1:H" & LastRow)
rng.Select
End Sub
Considering the fact that the "F" and "H" are hardcoded, then you can build up something like this:
Sub SelectingRange()
Dim rng As Range
Dim lastRow As Long
lastRow = Cells(Rows.Count, "F").End(xlUp).Row
Set rng = Range(Cells(ActiveCell.Row, "F"), Cells(lastRow, "H"))
rng.Select
End Sub
Or you can write it in 1-line, just to confuse someone:
Sub SelectingRange()
Range(Cells(ActiveCell.Row, "F"), Cells(Cells(Rows.Count, "F").End(xlUp).Row, "H")).Select
End Sub

Get rid of .Select in VBA Code

I would like to get rid of the sht2.Select and sht2.Range("B2").Select in the code below. Is there a way to do this?
Sub Remaining()
Dim sht2 As Worksheet
Dim cell As Range
Set sht2 = ThisWorkbook.Worksheets("Sheet2")
sht2.Select
sht2.Range("B2").Select
With sht2
For Each cell In .Range("B2", Cells(Rows.Count, "B").End(xlUp))
If .Range("A:A").Find(What:=cell.Value2, LookAt:=xlWhole) Is Nothing Then
Intersect(.UsedRange, cell.EntireRow).Offset(, 1).Copy Sheets("Sheet1").Cells(Rows.Count, "L").End(xlUp).Offset(1)
cell.Interior.Color = vbYellow
End If
Next cell
End With
End Sub
Sub Remaining()
Dim sht2 As Worksheet
Dim cell As Range
Set sht2 = ThisWorkbook.Worksheets("Sheet2")
With sht2
'A few fixes in the following line to make sure everything
' is referencing the correct sheet
For Each cell In .Range(.Range("B2"), .Cells(.Rows.Count, "B").End(xlUp))
If .Range("A:A").Find(What:=cell.Value2, LookAt:=xlWhole) Is Nothing Then
Intersect(.UsedRange, cell.EntireRow).Offset(, 1).Copy _
Sheets("Sheet1").Cells(Rows.Count, "L").End(xlUp).Offset(1)
cell.Interior.Color = vbYellow
End If
Next cell
End With
End Sub

Copy a range and shift it down one row on the same sheet

I am fairly new in excel VBA and would really appreciate any help on this matter.
The workbook includes data from range A5:AZ1000 (new client info is inputted in new rows, but some cells may be empty depending on the nature of the case). When a user inputs new client info (begins a new row) I would like the existing data (range A5:AZ1000) to shift down one row, and a blank row to appear in range A5:AZ:5. I would like users to be able to click a macro "New Client" for this to happen.
It should be noted that this is a shared workbook and therefore I cannot have macro that adds a new row.
Here is the code I'm working with:
Sub shiftdown()
' shiftdown Macro
Dim lastRow As Long
Dim lastColumn As Long
Dim rngToCopy As Range
Dim rng As Range
Set rng = ActiveSheet.Range("A1").Value
lastColumn = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column
lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
If rng > 0 Then
ActiveSheet.Range("A5" & lastRow).Select
Selection.Copy
PasteSelection.Offset(1, 0).PasteSpecial xlPasteValues
'Error Object Required
End If
End Sub
Normally I wouldn't answer if the question doesn't include any code to show effort, but I started writing the below while the question actually did show code so I may as well provide it. It may achieve what you are after.
Sub shiftdown()
' shiftdown Macro
Dim rng As Range
With ActiveSheet
If .Range("A1").Value > 0 Then
Set rng = .Range(.Cells(5, 1), _
.Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, _
.Cells(4, .Columns.Count).End(xlToLeft).Column))
rng.Offset(1, 0).Value = rng.Value
.Rows(5).EntireRow.ClearContents
End If
End With
End Sub
Set rng = ActiveSheet.Range("A1").Value ???
if rng is a range, then replace it by :
Set rng = ActiveSheet.Range("A1")
or if rng is a variable, replace
Dim rng As Range
by
Dim rng As variant
rng = ActiveSheet.Range("A1").Value
another error :
you declared rng as range and then you test if it is > 0
If rng > 0 Then ...
it is not possible

dynamic rows and columns in VBA Chart

so i am trying to write some VBA that can look at "sheet2" and determine how many rows and columns it needs to make in its line chart
i'm having difficulty in declaring range of .SetSourceData in terms of variable i need it to start at cell(1,4) and go to Cell(LastRow, LastColumn). when i try i get a error
'method 2
Dim LastRow As Long
Dim LastColumn As Long
Dim rng2 As Range
Dim ShName As String
With ActiveSheet
LastRow = ThisWorkbook.Sheets("Sheet2").Range("G" & .Rows.Count).End(xlUp).Row
LastColumn = ThisWorkbook.Sheets("Sheet2").Cells(1, Columns.Count).End(xlToLeft).Column
Set rng2 = ThisWorkbook.Sheets("Sheet2").Range(.Sheets("Sheet2").Cells(2, 4), .Sheets("Sheet2").Cells(LastRow, LastColumn))
' ThisWorkbook.Sheets("Sheet2").Range("R2").Value = rng2
ShName = .Name
End With
Charts.Add
With ActiveChart
.ChartType = xlLine
.SetSourceData Source:=rng2
End With
I try use you code and get error in line:
Set rng2 = ThisWorkbook.Sheets("Sheet2").Range(.Sheets("Sheet2").Cells(2, 4), .Sheets("Sheet2").Cells(LastRow, LastColumn))
try add ThisWorkbook before .Sheets:
Set rng2 = ThisWorkbook.Sheets("Sheet2").Range(ThisWorkbook.Sheets("Sheet2").Cells(2, 4), ThisWorkbook.Sheets("Sheet2").Cells(LastRow, LastColumn))
or remove dot before Sheets:
Set rng2 = ThisWorkbook.Sheets("Sheet2").Range(Sheets("Sheet2").Cells(2, 4), Sheets("Sheet2").Cells(LastRow, LastColumn))

Concatenate Cell Values Dynamically And Copy Result to Another Worksheet

I have a worksheet ("Data") with x-columns and y-rows. I want to concatenate the values of a row and the concatenated result should be copied to a second worksheet ("Insert") in the first column of the same row.
I tried this VBA and get an error message
Sub InsertStatementRow()
Dim x As String, rng As Range, rng1 As Range, cel As Range
Dim ColMax As Integer
Dim i As Long
Sheets("Data").Select
Range("A1").Select
ColMax = Cells(1, Columns.Count).End(xlToLeft).Column
With Worksheets("Data")
i = 1
Set rng = Range(Cells(i, 1), Cells(i, ColMax))
End With
For Each cel In rng
x = x & cel.Value
Next
Range(Sheets("Insert").Cells(i, 1)).Value = x
End Sub
Please show me what I am doing wrong by correcting my code. Thanks!
Use some "." :
Set rng = Range(.Cells(i, 1), .Cells(i, ColMax))