VBA Issue when Calculating - excel

On the Model.Calculate Line, I get an error.
The error is "Cannot execute code in breakmode"
Anything wrong with the code? NOTE: TM1 Addin Is enabled and must be.
Sub Status_Quo_Sensitivity()
Dim wb As Workbook, Summary As Worksheet, Inputs As Worksheet, Model As Worksheet
Dim SenRange As Range, Reductions(1 To 5) As Double
Dim ReducCell As Range
Dim val As Integer, num As Integer
Set Summary = ThisWorkbook.Worksheets("SUMMARY")
Set Inputs = ThisWorkbook.Worksheets("INPUTS")
Set Model = ThisWorkbook.Worksheets("MODEL")
Set SenRange = Summary.Range("K15:K19")
Set ReducCell = Inputs.Range("D17")
For val = 1 To 5
Reductions(val) = Summary.Cells(13, val + 8).Value
Next val
For num = LBound(Reductions) To UBound(Reductions)
Inputs.Cells("D17") = Reductions(num)
Model.Calculate
Summary.Calculate
SenRange.Copy
Inputs.Range(Inputs.Cells(15, 8 + num), Inputs.Cells(19, 8 + num)).PasteSpecial xlPasteValues
Next num

Inputs.Cells("D17")
to
Inputs.Range("D17")

Related

VBA resizes table causes a copy of data validation un the entire workbook

I'm trying to make a resize table VBA and that one worked but when I use it it copies also the data validation in another part of the file. These are not in a table. And are copied along with along.
I think that is has someting to do with the fact that 1 table has a data validation in it.
How can I prevent this from happening?
Sub Resize_Table()
Dim aSheetname As Worksheet
Dim bSheetname As Worksheet
Dim cSheetname As Worksheet
Dim aaSheetname As Worksheet
Dim aTableName As String
Dim bTableName As String
Dim cTableName As String
Dim aaTableName As String
Dim aTable As ListObject
Dim bTable As ListObject
Dim cTable As ListObject
Dim aaTable As ListObject
Dim aRows As Integer
Dim bRows As Integer
Dim cRows As Integer
Dim aaRows As Integer
'Define
aTableName = "PS_Calc"
bTableName = "Prices"
cTableName = "Handling"
aaTableName = "Testing"
'Werkblad
Set aSheetname = Sheets("PS Calc")
Set bSheetname = Sheets("Prices")
Set cSheetname = Sheets("Handling")
Set aaSheetname = Sheets("Tests")
'Define tabel
Set aTable = aSheetname.ListObjects(aTableName)
Set bTable = bSheetname.ListObjects(bTableName)
Set cTable = cSheetname.ListObjects(cTableName)
Set aaTable = aaSheetname.ListObjects(aaTableName)
'Looking up rows
aRows = aTable.Range.Rows.Count
bRows = bTable.Range.Rows.Count
cRows = cTable.Range.Rows.Count
aaRows = aaTable.Range.Rows.Count
'Add how many rows
aNewRows = Worksheets("PS Calc").Range("N20")
'Resize
aTable.Resize aTable.Range.Resize(aRows + aNewRows)
bTable.Resize bTable.Range.Resize(bRows + aNewRows)
cTable.Resize cTable.Range.Resize(cRows + aNewRows)
aaTable.Resize aaTable.Range.Resize(aaRows + aNewRows)
End Sub

Is there a way to reassign a Range variable to a different range?

I am very new to VBA, having started programming it yesterday. I am writing a data processing program which requires keeping track of two cells, one on each spreadsheet. The code which reproduces the errors I am experiencing is below. When I call the sub moveCell() in sub Processor(), nothing happens to DIRow and DIColumn, and the code spits out error 1004 at the line indicated. I have tried using DICell = DICell.Offset(), but it returns the same error.
How can I redefine a Range variable to be a different cell?
'<<Main Processor Code>>'
Sub Processor()
Dim PDRow As Integer
Dim PDColumn As Integer
Dim DIRow As Integer
Dim DIColumn As Integer
PDRow = 1
PDColumn = 1
DIRow = 1
DIColumn = 1
Dim PDCell As Range
Dim DICell As Range
Set PDCell = Worksheets("Processed Data").Cells(PDRow, PDColumn)
Set DICell = Worksheets("Data Input").Cells(DIRow, DIColumn)
Call moveCell(2, 0, "Data Input")
End Sub
'<<Function which moves the cell which defines the range>>'
Sub moveCell(r As Integer, c As Integer, sheet As String)
If sheet = "Processed Data" Then
PDRow = PDRow + r
PDColumn = PDColumn + c
Set PDCell = Worksheets("Data Input").Cells(PDRow, PDColumn)
ElseIf sheet = "Data Input" Then
DIRow = DIRow + r '<<<<<<This line does nothing to DIRow's value
DIColumn = DIColumn + c
Set DICell = Worksheets("Data Input").Cells(DIRow, DIColumn) '<<<<<<This line causes error 1004
End If
End Sub
As far as I can tell, you could instead use a quick Function instead. There doesn't seem to be any difference in your If statement results in the moveCell() function, except which worksheet you're using.
We can make this simpler by referring to the Range you're passing to moveCell.
Option Explicit ' forces you to declare all variables
Sub something()
Dim PDCell As Range
Set PDCell = Worksheets("Processed Data").Cells(1, 1)
Dim DICell As Range
Set DICell = Worksheets("Data Input").Cells(1, 1)
PDCell.Select ' can remove
Set PDCell = moveCell(2, 0, PDCell, PDCell.Worksheet.Name)
PDCell.Select ' can remove
Worksheets(DICell.Worksheet.Name).Activate ' can remove
DICell.Select ' can remove
Set DICell = moveCell(5, 0, DICell, DICell.Worksheet.Name)
DICell.Select ' can remove
End Sub
Function moveCell(rowsToMove As Long, colsToMove As Long, cel As Range, ws As String) As Range
Set moveCell = Worksheets(ws).Cells(cel.Row + rowsToMove, cel.Column + colsToMove)
End Function
I've included some rows you don't need (which I've marked with a comment afterwards), but that will show you how the routine works. You can step through with F8 to help see it step-by-step.
Edit: Although, you don't need a separate function at all. Just use OFFSET().
Set PDCell = ...whatever originally
Set PDCell = PDCell.Offset([rows],[cols])

VBA Deleting rows by found cell values

I have this code and i want to delete all rows by cell value which contain "BSB*" (so there are values like BSB 1, 2 etc) this code works partially. It deletes only every second cell which contain BSB. So i have BSB 2, 4 etc. I want to delete all rows where value BSB is present in speecific column ofc.
p.s english is my second language so please let mi know if i need to clarify something
Option Explicit
Sub PrList()
Dim wb As Workbook
Dim ws As Worksheet
Dim r As Range
Dim i As Range
Set wb = Workbooks.Open("?????")
Set ws = wb.Worksheets("owssvr")
With ws.Range("A1").CurrentRegion
.Find(What:="Product_Number", LookAt:=xlWhole).Name = "Product_Number"
.Find(What:="Product_Status", LookAt:=xlWhole).Name = "Product_Status"
.Find(What:="Group_of_product", LookAt:=xlWhole).Name = "Group_of_product"
End With
Set i = ws.Range("Product_Number", Range("Product_Number").End(xlDown))
For Each r In i
If r Like "BSB*" Then
r.Rows.Delete
End If
Next r
End Sub
Try this
Option Explicit
Sub NAPP_PrList()
Dim wb As Workbook
Dim ws As Worksheet
Dim r As Range
Dim i As Range
Dim j as Long
Set wb = Workbooks.Open("?????")
Set ws = wb.Worksheets("owssvr")
With ws.Range("A1").CurrentRegion
.Find(What:="Product_Number", LookAt:=xlWhole).Name = "Product_Number"
.Find(What:="Product_Status", LookAt:=xlWhole).Name = "Product_Status"
.Find(What:="Group_of_product", LookAt:=xlWhole).Name = "Group_of_product"
End With
Set i = ws.Range("Product_Number", Range("Product_Number").End(xlDown))
For j = i.Rows.Count To 1 Step -1
If i(j, 1) Like "BSB*" Then i(j, 1).EntireRow.Delete
Next
End Sub

Runtime Error 9 on Loop

I have three workbooks; all with information on the same policies, but come from different documents. I'm trying to copy the value of the same cell from each worksheet that has the same worksheet name in workbooks 1 & workbook 3. This is the code that I have:
Sub foo()
Dim wbk1 As Workbook
Dim wbk2 As Workbook
Dim wkb3 As Workbook
Dim shtName As String
Dim i As Integer
Set wkb1 = Workbooks.Open("C:\Users\lliao\Documents\Trad Reconciliation.xlsx")
Set wkb2 = Workbooks.Open("C:\Users\lliao\Documents\TradReconciliation.xlsx")
Set wkb3 = Workbooks.Open("C:\Users\lliao\Documents\Measure Trad Recon LS.xlsx")
shtName = wkb2.Worksheets(i).Name
For i = 2 To wkb2.Worksheets.Count
wkb2.Sheets(shtName).Range("D3").Value = wkb1.Sheets(shtName).Range("D2")
wkb2.Sheets(shtName).Range("E3").Value = wkb1.Sheets(shtName).Range("E2")
wkb2.Sheets(shtName).Range("F3").Value = wkb1.Sheets(shtName).Range("F2")
wkb2.Sheets(shtName).Range("D4").Value = wkb3.Sheets(shtName).Range("D2")
wkb2.Sheets(shtName).Range("E4").Value = wkb3.Sheets(shtName).Range("E2")
wkb2.Sheets(shtName).Range("F4").Value = wkb3.Sheets(shtName).Range("F2")
Next i
End Sub
I don't understand how I'm using the subscript wrong. This is my first time coding VBA (first time in 5+ years), so I'm unfamiliar with coding errors.
Thank you!
Dim i As Integer
Set wkb1 = Workbooks.Open("C:\Users\lliao\Documents\Trad Reconciliation.xlsx")
Set wkb2 = Workbooks.Open("C:\Users\lliao\Documents\TradReconciliation.xlsx")
Set wkb3 = Workbooks.Open("C:\Users\lliao\Documents\Measure Trad Recon LS.xlsx")
shtName = wkb2.Worksheets(i).Name
Variable i is declared, but used before it's assigned - its value is therefore an implicit 0.
With VBA collections being 1-based, that makes wkb2.Worksheets(i) be out of bounds.
Dim i As Integer
i = 1
'...
shtName = wkb2.Worksheets(i).Name
Will fix it.
You probably want to move it inside the loop though.
may be you're after this:
For i = 2 To wkb2.Worksheets.Count
wkb2.Sheets(i).Range("D3:F3").Value = wkb1.Sheets(i).Range("D2:F2")
wkb2.Sheets(i).Range("D4:F4").Value = wkb3.Sheets(i).Range("D2:F2")
Next i

Compile Error: Object Required VBA

I'm new to VBA. I'm trying to write a script that cleans up some data from an experiment. I keep getting an error saying "Object Required" and it highlights pold. Does anyone have any idea why?
As for the script, I'm trying to go down a column of participant numbers and map out what range each participant is in. There are around 30 lines per participant, and I want to define that as values in an array.
Sub Cleanthismofoup()
Dim pranges(1 To 50) As Long
Dim pbegin As Range
Dim pend As Range
Dim pold As Integer
Dim pnew As Integer
Dim pcell As Range
Dim pcounter As Long
Dim i As Long
Set pcell = Range("A1:A1")
Set pbegin = Range("A2:A2")
Set pold = Range("B2:B2").Value
pcounter = 0
'for every item, store value in pnew
' move down one line. Check pnew = pold
' if it is, do again. else create new range
For i = 1 To rngl
pcell = pcell.Offset(-1, 0)
pnew = pcell.Cells.Value
If pnnew <> pold Then pcell = pend
If pcell = pend Then
counter = counter + 1
pranges(counter) = pbegin
counter = counter + 1
pranges(counter) = pend
pbegin = pcell.Offset(-1, 0)
Else: pold = pnew
End If
i = i + 1
Next
End Sub
The error is because you are using a Set keyword which is used to assign reference to the object. Since the output on the RHS of Set pold = Range("B2:B2").Value is an Integer, vba gives you an error. To resolve it simply remove the Set keyword. However I also noticed that you are using rng1 in the for loop without initializing the rng1 variable, in which case your for loop will never execute. You might also want to rectify that.

Resources