dynamic rows and columns in VBA Chart - vba

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))

Related

Finding a value and then copying a column in which it occurs

I am trying to write a macro which will find a value "Spot price" in a header of a table2 and then take the whole column where that value is to copy and paste it (as values and not formulas). I need to do it because in that column I have a reference to external workbook and I need to share this worksheet without sharing the other workbook. This is what I wrote
Sub row()
Dim firstrow As Range
Dim ColumnToPaste As Range
Dim copy As Range
Set firstrow = Worksheets("Raw Data").ListObjects("Table2").HeaderRowRange
Set ColumnToPaste = firstrow.Find("Spot price", , xlValues, xlWhole).Columns(1)
With Worksheets("Raw Data").Range(ColumnToPaste)
.PasteSpecial Paste:=xlPasteValues
End With
End Sub
The error occurs on this line (Application-defined or object-defined error):
With Worksheets("Raw Data").Range(ColumnToPaste)
I think I'm using wrong range argument but not sure how to write it properly.
How about the following, instead of copying the full column, specify the range:
Sub row2()
Dim ws As Worksheet: Set ws = Sheets("Raw Data")
'declare and set your worksheet, amend as required
Dim firstrow As Range
Dim ColumnToPaste As Range
Dim LastRow As Long
Set firstrow = ws.ListObjects("Table2").HeaderRowRange
Set ColumnToPaste = firstrow.Find("Spot price", , xlValues, xlWhole).Columns
col = ColumnToPaste.Column
LastRow = ws.Cells(ws.Rows.Count, col).End(xlUp).Row
'get the last row with data on Column A
With ws
.Range(.Cells(1, col), .Cells(LastRow, col)).Copy
.Range(.Cells(1, col), .Cells(LastRow, col)).PasteSpecial xlPasteValues
End With
End Sub

Filldown makes my formula disappear Excel VBA

Looking to fill a variable range here. I can fill my formula to the right but when I want to fil it down, the formula disappears. I have also attached a picture and Column A starts with "Fund Name". Hope somebody can help me here thank you!! The link to the table is here. 1: https://i.stack.imgur.com/y1ZVH.png
Sub Six_Continue()
Dim Lastrow As Long
Dim lrow As Range
Dim Lastcol As Long
Dim lcol As Range
Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Set lrow = Range("C5:C" & Lastrow)
Lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
Set lcol = Range("C3", Cells(3, Lastcol))
Range("C5").FormulaArray = "=IFERROR(INDEX(DataTable[[Period]:[Period]],SMALL(IF(DataTable[[LP ID]:[LP ID]]=C$2,IF(DataTable[[Fund ID]:[Fund ID]]=$B5,ROW(DataTable[[Fund ID]:[Fund ID]])-ROW(DataTable[[Fund ID]:[Fund ID]]))),1)),"" "")"
Range("C5", lcol.Offset(2)).FillRight
Range("C5", lcol.Offset(2)).FillDown
End Sub
The code below worked for me (with a much simplified array formula).
Sub Six_Continue()
' 25 Jan 2018
Dim Rng As Range
Dim LastRow As Long
Dim LastClm As Long
With ActiveSheet ' better specify by name
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastClm = .Cells(1, .Columns.Count).End(xlToLeft).Column
Cells(5, 3).FormulaArray = "=IFERROR(INDEX(DataTable[[Period]:[Period]],SMALL(IF(DataTable[[LP ID]:[LP ID]]=C$2,IF(DataTable[[Fund ID]:[Fund ID]]=$B5,ROW(DataTable[[Fund ID]:[Fund ID]])-ROW(DataTable[[Fund ID]:[Fund ID]]))),1)),"" "")"
' .Cells(5, 3).FormulaArray = "= $a1 * $k1:$k5 * C$1"
Set Rng = Range(.Cells(5, 3), .Cells(5, LastClm))
Rng.FillRight
Set Rng = Range(.Cells(5, 3), .Cells(LastRow, LastClm))
Rng.FillDown
End With
End Sub

count items based on columns in another worksheet

Trying to use vba to write countifs function but got an error of object doesn't support this property or method. (Run-time error 438)
Sub counters()
Dim rng, rng2, rng3, rng4 As Range
Dim lrow, lr, lr2, lr3, lr4 As Long
Dim ws, ws1 As Worksheet
Set ws = Sheets("Data")
Set ws1 = Sheets("Table E-1 Zip Codes PIF")
lr = ws.Cells(Cells.Rows.Count, "D").End(xlUp).Row
lr2 = ws.Cells(Cells.Rows.Count, "X").End(xlUp).Row
lr3 = ws.Cells(Cells.Rows.Count, "Y").End(xlUp).Row
lr4 = ws.Cells(Cells.Rows.Count, "AE").End(xlUp).Row
lrow = ws1.Cells(Cells.Rows.Count, "B").End(xlUp).Row
'zip code
Set rng = ws.Range("D2:D" & lr)
'county
Set rng2 = ws.Range("X2:X" & lr2)
'Region
Set rng3 = ws.Range("Y2:Y" & lr3)
'policy form
Set rng4 = ws.Range("AE2:AE" & lr4)
For i = 5 To lrow - 1
Worksheets("Table E-1 Zip Codes PIF").Cells(i, 4).Value = Application.WorksheetFunction.CountIfs(ws.rng, ws1.Cells(i, 2).Value, ws.rng2, ws1.Cells(i, 3).Value, ws.rng3, "NW", ws.rng4, "Basic Choice")
Next i
End Sub
Excel function works fine but need to use vba to automate the process. Tried recording the macro but it gives reference and wasn't sure how to re-write codes for ranges (ws.Range("x2:x" & lr)). All of the rng have the same amount of data (rows) so wasn't sure if I need to define it each time.
I want my result to be from D5 to N-1 in Table E-1... sheet. I used For i = 5 To lrow - 1 since there is a total in the last row.
In Table E-1 sheet, Column D have a list of zip codes and column E have a list of county that I am trying to match.
Except for how you use your CountIfs Function, since there are no screen-shots of your data, the code below will take care of your run-time errors.
Explanation for your run-time errors:
1.Wrong declaration, Dim ws, ws1 As Worksheet should be Dim ws As Worksheet, ws1 As Worksheet , otherwise ws is not defined as Worksheet. The same goes to all of your variables declarations.
2.Find last row in a column: lr = ws.Cells(Cells.Rows.Count, "D").End(xlUp).Row is wrong, it should be lr = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row.
3.Like #Scott Holtzman wrote in his comment, since you already set your rng in previous lines, (Set rng = ws.Range("D2:D" & lr)) , then it should be used with just rng and not ws.rng.
4.To improve and clean your code, you could just use With ws in the beginning of your code, and nest most of it related objects underneath.
"Clean" Code
Option Explicit
Sub counters()
Dim rng As Range, rng2 As Range, rng3 As Range, rng4 As Range
Dim lrow As Long, lr As Long, lr2 As Long, lr3 As Long, lr4 As Long
Dim ws As Worksheet, ws1 As Worksheet
Set ws = Sheets("Data")
Set ws1 = Sheets("Table E-1 Zip Codes PIF")
' last row in Column B in "Table E-1 Zip Codes PIF" sheet
lrow = ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row
With ws
lr = .Cells(.Rows.Count, "D").End(xlUp).Row
lr2 = .Cells(.Rows.Count, "X").End(xlUp).Row
lr3 = .Cells(.Rows.Count, "Y").End(xlUp).Row
lr4 = .Cells(.Rows.Count, "AE").End(xlUp).Row
'zip code
Set rng = .Range("D2:D" & lr)
'county
Set rng2 = .Range("X2:X" & lr2)
'Region
Set rng3 = .Range("Y2:Y" & lr3)
'policy form
Set rng4 = .Range("AE2:AE" & lr4)
For i = 5 To lrow - 1
ws1.Cells(i, 4).Value = Application.WorksheetFunction.CountIfs(rng, ws1.Cells(i, 2).Value, rng2, ws1.Cells(i, 3).Value, rng3, "NW", rng4, "Basic Choice")
Next i
End With
End Sub

Dynamic range condensed into one line of code

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))

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))