Having trouble manipulating seriescollections on vba charts - vba

Is something outdated on msdn (here, in particular https://msdn.microsoft.com/en-us/library/office/ff821866.aspx ), or am I just really dumb? I have some code:
'Cel is a range, CelCol is a long, i, j, k, l are all long, LastColumn is a long, GraphDataStationBlock is a constant (long), wsh1 and wsh2 are worksheets, and chrt is the chart
'option explicit is on so if I missed mentioning a variable, it *was* declared, I just missed it
'I tried setting the source data both before and after all this just in case it mattered but nothing changed
j = 1
For Each Cel In wsh1.Range(wsh1.Cells(GraphDataStationBlock * i + 1, 1), wsh1.Cells(GraphDataStationBlock * (i + 1), 1)).Cells
If Cel.Offset(0, 1) <> vbNullString Then
wsh1.Cells(Cel.Row, CelCol) = WorksheetFunction.Max(wsh2.Range(wsh2.Cells(Cel.Row, 3), wsh2.Cells(Cel.Row, 26)))
chrt.SeriesCollection(j).XValues = wsh1.Range("B3:B5") 'all but straight from the msdn website, still doesn't work!
'I also tried a standard range(cell1, cell2) format (not letter/number) in case that would work but it did not, even though msdn says ranges should be fine
chrt.SeriesCollection(j).name = wsh1.Cells(Cel.Row, 1) & vbSpace & wsh1.Cells(Cel.Row, 2)
'always gives "unable to get name property of the series class"
j = j + 1
End If
Next Cel
'there's a lot more besides this of course but this is just the problematic part
long story short, it copies a row maxima from sheet A (representing hourly data for a single day), puts it into the appropriate column on sheet B (representing each day of that month), and then maps it onto chart C. Or it's supposed to. In practice, it copies the maxima over just fine and then I get an endless series of run-time error 1004, for both the name and the XValues portion of the seriescollection.
I haven't really built all that many charts - namely, none - so I'm kind of blundering around a bit, and if there's a better method of making a chart then I'm all ears, but otherwise...
Edit: the data it's getting this from is pretty straightforward - day numbers (1 to the last day of the month) across the top, labels in the left two columns, and then data that gets filled in each day. Come to think of it, it wouldn't matter that at any given time a fair amount of the source data is empty, right?
Also, more code. This still isn't nearly the entire program, but it covers a lot more than the above loop.
Option Explicit
'vbSpace will be mentioned, I just saved it as a public variable equalling " " because I find it easier to type
Sub UpdateMonthlyGraphData(ByVal wsh2 As Worksheet, ByVal Yesterdate As Date, ByVal FirstDate As Date, ByVal LastDate As Date)
'wsh2 has the daily information
With Excel.Application
.DisplayAlerts = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
Dim LastColumn As Long, i As Long, j As Long, SheetCount As Long, CelCol As Long
Dim FirstDay As Long, LastDay As Long, FirstWeekday As Long
Dim Yesteryear As Long, Yestermonth As Long, Yesterday As Long
Dim FormattedMonth As String, ChartType As String
Dim Cel As Range
Dim SerCol As Series
Dim wsh1 As Worksheet
Dim wb1 As Workbook
Dim chrt As Chart
Set wb1 = ThisWorkbook
FirstWeekday = Weekday(FirstDate, vbSunday)
LastDay = Day(LastDate)
FormattedMonth = Format(Yesterdate, "MMM YYYY")
SheetCount = wb1.Sheets.Count
Yesteryear = Year(Yesterdate)
Yestermonth = Month(Yesterdate)
Yesterday = Day(Yesterdate)
LastColumn = 2 + LastDay 'Set the data range to the appropriate size according the number of days in the month
If Not CBool(WorksheetExists(MonthName(Yestermonth, True) & vbSpace & Yesteryear & vbSpace & "Monthly Graph Data")) Then
'WorksheetExists just looks thruogh each worksheet and either returns the index of the sheet named (if it exists) or zero (if not).
SheetCount = wb1.Sheets.Count 'Monthly Data sheet creation
wb1.Worksheets("Template Monthly Graph Data").Copy after:=wb1.Sheets(SheetCount)
SheetCount = SheetCount + 1
Set wsh1 = wb1.Sheets(SheetCount)
wsh1.Move after:=wb1.Worksheets("Template WOT Main")
wsh1.name = MonthName(Yestermonth, True) & vbSpace & Yesteryear & vbSpace & "Monthly Graph Data"
LastDay = Day(DateSerial(Yesteryear, Yestermonth + 1, 0))
For i = 1 To 31 'only sort of tested code, be sure to check in on it to make sure it works properly
If i <= LastDay Then
wsh1.Cells(2, i + 2) = i & " : " & WeekdayName(Weekday(DateSerial(Yesteryear, Yestermonth, i), vbSunday), True)
Else
wsh1.Cells(2, i + 2) = "N/A"
End If
Next i
Else
Set wsh1 = wb1.Worksheets(MonthName(Yestermonth, True) & vbSpace & Yesteryear & vbSpace & "Monthly Graph Data")
LastDay = DateSerial(Yesteryear, Yestermonth + 1, 0)
End If
Set Cel = wsh1.Range(wsh1.Cells(2, 3), wsh1.Cells(2, LastDay + 2)).Cells.Find(Yesterday & " : " & WeekdayName(Weekday(Yesterdate, vbSunday), True))
'importing yesterday's data
If Not Cel Is Nothing Then
Set Cel = wsh1.Cells(GraphDataStationBlock - 3, Cel.Column)
CelCol = Cel.Column
Cel = WorksheetFunction.Max(wsh2.Range(wsh2.Cells(GraphDataStationBlock - 3, 3), wsh2.Cells(GraphDataStationBlock - 3, 26)))
Cel.Offset(1, 0) = WorksheetFunction.Max(wsh2.Range(wsh2.Cells(GraphDataStationBlock - 2, 3), wsh2.Cells(GraphDataStationBlock - 2, 26)))
wsh1.Range(Cel, Cel.Offset(1, 0)).NumberFormat = "0.0"
Else
MsgBox "Monthly Graph Data Sheet did not initialize correctly. Please review code and results."
Exit Sub 'just in case?
End If
For i = 0 To 2
Select Case i
Case 0
ChartType = "Winding"
Case 1
ChartType = "Oil"
Case 2
ChartType = "MW"
End Select
If Not CBool(ChartExists(FormattedMonth & " Monthly " & ChartType & " Graph")) Then 'the chart counterpart to the above "WorksheetExists"
wb1.Charts("Template Monthly " & ChartType & " Graph").Copy after:=wb1.Sheets(SheetCount)
SheetCount = SheetCount + 1
Set chrt = wb1.Sheets(SheetCount)
chrt.name = FormattedMonth & " Monthly " & ChartType & " Graph"
chrt.Move before:=wb1.Worksheets(FormattedMonth & " Monthly Graph Data")
chrt.Legend.Font.Size = 10 'was there before, keep it I guess?
If i < 2 Then
chrt.ChartTitle.Characters.Text = FormattedMonth & vbSpace & ChartType & " Temp Peaks"
Else
chrt.ChartTitle = FormattedMonth & vbSpace & ChartType & " Peaks"
End If
Else
Set chrt = wb1.Charts(FormattedMonth & " Monthly " & ChartType & " Graph")
End If
chrt.SetSourceData Source:=Union(wsh1.Range(wsh1.Cells(GraphDataStationBlock * i + 3, 3), wsh1.Cells(GraphDataStationBlock * i + 2 + Feeders138, LastColumn)), wsh1.Range(wsh1.Cells(GraphDataStationBlock * (i + 1) - Feeders416 - 4, 3), wsh1.Cells(GraphDataStationBlock * (i + 1) - 5, LastColumn))), PlotBy:=xlRows
For Each SerCol In chrt.SeriesCollection
Debug.Print SerCol.ChartType 'this didn't work
Stop 'it'd be convenient if it did, though, and if I can get the code to work at all, I will probably try and make it look all pretty and compact like this
Next SerCol
j = 1
For Each Cel In wsh1.Range(wsh1.Cells(GraphDataStationBlock * i + 1, 1), wsh1.Cells(GraphDataStationBlock * (i + 1), 1)).Cells
If Cel.Offset(0, 1) <> vbNullString Then
wsh1.Cells(Cel.Row, CelCol) = WorksheetFunction.Max(wsh2.Range(wsh2.Cells(Cel.Row, 3), wsh2.Cells(Cel.Row, 26)))
'noted limitation: as is this requires that the order of the feeders in the monthly and daily graph data sheets be structured the same way
chrt.SeriesCollection(j).XValues = wsh1.Range("B3:B5") '"='" & wsh1.name & "'!" & wsh1.Range(wsh1.Cells(2, 3), wsh1.Cells(2, LastColumn)).Address '"='" & Yesterdate & " Graph Data'!R2C3:R2C26"
chrt.SeriesCollection(j).name = wsh1.Cells(Cel.Row, 1) & vbSpace & wsh1.Cells(Cel.Row, 2)
j = j + 1
End If
Next Cel
Next i

Related

Error Type 13, Mismatch, for If Statement half of the time

I am working on automating a report with VBA, the code is working relatively well except for one of my If statements. In fact it works once every other time, and when it doesn't I get a mismatch error. After trying to resolve this all morning, I think that there may be an issue with the variable types I'm using or the fact that I use a text format at some point.
Here's the part where I am having problems, the error, when it pops up, does so at the ElseIf statement marked below
Dim source As Excel.Workbook 'ARS15.xls
Dim target As Excel.Workbook 'reporting cc
Dim clist As Excel.Workbook 'client list
Dim i As Integer
Dim lastRow As Long
Dim rowz As Long
Dim temp As Range 'payment condition and client codes used for index match
Dim pay As Variant 'payment condition
Dim fact As Variant 'insurance status
'get payment conditions and insurance status
clist.Sheets("ZTRE128C").AutoFilterMode = False
lastRow = clist.Sheets("ZTRE128C").Cells(Rows.Count, "C").End(xlUp).Row
Set temp = clist.Sheets("ZTRE128C").Range("C3:S" & lastRow)
temp.Copy
target.Sheets("Countries").Range("D3").PasteSpecial Paste:=xlPasteValues
target.Sheets("Countries").Range("D3:D" & lastRow) = Application.Trim(target.Sheets("Countries").Range("D3:D" & lastRow).Value)
target.Sheets("Countries").Range("D3:D" & lastRow).NumberFormat = "#"
target.Sheets("Countries").Range("T3:T" & lastRow) = Application.Trim(target.Sheets("Countries").Range("T3:T" & lastRow).Value)
For i = 0 To rowz - 1
pay = Application.Index(target.Sheets("Countries").Range("G3:G" & lastRow), Application.Match(target.Sheets("TEST").Range("D" & 7 + i), target.Sheets("Countries").Range("D3:D" & lastRow), 0))
fact = Application.Index(target.Sheets("Countries").Range("T3:T" & lastRow), Application.Match(target.Sheets("TEST").Range("D" & 7 + i), target.Sheets("Countries").Range("D3:D" & lastRow), 0))
If IsNumeric(pay) Then
target.Sheets("TEST").Range("F" & 7 + i) = pay
ElseIf Left(pay, Len(pay) - 1) = "0" Then 'HERE IS WERE THE ERROR HAPPENS
target.Sheets("TEST").Range("F" & 7 + i) = 1
Else: target.Sheets("TEST").Range("F" & 7 + i) = Left(pay, Len(pay) - 1)
End If
If IsEmpty(fact) Then
target.Sheets("TEST").Range("G" & 7 + i).Value = ""
Else: target.Sheets("TEST").Range("G" & 7 + i).Value = ChrW(&H2713)
End If
Next i
I also noticed that when the error appears the code goes through the whole For statement just once...
I would also like to say that I am relatively new to coding and VBA, sorry in advance for any abominations that may be written in this code.

Excel VBA - Selecting random rows based on multiple criteria

I have the below code set which takes a list of ticket data, and randomly selected three rows based on the username in Col D.
However, with a recent change in our ticketing system, I now need to update it to not select certain tickets. Specifically, I need only INC and SCTASK tickets to be selected, and not RITM tickets.
I am not quite sure how to add the filter so that tickets with RITM in the ticket number (ticket numbers are in Col A) are not included in this search.
Sub DailyTicketAudit()
'Set parameters and variables
Const sDataSheet As String = "Page 1"
Const sUserCol As String = "D"
Const lHeaderRow As Long = 1
Const lShowRowsPerUser As Long = 3
Const bSortDataByUser As Boolean = False
Dim wb As Workbook, ws As Worksheet
Dim rData As Range, rShow As Range
Dim aData() As Variant, aUserRows() As Variant
Dim i As Long, j As Long, k As Long, lRandIndex As Long, lTotalUnqUsers As Long, lMaxUserRows As Long
Set wb = Workbooks.Open("D:\Users\stefan.bagnato\Desktop\Raw Data Files\Audit Tickets Created")
Set ws = ActiveWorkbook.Sheets(sDataSheet)
Sheets("Page 1").name = "Audit Tickets"
'Work with the data range set by parameters
With ws.Range(sUserCol & lHeaderRow + 1, ws.Cells(ws.Rows.Count, sUserCol).End(xlUp))
If .Row < lHeaderRow + 1 Then
MsgBox "No data found in [" & sDataSheet & "]" & Chr(10) & _
"Verify column containing users is Column [" & sUserCol & "] or correct sUserCol in code." & Chr(10) & _
"Verify header row is Row [" & lHeaderRow & "] or correct lHeaderRow in code." & Chr(10) & _
"Once corrections have been made and data is available, try again."
Exit Sub
End If
lTotalUnqUsers = Evaluate("SUMPRODUCT((" & .Address(external:=True) & "<>"""")/COUNTIF(" & .Address(external:=True) & "," & .Address(external:=True) & "&""""))")
lMaxUserRows = Evaluate("max(countif(" & .Address(external:=True) & "," & .Address(external:=True) & "))")
If bSortDataByUser Then .Sort .Cells, xlAscending, Header:=xlNo
Set rData = .Cells
aData = .Value
ReDim aUserRows(1 To lTotalUnqUsers, 1 To 3, 1 To lMaxUserRows)
End With
'Load all available rows into the results array, grouped by the user
For i = LBound(aData, 1) To UBound(aData, 1)
For j = LBound(aUserRows, 1) To UBound(aUserRows, 1)
If IsEmpty(aUserRows(j, 1, 1)) Or aUserRows(j, 1, 1) = aData(i, 1) Then
If IsEmpty(aUserRows(j, 1, 1)) Then aUserRows(j, 1, 1) = aData(i, 1)
k = aUserRows(j, 2, 1) + 1
aUserRows(j, 2, 1) = k
aUserRows(j, 3, k) = i + lHeaderRow
Exit For
End If
Next j
Next i
'Select random rows up to lShowRowsPerUser for each user from the grouped results array
For j = LBound(aUserRows, 1) To UBound(aUserRows, 1)
Do
Randomize
lRandIndex = Int(Rnd() * aUserRows(j, 2, 1)) + 1
If Not rShow Is Nothing Then
Set rShow = Union(rShow, ws.Cells(aUserRows(j, 3, lRandIndex), sUserCol))
Else
Set rShow = ws.Cells(aUserRows(j, 3, lRandIndex), sUserCol)
End If
Loop While rShow.Cells.Count < j * Application.Min(lShowRowsPerUser, aUserRows(j, 2, 1))
Next j
rData.EntireRow.Hidden = True
rShow.EntireRow.Hidden = False
'Format table
'Sort by Opened By
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Worksheets("Audit Tickets").Sort.SortFields.Add Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlAscending
With Worksheets("Audit Tickets").Sort
.SetRange Range("A2:G" & LastRow)
.Orientation = xlTopToBottom
.Apply
End With
'Widen columns
Range("A:B,G:G").ColumnWidth = 15
Columns("C:D").ColumnWidth = 18
Columns("E:E").ColumnWidth = 50
Columns("F:F").ColumnWidth = 22
'Wrap text
Range("E1:E" & LastRow).WrapText = True
End Sub
Far more efficient, assuming aData holds all the data and the first column is tickets, is to simply process only the two of interest with the following.
Change 1 in aData(i, 1) to whichever column holds the items of interest in the array.
For i = LBound(aData, 1) To UBound(aData, 1)
If aData(i, 1) = "INC" Or aData(i, 1) = "SCTASK" Then
For j = LBound(aUserRows, 1) To UBound(aUserRows, 1)
''other code
End If
Next i
You could use advanced filter:
Sheets("Emps").Range("A1:D8").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Emps").Range("F5:F6"), CopyToRange:=Range("A1:B1"), _
Unique:=False
Data to selectively copy:
Data copied:
Reference this short YouTube video; You can record a marco to help yourself with the code also:
https://www.youtube.com/watch?v=bGUKjXmEi2E
A more thorough tutorial is found here:
http://www.contextures.com/xladvfilter01.html
This tutorial shows how to get the source data from outside Excel:
https://www.extendoffice.com/documents/excel/4189-excel-dynamic-filter-to-new-sheet.html
This tutorial shows how to split data values based on a column to different sheets (Fruit column; Apple sheet, Pear sheet, etc.):
https://www.extendoffice.com/documents/excel/2884-excel-save-filtered-data-new-sheet-workbook.html

Excel VBA - Randomly select 3 rows per username

I have a large list of tickets with a total of 6 different user names. What I need the code to do is randomly select 3 rows of data per user (18 total) and hide the rest of the rows, as I only need to see the selected rows.
The code will be something like the below, but I am not sure how to write the "random" part.
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
With Range("A2:F" & LastRow)
*Select 3 random rows for user A*
*Select 3 random rows for user B*
*The same for C-F*
*Hide all other rows*
End With
Found this to be an interesting challenge. Something like this should work for you. Commented code for clarity.
Sub tgr()
'Adjust these parameters as necessary
Const sDataSheet As String = "Sheet1"
Const sUserCol As String = "A"
Const lHeaderRow As Long = 1
Const lShowRowsPerUser As Long = 3
Const bSortDataByUser As Boolean = False
'Declare variables
Dim ws As Worksheet
Dim rData As Range
Dim rShow As Range
Dim aData() As Variant
Dim aUserRows() As Variant
Dim lTotalUnqUsers As Long
Dim lMaxUserRows As Long
Dim i As Long, j As Long, k As Long
Dim lRandIndex As Long
'Test if sDataSheet name provided exists in ActiveWorkbook
On Error Resume Next
Set ws = ActiveWorkbook.Sheets(sDataSheet)
On Error GoTo 0
If ws Is Nothing Then
MsgBox "No sheet named [" & sDataSheet & "] found in " & ActiveWorkbook.Name & Chr(10) & _
"Correct sDataSheet in code and try again."
Exit Sub
End If
ws.Cells.EntireRow.Hidden = False 'Reset rows to show all data
'Work with the data range set by parameters
With ws.Range(sUserCol & lHeaderRow + 1, ws.Cells(ws.Rows.Count, sUserCol).End(xlUp))
'Verify data exists in specified location
If .Row < lHeaderRow + 1 Then
MsgBox "No data found in [" & sDataSheet & "]" & Chr(10) & _
"Verify column containing users is Column [" & sUserCol & "] or correct sUserCol in code." & Chr(10) & _
"Verify header row is Row [" & lHeaderRow & "] or correct lHeaderRow in code." & Chr(10) & _
"Once corrections have been made and data is available, try again."
Exit Sub
End If
lTotalUnqUsers = Evaluate("SUMPRODUCT((" & .Address(external:=True) & "<>"""")/COUNTIF(" & .Address(external:=True) & "," & .Address(external:=True) & "&""""))") 'Get total unique users
lMaxUserRows = Evaluate("max(countif(" & .Address(external:=True) & "," & .Address(external:=True) & "))") 'Get max rows per user
If bSortDataByUser Then .Sort .Cells, xlAscending, Header:=xlNo 'If bSortByUser is set to True, then sort the data
Set rData = .Cells 'Store the data in a range object for later use
aData = .Value 'Load the data into an array to speed operations
ReDim aUserRows(1 To lTotalUnqUsers, 1 To 3, 1 To lMaxUserRows) 'Ready the results array that random rows will be selected from
End With
'Load all available rows into the results array, grouped by the user
For i = LBound(aData, 1) To UBound(aData, 1)
For j = LBound(aUserRows, 1) To UBound(aUserRows, 1)
If IsEmpty(aUserRows(j, 1, 1)) Or aUserRows(j, 1, 1) = aData(i, 1) Then 'Find correct user
If IsEmpty(aUserRows(j, 1, 1)) Then aUserRows(j, 1, 1) = aData(i, 1) 'If user isn't in results array yet, add it
k = aUserRows(j, 2, 1) + 1 'Increment row counter for this user
aUserRows(j, 2, 1) = k
aUserRows(j, 3, k) = i + lHeaderRow 'Load this row into this user's group of rows
Exit For
End If
Next j
Next i
'Select random rows up to lShowRowsPerUser for each user from the grouped results array
For j = LBound(aUserRows, 1) To UBound(aUserRows, 1)
Do
Randomize
lRandIndex = Int(Rnd() * aUserRows(j, 2, 1)) + 1
If Not rShow Is Nothing Then
Set rShow = Union(rShow, ws.Cells(aUserRows(j, 3, lRandIndex), sUserCol))
Else
Set rShow = ws.Cells(aUserRows(j, 3, lRandIndex), sUserCol)
End If
Loop While rShow.Cells.Count < j * Application.Min(lShowRowsPerUser, aUserRows(j, 2, 1))
Next j
rData.EntireRow.Hidden = True 'Hide all relevant rows
rShow.EntireRow.Hidden = False 'Only show the rows that have been randomly selected
End Sub

Evaluate and Store Complex Expression in Excel VBA

I am working on an accounting VBA program that will post Journal entries to a Ledger, and then generate trial balances (i.e. print out the values on a new sheet following "Bal. " in the Ledger). To do this, I need a way to assign the numerical part of the balance cells to a variable or collection. Unfortunately, when I use Debug.Print I see the only value stored is 0 (I am testing just with Common Stock). My expression is: y = Application.Evaluate("=SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1])") where y represents the balance of Common Stock. How do I properly store the balance value in a variable?
' TODO BE ABLE TO RUN MULTIPLE TIMES
' CHECK FOR POSTED MARK & START WRITING WHEN
' r = "one of the keys", or just creates new Ledger Worksheet every time
Sub MacCompileData()
Application.ScreenUpdating = False
Dim lastRow As Long, x As Long
Dim data, Key
Dim r As Range
Dim cLedger As Collection, cList As Collection
Set cLedger = New Collection
With Worksheets("Journal")
lastRow = .Range("B" & .Rows.Count).End(xlUp).Row
For x = 2 To lastRow
Key = Trim(.Cells(x, 2))
On Error Resume Next
Set cList = cLedger(Key)
If Err.Number <> 0 Then
Set cList = New Collection
cLedger.Add cList, Key
End If
On Error GoTo 0
cLedger(Key).Add Array(.Cells(x, 1).Value, .Cells(x, 3).Value, .Cells(x, 4).Value)
Worksheets("Journal").Cells(x, 5).Value = ChrW(&H2713)
Next
End With
With Worksheets("Ledger")
Dim IsLiability As Boolean
Dim y As Integer
For Each r In .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
If r <> "" Then
On Error Resume Next
Key = Trim(r.Text)
If Key = "LIABILITIES" Then
IsLiability = True
End If
data = getLedgerArray(cLedger(Key))
If Err.Number = 0 Then
Set list = cLedger(Key)
x = cLedger(Key).Count
With r.Offset(2).Resize(x, 3)
.Insert Shift:=xlDown, CopyOrigin:=r.Offset(1)
.Offset(-x).Value = data
If IsLiability Then
.Offset(0, 2).Resize(1, 1).FormulaR1C1 = "=""Bal. "" & TEXT(SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1]),""$#,###"")"
' LOOK HERE FOR Y
y = Application.Evaluate("=SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1])")
Debug.Print "Common Stock Balance Equals "; y
Else
.Offset(0, 1).Resize(1, 1).FormulaR1C1 = "=""Bal. "" & TEXT(SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1]),""$#,###"")"
End If
r.Offset(1).EntireRow.Delete
End With
End If
On Error GoTo 0
End If
Next
End With
Application.ScreenUpdating = True
End Sub
Function getLedgerArray(c As Collection)
Dim data
Dim x As Long
ReDim data(1 To c.Count, 1 To 3)
For x = 1 To c.Count
data(x, 1) = c(x)(0)
data(x, 2) = c(x)(1)
data(x, 3) = c(x)(2)
Next
getLedgerArray = data
End Function
Here is a solution that I was able to figure out, though I am not sure if it is the most efficient. In line before the formula is set, I set a Range named BalanceCell to the cell where the formula will be written. I then used the Mid Function to get the string number value from the cell (since the length of "Bal. " is always 5 characters) after the formula is put into BalanceCell.
If IsLiability Then
Set BalanceCell = .Offset(0, 2).Resize(1, 1)
BalanceCell.FormulaR1C1 = "=""Bal. "" & TEXT(SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1]),""$#,###"")"
y = Mid(BalanceCell.Value, 6, Len(BalanceCell.Value))
Debug.Print "Common Stock Balance is "; y

Modifying VBA copy and paste code to search down rather than across

I have the following VBA code:
Sub test():
Dim NameValue As String, w1 As Worksheet, w2 As Worksheet
Dim i As Long, j As Long, k As Long, c As Long
Set w1 = Sheets("Sheet2"): Set w2 = Sheets("Sheet3")
GetNameValue: For i = 1 To w1.Range("A" & Rows.Count).End(xlUp).row
If w1.Range("A" & i) = "NAME:" Then
If InStr(1, NameValue, w1.Range("B" & i)) Then GoTo GetNext
j = i + 1: Do Until w1.Range("A" & j) = "DATE OF BIRTH:": j = j + 1: Loop
NameValue = Trim(NameValue & " " & w1.Range("B" & i) & "|" & w1.Range("B" & j))
c = c + 1: End If
GetNext: Next i: NameValue = NameValue & " "
For k = 1 To c
i = InStr(1, NameValue, "|"): j = InStr(i, NameValue, " ")
w2.Range("A" & k) = Left(NameValue, i - 1): w2.Range("B" & k) = Mid(NameValue, i + 1, j - i)
NameValue = Mid(NameValue, j + 1, Len(NameValue) - j)
Next k
End Sub
To break down what this code does:
1) Set the first sheet that should be searched and the second sheet (output sheet) that the results should be appended to.
2) Search the first column for a certain string "NAME:" and once found take the value in the second column, place it in the output sheet go look for "DATE OF BIRTH:". Once "DATE OF BIRTH:" is found put it beside the value for "NAME:" in the output sheet.
3) Repeat until there are no more entries.
I'm sure this is a very simple modification, but what I'd like to do is check whether a certain string exists, if it does grab the entry directly BELOW it, and then continue searching for the next string and associated entry just like the code does already.
Can anyone point me to what I would need to change in order to do this (and preferably why)?
In addition, how might I be able to extend this code to run over multiple sheets while depositing the results in a single sheet? Do I need to set up a range running over the worksheets w_1....w_(n-1) (with output sheet w_n possibly in a different workbook)?
Removed Line continuations in code:
Sub test()
Dim NameValue As String, w1 As Worksheet, w2 As Worksheet
Dim i As Long, j As Long, k As Long, c As Long
Set w1 = Sheets("Sheet2")
Set w2 = Sheets("Sheet3")
GetNameValue:
For i = 1 To w1.Range("A" & Rows.Count).End(xlUp).Row
If w1.Range("A" & i) = "NAME:" Then
If InStr(1, NameValue, w1.Range("B" & i)) Then GoTo GetNext
j = i + 1
Do Until w1.Range("A" & j) = "DATE OF BIRTH:"
j = j + 1
Loop
NameValue = Trim(NameValue & " " & w1.Range("B" & i) & "|" & w1.Range("B" & j))
c = c + 1
End If
GetNext:
Next i
NameValue = NameValue & " "
For k = 1 To c
i = InStr(1, NameValue, "|")
j = InStr(i, NameValue, " ")
w2.Range("A" & k) = Left(NameValue, i - 1)
w2.Range("B" & k) = Mid(NameValue, i + 1, j - i)
NameValue = Mid(NameValue, j + 1, Len(NameValue) - j)
Next k
End Sub
UPDATE: Just to make sure we're all on the same page about what the output would look like. Suppose we are searching for the entry below A and the entry beside C:
INPUT
A 1
B
y 3
z 4
t
d
s 7
C 8
A 1
Z
y 3
z 4
t
d
s 7
C 12
OUTPUT
B 8
Z 12
.
.
.
Assuming I understand your desire correctly, you can use the .Offset method with your current range to get to the cell below it. You would need to add a dim, so here's my stab at what you're trying to accomplish:
Sub test()
Dim NameValue As String, w1 As Worksheet, w2 As Worksheet
'new local variable
Dim newValue as string
Dim i As Long, j As Long, k As Long, c As Long
Set w1 = Sheets("Sheet2")
Set w2 = Sheets("Sheet3")
GetNameValue:
For i = 1 To w1.Range("A" & Rows.Count).End(xlUp).Row
'assuming your string is in column A
If w1.Range("A" & i) = "FIND ME" Then
newValue = w1.Range("A" & i).Offset(1,0).Value
End If
If w1.Range("A" & i) = "NAME:" Then
If InStr(1, NameValue, w1.Range("B" & i)) Then GoTo GetNext
j = i + 1
Do Until w1.Range("A" & j) = "DATE OF BIRTH:"
j = j + 1
Loop
NameValue = Trim(NameValue & " " & w1.Range("B" & i) & "|" & w1.Range("B" & j))
c = c + 1
End If
GetNext:
Next i
NameValue = NameValue & " "
For k = 1 To c
i = InStr(1, NameValue, "|")
j = InStr(i, NameValue, " ")
w2.Range("A" & k) = Left(NameValue, i - 1)
w2.Range("B" & k) = Mid(NameValue, i + 1, j - i)
NameValue = Mid(NameValue, j + 1, Len(NameValue) - j)
Next k
End Sub
Then you could do anything you desired with the newValue string, including putting it in w2 like so: w2.Range("D1").value = newValue
UPDATED ANSWER
I am now 89% sure I know what you are trying to accomplish :) thanks for your clarifying example.
To search a range for your search string, you need to set up a range you are looking in:
dim searchRange as range
dim w1,w2 as worksheet
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
set searchRange = w1.Range("A" & Rows.Count).End(xlUp).Row
Then you search the searchRange for both of your search strings (which I'm saying are "A" for the first and "C" for the second). As long as both strings are found in the searchRange, it will create a new Dictionary entry for the two values, having the value below "A" as the key and the value beside "C" as the item.
dim rng as range
dim valueBelowFirstSearch as string
dim resultsDictionary as object
dim i as integer
dim c, d as range
dim cAddress, dAddress as string
set resultsDictionary = CreateObject("scripting.dictionary")
with searchRange
set c = .Find("A", lookin:=xlValues)
set d = .Find("C", lookin:=xlValues)
if not c Is Nothing and not d Is Nothing then
cAddress = c.address
dAddress = d.address
resultsDictionary.add Key:=c.offset(1,0).value, Item:=d.value
Do
set c = .FindNext(c)
set d = .FindNext(d)
Loop While not c is nothing and not d is nothing and c.address <> cAddress and d.address <> dAddress
end if
end with
Now that we have all of the results in the resultsDictionary, we can now output the values into another place, which I'm choosing to be w2.
dim outRange as range
dim item as variant
set outRange = w2.Range("A1")
for each item in resultsDictionary
outRange.Value = item.key
set outRange = outRange.Offset(0,1)
outRange.Value = item.item
set outRange = outRange.Offset(1,-1)
next item
Can anyone point me to what I would need to change in order to do this
(and preferably why)?
Basically you need to change the parts of which NameValue is composed.
Originally you took the value beside the first match as w1.Range("B" & i) and now you want the value below the first match, which is w1.Range("A" & i + 1).
Originally it was:
Trim(NameValue & " " & w1.Range("B" & i) & "|" & w1.Range("B" & j))
Now you need something like this:
Trim(NameValue & " " & w1.Range("A" & i + 1) & "|" & w1.Range("B" & j))
In addition, how might I be able to extend this code to run over
multiple sheets while depositing the results in a single sheet?
(with output sheet w_n possibly in a different workbook)?
To achieve that you can e.g. create an array of Sheets and let the code run for each Sheet of this array. Note that the array might contain 1-N Sheets.
' Set array of sheets for just one sheet
Dim searchedSheets As Sheets
Set searchedSheets = Workbooks("SomeBook.xlsx").Sheets(Array("Sheet1"))
' Set array of sheets for more sheets, e.g. "Sheet1" and "Sheet2" and "Sheet3"
Dim searchedSheets As Sheets
Set searchedSheets = Workbooks("SomeBook.xlsx").Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
' Finally set the second sheet where the results should be appended
' to sheet in the same workbook as the searched sheets
Dim outputSheet As Worksheet
Set outputSheet = Workbooks("SomeBook.xlsx").Worksheets("ResultSheet")
' Or set the second sheet where the results should be appended to sheet
' in a different workbook then the searched sheets belong to
Dim outputSheet As Worksheet
Set outputSheet = Workbooks("SomeOtherBook.xlsx").Worksheets("ResultSheet")
The complete code might look like this (tested with data you provided).
Option Explicit
Public Sub main()
' String to search below of it
Dim string1 As String
string1 = "A"
' String to search beside of it
Dim string2 As String
string2 = "C"
' Set the sheets that should be searched
Dim searchedSheets As Sheets
Set searchedSheets = Workbooks("SomeBook.xlsx").Sheets(Array("Sheet1", "Sheet2"))
' Set the second sheet (outputSheet sheet) that the results should be
' appended to external sheet in different book
Dim outputSheet As Worksheet
Set outputSheet = Workbooks("SomeOtherBook.xlsx").Worksheets("ResultSheet")
SearchFor string1, string2, searchedSheets, outputSheet
End Sub
Public Sub SearchFor( _
string1 As String, _
string2 As String, _
searchedSheets As Sheets, _
output As Worksheet)
Dim searched As Worksheet
Dim NameValue As String
Dim below As String
Dim beside As String
Dim i As Long
Dim j As Long
Dim k As Long
Dim c As Long
Dim rowsCount As Long
For Each searched In searchedSheets
rowsCount = searched.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To rowsCount
' Search the first column for a 'string1'
If searched.Range("A" & i) = string1 Then
' once 'string1' was found grab the entry directly below it
below = searched.Range("A" & i + 1)
If InStr(1, NameValue, below) Then
' skip this 'below' result because it was found before
GoTo GetNext
End If
' Search the first column for a 'string2' starting at the
' position where 'below' was found
For j = i + 1 To rowsCount
If searched.Range("A" & j) = string2 Then
' once 'string2' was found grab the entry directly
' beside it
beside = searched.Range("B" & j)
Exit For
End If
Next j
' Append 'below' and 'beside' to the result and count the
' number of metches
NameValue = Trim(NameValue & " " & below & "|" & beside)
c = c + 1
End If
GetNext:
Next i
Next searched
' Write the output
NameValue = NameValue & " "
For k = 1 To c
i = InStr(1, NameValue, "|")
j = InStr(i, NameValue, " ")
output.Range("A" & k) = Left(NameValue, i - 1)
output.Range("B" & k) = Mid(NameValue, i + 1, j - i)
NameValue = Mid(NameValue, j + 1, Len(NameValue) - j)
Next k
End Sub
Note: I replaced the Do-Until loop with For-Next loop because the Do-Until might cause a Stack-Overflow :-) error if the string "DATE OF BIRTH:" does not exist in the first column. However I have tryied to keep your originall code structure so you still understand it. HTH.
Assuming that you want to find one value (Name:), then continue searching till to find the second one (Date Of Birth:)... Finally, you want to move these pair of data into another worksheet.
To achieve that, i'd suggest to use Dictionary object to get only distinct values. I strongly do not recommend to use string concatenation as you provided in your code!
Option Explicit
Sub Test()
Dim src As Worksheet, dst As Worksheet
Set dst = ThisWorkbook.Worksheets("Sheet2")
For Each src In ThisWorkbook.Worksheets
If src.Name = dst.Name Then GoTo SkipNext
NamesToList src, dst
SkipNext:
Next
End Sub
'needs reference to MS Scripting Runtime library
Sub NamesToList(ByVal srcWsh As Worksheet, ByVal dstWsh As Worksheet, _
Optional ByVal SearchFor As String = "NAME:", Optional ByVal ThenNextFor As String = "DATE OF BIRTH:")
Dim dic As Dictionary, i As Long, j As Long, k As Long
Dim sKey As String, sVal As String
On Error GoTo Err_NamesToList
Set dic = New Dictionary
i = 2
j = GetFirstEmpty(srcWsh)
Do While i < j
If srcWsh.Range("A" & i) = SearchFor Then
sKey = srcWsh.Range("B" & i)
If Not dic.Exists(sKey) Then
Do While srcWsh.Range("A" & i) <> ThenNextFor
i = i + 1
Loop
sVal = srcWsh.Range("B" & i)
dic.Add sKey, sVal
k = GetFirstEmpty(dstWsh)
With dstWsh
.Range("A" & k) = sKey
.Range("B" & k) = sVal
End With
'sKey = ""
'sVal = ""
End If
End If
SkipNext:
i = i + 1
Loop
Exit_NamesToList:
On Error Resume Next
Set dic = Nothing
Exit Sub
Err_NamesToList:
Resume Exit_NamesToList
End Sub
Function GetFirstEmpty(ByVal wsh As Worksheet, Optional ByVal sCol As String = "A") As Long
GetFirstEmpty = wsh.Range(sCol & wsh.Rows.Count).End(xlUp).Row + 1
End Function
Sample output:
Name DateOfBirth:
A 1999-01-01
B 1999-01-02
C 1999-01-03
D 1999-01-04
E 1999-01-05