Storing SeriesCollection Values in dynamical array - excel

I have the following issue: I create a chart from values that a user will insert in specific cells of a spreadsheet and the chart displays what I expect (The series are collected in the right way). The Series are created consecutively with 2 data points only. I want to store the Xvalues (And YValues) of the series in dynamic arrays for performing calculations later (outside the series collection loop). I tried the following but the arrays only store the last values of the Series (despite using redim preserve everywhere).
Is it because the array is not redimensioned correctly?
Thanks for your help!
i = 1
SeriesAddedA = 1
RangeOccupiedA = Range("A2").End(xlDown).Row
RangeOccupiedB = Range("B2").End(xlDown).Row
MaxRangeOccupied = WorksheetFunction.Max(RangeOccupiedA, RangeOccupiedB)
Dim XValuesA()
Dim YValuesA()
ReDim XValues(RangeOccupiedA + 2)
ReDim YValues(RangeOccupiedA + 2)
Do While i < MaxRangeOccupied
FilledCompA = NextFilled(Cells(i, 1))
PrevCell = PrevFilled(Cells(i + 1, 1))
rowdiff = FilledCompA - PrevCell
If rowdiff < 2 And PrevCell <> 0 And FilledCompA <> 0 Then
ReDim Preserve XValuesA(1 To RangeOccupiedA + 2)
chart.SeriesCollection.NewSeries
chart.FullSeriesCollection(SeriesAddedA).Name = "Reaction Step A " & SeriesAddedA - 1
chart.FullSeriesCollection(SeriesAddedA).XValues = "=" & "'" & ActiveSheet.Name & "'" & "!$E$" & FilledCompA - 1 & ":$E$" & FilledCompA
chart.FullSeriesCollection(SeriesAddedA).Values = "=" & "'" & ActiveSheet.Name & "'" & "!$M$" & FilledCompA - 1 & ":$M$" & FilledCompA
XValuesA() = chart.SeriesCollection(SeriesAddedA).XValues
SeriesAddedA = chart.SeriesCollection.Count + 1
ElseIf PrevCell <> 0 And rowdiff > 0 Then
ReDim Preserve XValuesA(1 To RangeOccupiedA + 2)
chart.SeriesCollection.NewSeries
chart.FullSeriesCollection(SeriesAddedA).Name = "Reaction Step prev A " & SeriesAddedA - 1
chart.FullSeriesCollection(SeriesAddedA).XValues = "=" & "(" & "'" & ActiveSheet.Name & "'" & "!$E$" & FilledCompA - rowdiff & "," & "'" & ActiveSheet.Name & "'" & "!$E$" & FilledCompA & ")"
chart.FullSeriesCollection(SeriesAddedA).Values = "=" & "(" & "'" & ActiveSheet.Name & "'" & "!$M$" & FilledCompA - rowdiff & "," & "'" & ActiveSheet.Name & "'" & "!$M$" & FilledCompA & ")"
XValuesA() = chart.SeriesCollection(SeriesAddedA).XValues
SeriesAddedA = chart.SeriesCollection.Count + 1
End If
loop
'the debug print says:
XvaluesA(1) 310.52
XvaluesA(2) 408.58

With XValuesA() = chart.SeriesCollection(SeriesAddedA).XValues you overwrite the data in XValuesA everytime you call it in your loop. That is why it only contains the last values.
Use XValuesA(i) = chart.SeriesCollection(SeriesAddedA).XValues instead and then
Debug.Print XvaluesA(1)(1)
Debug.Print XvaluesA(1)(2)
gives the first values and
Debug.Print XvaluesA(2)(1)
Debug.Print XvaluesA(2)(2)
the second …

Related

Using VBA in excel - Pad the resulting output of a formula with a 0

I have written a block of code using VBA in excel that counts the number of records produced in a file exported from another platform on the very last line of the file.
This file has to contain a specific number of characters to validate. If the record count is 9 or less then it will operate just fine. However, if the record count is 10 or above the character limit is exceeded and it will fail.
Is there a way to embed a line code into the module that will pad the output from this last line of syntax with a leading 0 if the record count is less than 10? If so, I can then alter the earlier part of the code to remove a static character and meet the files requirements regardless of if the record count is above or below 9.
Code is below...The bolded area on the last line of the code at the very end (" & lastrow - 2 & ") is where I need to pad the 0 of the resulting formula.
Sub Hidden_macro1()
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Testing_Report_Out").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Sheets.Add.Name = "Testing_Report_Out"
Dim lastrow As Long
Dim lastrowfooter As Long
Dim I As Integer
Dim J As Integer
Sheets("Testing Report").Select
lastrow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
Sheets("Testing_Report_Out").Select
Range("A1").Formula = "=CONCATENATE('Testing Report'!A4,"" "",'Testing Report'!B4,'Testing Report'!C4,"" "",'Testing Report'!D4,'Testing Report'!E4,"" "",'Testing Report'!F4)"
I = 4
J = 2
Do Until I = lastrow
'Fill 1st formula (:20:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!G" & I & ",'Testing Report'!H" & I & ")"
J = J + 1
'Fill 2nd formula (:23B:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!I" & I & ",'Testing Report'!J" & I & ")"
J = J + 1
'Fill 3rd formula (:32A:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!K" & I & ",'Testing Report'!L" & I & ",'Testing Report'!M" & I & ",'Testing Report'!N" & I & ",'Testing Report'!O" & I & ")"
J = J + 1
'Fill 4th formula (:50K:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!P" & I & ",'Testing Report'!Q" & I & ")"
J = J + 1
'Fill 5th formula (:50K: Addy)
Range("A" & J).Formula = "='Testing Report'!R" & I & ""
J = J + 1
'Fill 6th formula (:57A:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!S" & I & ",'Testing Report'!T" & I & ")"
J = J + 1
'Fill 7th formula (:59:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!U" & I & ",'Testing Report'!V" & I & ")"
J = J + 1
'Fill 8th formula (:59:/ 3)
Range("A" & J).Formula = "='Testing Report'!W" & I & ""
J = J + 1
'Fill 9th formula (:59:/ 5)
Range("A" & J).Formula = "='Testing Report'!X" & I & ""
J = J + 1
'Fill 10th formula (:70:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!Z" & I & ",'Testing Report'!AA" & I & ")"
J = J + 1
'Fill 11th formula (:71A:)
Range("A" & J).Formula = "='Testing Report'!AB" & I & ""
J = J + 1
'Fill 12th formula (:72:)
Range("A" & J).Formula = "='Testing Report'!AC" & I & ""
I = I + 1
J = J + 1
Loop
lastrowfooter = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Range("A" & lastrowfooter + 1).Formula = "=CONCATENATE('Testing Report'!AD4,'Testing Report'!AE4,'Testing Report'!AF4,'Testing Report'!AG4,"" "",'Testing Report'!AH4,'Testing Report'!AI4,'Testing Report'!AJ4," **& lastrow - 2 &** ")"
End Sub
You can use Format() as suggested by #BigBen
Range("A" & lastrowfooter + 1).Formula = _
"=CONCATENATE('Testing Report'!AD4,'Testing Report'!AE4,'Testing Report'!AF4," & _
"'Testing Report'!AG4,"" "",'Testing Report'!AH4,'Testing Report'!AI4," & _
"'Testing Report'!AJ4," & Format(lastrow - 2, "00") & ")"

excel vba problem when item already registered in sheet, only sum the quantity

so what i want is when user add a product which is already registered in the sheet, the quantity of that product is added and not add new row. this is the code that i already make but the problem with this code is that the system doesnt enter the if loop and so it adds new rows again.
this is the sample:
code:
Private Sub btnSubmit_Click()
Sheet2.Activate
Dim lastRow2 As Long
lastRow2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
Set rng2 = Worksheets("Sheet2").Range("B2:B" & lastRow2)
For Each cell2 In rng2
If cell2 = tbTipe.Text + " " + "(" + tbColor.Text + ")" + " " + "-" + " " + tbProduct.Text Then
cell2.Offset(0, 1) = cell2.Offset(0, 1) + tbQty.Value
Else
Sheets("Sheet2").Range("A" & lastRow2).Value = CDate(tbDate)
Sheets("Sheet2").Range("B" & lastRow2).Value = UCase(tbTipe.Text + " " + "(" + tbColor.Text + ")" + " " + "-" + " " + tbProduct.Text)
Sheets("Sheet2").Range("C" & lastRow2).Value = tbQty.Value
Sheets("Sheet2").Range("D" & lastRow2).Value = tbPrice.Value
End If
Next cell2
End Sub
Private Sub UserForm_Initialize()
tbDate.Value = Date
tbProduct.Value = ""
tbQty.Value = ""
tbPrice.Value = ""
tbTipe.Value = ""
tbColor.Value = ""
End Sub
Generate your product string and look for it in column B. If found, add the quantity to column C; if not add a new row with all information.
Private Sub btnSubmit_Click()
Dim lastRow2 As Long, m as variant, str as string
with workSheets("Sheet2")
.Activate
str = UCase(tbTipe.Text & " (" & tbColor.Text & ") - " & tbProduct.Text)
m = application.match(str, .range("B:B"), 0)
if not iserror(m) then
.Range("C" & m).Value = .Range("C" & m).Value + val(tbQty.Value)
else
lastRow2 = .Range("A" & Rows.Count).End(xlUp).Row + 1
.Range("A" & lastRow2).Value = CDate(tbDate)
.Range("B" & lastRow2).Value = str
.Range("C" & lastRow2).Value = val(tbQty.Value)
.Range("D" & lastRow2).Value = val(tbPrice.Value)
End If
Next cell2
End Sub
VBA's string concatenation symbol is the ampersand (&), not the plus sign (+).

VBA, NumberFormat when pasting an array

I have the following vba code, but I want to paste it giving the format of dd-mm-yyyy.
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr
I've tried:
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr.Numberformat = ('dd-mm-yyyy')
I am unsure on the format of this. Where do I put numberformat?
on a different line, two actions:
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).NumberFormat = "dd-mm-yyyy"
But we can shorten it a little with With and Resize
With Worksheets("stack").Range("M" & LastRowM + 1).resize(Ubound(pasteArr,1),18)
.Value = PasteArr
.NumberFormat = "dd-mm-yyyy"
End With

IF logic for all date variables empty

I'm trying to modify the below function to include logic where if the variables PPD_1_Date, PPD_2_Date and TSpot_Date are all empty (blank) then output to my "Error" worksheet.
I have rows that should fall under this logic, however they are falling under the Else condition instead.
Function PPDdate()
Dim PPD_1_Date As Date
Dim PPD_2_Date As Date
Dim TSpot_Date As Variant
Dim i As Long, j As Long, k As Long
j = Worksheets("PPDCI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
PPD_1_Date = Worksheets("Data").Range("AW" & i)
PPD_2_Date = Worksheets("Data").Range("BA" & i)
Entity = Worksheets("Data").Range("J" & i)
Dept = Worksheets("Data").Range("M" & i)
TSpot_Date = Worksheets("Data").Range("AS" & i)
If PPD_1_Date > PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_1_Date
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AZ" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("AY" & i).Value
j = j + 1
Else
If PPD_1_Date < PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_2_Date
'Worksheets("PPDCI").Range("G" & j).Value = "ELSE IF CONDITION"
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("BB" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("BD" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("BC" & i).Value
j = j + 1
Else
'If IsEmpty(Worksheets("Data").Range(PPD_1_Date & i).Value) = True And IsEmpty(Worksheets("Data").Range(PPD_2_Date & i).Value) = True Then
'GoTo EmptyRange
'Else
If (InStr(1, Entity, "CNG Hospital") Or InStr(1, Entity, "Home Health") Or InStr(1, Entity, "Hospice") Or InStr(1, Dept, "Volunteers") Or ((IsEmpty(PPD_1_Date) = True) And (IsEmpty(PPD_2_Date) = True))) And IsEmpty(TSpot_Date) = True Then
Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
k = k + 1
Else
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = TSpot_Date
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AY" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = "NO PPD DATES BUT HAS TSPOT DATE1"
j = j + 1
End If
End If
End If
'EmptyRange:
'k = k + 1
Next i
End Function
Here is the code I added to the other OR logic;
Or ((IsEmpty(PPD_1_Date) = True) And (IsEmpty(PPD_2_Date) = True))
Example row has empty cells in columns AW, BA, and AS, so it should write to my Error worksheet. Is there a syntax or logic issue? I did initially have TSPOT_Date defined as a Date variable, however I was getting a '1004' runtime error (I think because some column rows are empty) so I changed to Variant, however logic still doesn't work as I expect.
The problem you're running into is that you can't check if Date variables are "empty" using isEmpty() or even with Len() because the default value for a date is 30-Dec-1899 00:00:00, so there is always a value in a Date variable.
Instead, you should check to see that a Date variable is empty/has not been filled like this
If PPD_2_Date = 0 Then
...

Need to run a Do While loop across multiple worksheets

I have been trying to run the same Do While loop function across multiple worksheets in a workbook and compile the data in another worksheet. The code works for the one worksheet that is specified but how do I get it to work across the others that are in the workbook at the same time?
Also worth mentioning that I only want it to run on some of the worksheets not all that are in the workbook (sheets are named as years - 2014, 2015 etc).
This is the code
Sub Total_Button1_Click()
Dim i As Integer
Dim strSheetFrom As String
Dim j As Integer
Dim strSheetTo As String
i = 3
j = 2
strSheetFrom = "2014"
strSheetTo = "Total"
Do While Trim(Sheets(strSheetTo).Range("B" & CStr(j)).Text) <> ""
j = j + 2
Loop
Do While Trim(Sheets(strSheetFrom).Range("B" & CStr(i)).Text) <> ""
If UCase(Trim(Sheets(strSheetFrom).Range("A" & CStr(i)).Text)) = "Y" Then
Sheets(strSheetTo).Range("B" & j & ":G" & j).Value = Sheets(strSheetFrom).Range("B" & i & ":G" & i).Value
Sheets(strSheetTo).Range("H" & j & ":I" & j).Value = Sheets(strSheetFrom).Range("I" & i & ":J" & i).Value
Sheets(strSheetTo).Range("J" & j & ":J" & j).Value = Sheets(strSheetFrom).Range("L" & i & ":L" & i).Value
Sheets(strSheetTo).Range("K" & j & ":K" & j).Value = Sheets(strSheetFrom).Range("Q" & i & ":Q" & i).Value
Sheets(strSheetTo).Range("L" & j & ":AH" & j).Value = Sheets(strSheetFrom).Range("s" & i & ":AO" & i).Value
j = j + 1
End If
i = i + 1
Loop
MsgBox "Total book created"
End Sub
Try making your strSheetFrom variable an array something like this:
strSheetFrom = new strSheetFrom[3]
strSheetFrom[2] = "2012"
strSheetFrom[1] = "2013"
strSheetFrom[0] = "2014"
Then put your code into another loop like so:
dim w as integer
for w = 0 To 3
Do While Trim(Sheets(strSheetTo).Range("B" & CStr(j)).Text) <> ""
j = j + 2
Loop
Do While Trim(Sheets(strSheetFrom[w]).Range("B" & CStr(i)).Text) <> ""
If UCase(Trim(Sheets(strSheetFrom[w]).Range("A" & CStr(i)).Text)) = "Y" Then
Sheets(strSheetTo).Range("B" & j & ":G" & j).Value = Sheets(strSheetFrom[w]).Range("B" & i & ":G" & i).Value
Sheets(strSheetTo).Range("H" & j & ":I" & j).Value = Sheets(strSheetFrom[w]).Range("I" & i & ":J" & i).Value
Sheets(strSheetTo).Range("J" & j & ":J" & j).Value = Sheets(strSheetFrom[w]).Range("L" & i & ":L" & i).Value
Sheets(strSheetTo).Range("K" & j & ":K" & j).Value = Sheets(strSheetFrom[w]).Range("Q" & i & ":Q" & i).Value
Sheets(strSheetTo).Range("L" & j & ":AH" & j).Value = Sheets(strSheetFrom[w]).Range("s" & i & ":AO" & i).Value
j = j + 1
End If
i = i + 1
Loop
w -= 1
next
I haven't tested it, but something like that. You get the idea.
Use a For Each and iterate over the Worksheet collections like this
'Variables
Dim useWorkSheet As Worksheet
Dim totalWorkSheet As Worksheet
Dim yearAsNumeric As Integer
Dim startingYear As Integer
'Settings
startingYear = 2014
'To reference the total worksheet so we can work with it
Set totalWorkSheet = ActiveWorkbook.Worksheets("Total")
'Iterate over each item in the collection
For Each useWorkSheet In ActiveWorkbook.Worksheets
'Force the name into a numeric value. If it starts with anything non numeric (A-Z|a-z|$,#,etc) then it will return 0
yearAsNumeric = Val(useWorkSheet.Name)
'Greater than or equal to the year we want to start with?
If yearAsNumeric >= startingYear Then
'Yes. Do your stuff here
useWorkSheet.Name
End If
Next

Resources